Reading two ADS1115 ADC i2c

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
Algomez
 
Posts: 2
Joined: Fri Jul 23, 2021 2:01 am

Reading two ADS1115 ADC i2c

Post by Algomez »

Hi to everybody!

I recently started a project using Arduino UNO and the ADS1115 ADC module (https://learn.adafruit.com/adafruit-4-c ... -breakouts) followed all instructions. Everything worked perfectly according to the documentation provided. I've been able to log (SD loggershield) differential voltages from pressure transducers MPX2010DP using differential channels A0_A1 and A2_A3 with one ADS1115 ADC using default address 0x48 using ADR-GND pins. When trying to read the data from another two pressure transducers with a second ADS11115 ADC unit and setting the address 0x49, using ADR-VDD pins and read with the two differential channels of the second ADS1115 unit A0_A1 and A2_A3. And defining the addresses as with

ads.begin(0x48);
ads.begin(0x49);

according to, (https://learn.adafruit.com/adafruit-4-c ... -2974130-2), quoted in an earlier thread (viewtopic.php?f=25&t=181255&p=881401&hi ... 15#p881401)

The logging collapses after a few minutes of logging

¿How do I specify each ADC unit address and read the data from each distinct ADS1115 unit?
The documentation indicates the possible ADC address setting but no information or code related to several ADS1115 ADC units working a the same time.

I need to log up to eight pressure transducers and I intend to use four ADS1115 modules.

Any help is welcome!!

Best regards

AGT

Code: Select all

#include <SD.h>
#include <Wire.h>
// #include <Time.h>
#include <DS1307RTC.h> 
#include <Adafruit_ADS1X15.h>
#include <SPI.h>


/// Time variables
uint32_t UnixTime; // linuxtime RTC
long int YearInMillenium;
long int MonthInYear;
long int DayInMonth;
long int HoursInDay;
long int MinutesInHour;
long int SecondsInMinute;
//long int SecondsInDay;

 Adafruit_ADS1115 ads; /* Use this for the 16-bit version */
const int chipSelect = 10;

void setup() {
pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(9600);
  
// see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1);
  }
   

   
   // RTC and Time
  setSyncProvider(RTC.get); 
  UnixTime = now();
  YearInMillenium = year();
  MonthInYear = month();
  DayInMonth = day();
  HoursInDay = hour();
  MinutesInHour = minute();
  SecondsInMinute = second();


Serial.println("Unixtime (s), Time (Y/M/D H:M:S), Dv1 (mV), Dv2 (mV), Vin");

ads.begin(0x48);
ads.begin(0x49);
   
} // End setup
// #############################################################



void loop() {
 // Reads the clock to the Arduino and calculates time variables
  UnixTime = now();
  setSyncProvider(RTC.get); 
  YearInMillenium = year();
  MonthInYear = month();
  DayInMonth = day();
  HoursInDay = hour();
  MinutesInHour = minute();
  SecondsInMinute = second();

  
  // Starting ADS1115_adc1
  ads.begin(0x48);
  ads.setGain(GAIN_SIXTEEN);    // setting the gain 16x gain for adc1  +/- 0.256V  1 bit = 0.125mV  0.0078125mV
  int16_t results;
  int16_t results2;
  results = ads.readADC_Differential_0_1();
  results2 = ads.readADC_Differential_2_3();

  delay(250);
  
  
  // Starting ADS1115_adc2
  ads.begin(0x49);
  ads.setGain(GAIN_SIXTEEN);    // Setting the gain for adc2 16x gain  +/- 0.256V  1 bit = 0.125mV  0.0078125mV
  int16_t results3;
  int16_t results4;
  results3 = ads.readADC_Differential_0_1();
  results4 = ads.readADC_Differential_2_3();

  delay(250);
  
    /* Be sure to update this value based on the IC and the gain settings! */
  float multiplier = 0.1875F; /* ADS1115  @ +/- 6.144V gain (16-bit results) */

  int VoltValue = analogRead(A0);
  float VoltDec = (VoltValue/1023)*5;

  // Make a string for assembling the data to log:
    String dataString = "";
    dataString += String(UnixTime);
    dataString += ", ";
    dataString += String(YearInMillenium);
    dataString += "/";
    dataString += String(MonthInYear);
    dataString += "/";
    dataString += String(DayInMonth);
    dataString += " ";
    dataString += String(HoursInDay);
    dataString += ":";
    dataString += String(MinutesInHour);
    dataString += ":";
    dataString += String(SecondsInMinute);
    dataString += ", ";
    dataString += String(results*multiplier);
    dataString += ", ";
    dataString += String(results2*multiplier);
    dataString += ", ";
    dataString += String(results3*multiplier);
    dataString += ", ";
    dataString += String(results4*multiplier);


    // open the file to log and log the data line
    File dataFile = SD.open("data.csv", FILE_WRITE);
if (dataFile) {
      dataFile.println(dataString);
      dataFile.close();
    }
        else {
      Serial.println("data.csv ?");
    }
    delay(100);
    

    Serial.println(dataString);

   delay(5000); /// 5 sec
 
 }

User avatar
dastels
 
Posts: 15608
Joined: Tue Oct 20, 2015 3:22 pm

Re: Reading two ADS1115 ADC i2c

Post by dastels »

I see two things:
1) you call ads.begin() and setGain() in the loop. That should be done(well, certainly the begin) once in setup.
2) then I saw the more fundamental problem (which explains the first one): you need to create a separate object for each sensor, using begin to attach it to an I2C address in setup(). Then just read it in loop.

Dave

User avatar
Algomez
 
Posts: 2
Joined: Fri Jul 23, 2021 2:01 am

Re: Reading two ADS1115 ADC i2c

Post by Algomez »

Thank you very much for your useful reply Dave.

Did exactly what you suggested, first I created two objects named ads1 and ads2

Code: Select all

 Adafruit_ADS1115 ads1; // First ADS1115 for the (0x48) address object
 Adafruit_ADS1115 ads2; // Second ADS1115 for the (0x49) address object
Second, within the setup(), I defined the gain for ads1 and ads2, and then used begin and the specific I²C address of each ADS1115.

Code: Select all

ads1.setGain(GAIN_SIXTEEN);    // 16x gain  +/- 0.256V  1 bit = 0.125mV  0.0078125mV
ads2.setGain(GAIN_SIXTEEN);    // 16x gain  +/- 0.256V  1 bit = 0.125mV  0.0078125mV

// Starting ads1 at 0x48 I²C position
if (!ads1.begin(0x48)) {
   Serial.println("Failed to initialize ADS1.");
   while (1);
  }

// Starting ads1 at 0x49 I²C position
if (!ads2.begin(0x49)) {
   Serial.println("Failed to initialize ADS2.");
   while (1);
  }



Third, within the loop(), I command the Arduino to read each ads object (ads1 and ads2), using differential mode and write the reading in four variables, results and results2 for the first ADS1115 with 0x48 address, declared as ads1, and results3 and results4 for the second ADS1115 with 0x49 address.

Code: Select all

  // Reading the first ADS1115; ads1
  int16_t results;
  int16_t results2;
  results = ads1.readADC_Differential_0_1();
  results2 = ads1.readADC_Differential_2_3();

  delay(250);
  
  
  // Reading the second ADS1115; ads2
  int16_t results3;
  int16_t results4;
  results3 = ads2.readADC_Differential_0_1();
  results4 = ads2.readADC_Differential_2_3();

  delay(250);

The output is just perfect both in the serial monitor and the SD card that logs the differential voltage data.

The next version of the code can be seen below:

Code: Select all

/

#include <SD.h>
#include <Wire.h>
// #include <Time.h>
#include <DS1307RTC.h> 
#include <Adafruit_ADS1X15.h>
#include <SPI.h>


/// Time variables
uint32_t UnixTime; // linuxtime RTC
long int YearInMillenium;
long int MonthInYear;
long int DayInMonth;
long int HoursInDay;
long int MinutesInHour;
long int SecondsInMinute;
//long int SecondsInDay;

#define I2C_ADDRESS_1  0x48
#define I2C_ADDRESS_2  0x49

// ADS1115 Variables

 Adafruit_ADS1115 ads1; // First ADS1115 (0x48) object
 Adafruit_ADS1115 ads2; // Second ADS1115 (0x49) object

const int chipSelect = 10; // For the Datalogger shield

void setup() {
pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(9600);
  
// If the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1);
  }
   

   
   // RTC and Time
  setSyncProvider(RTC.get); 
  Serial.println();

  UnixTime = now();
  YearInMillenium = year();
  MonthInYear = month();
  DayInMonth = day();
  HoursInDay = hour();
  MinutesInHour = minute();
  SecondsInMinute = second();

  /// ADS115 gain
  // ads.setGain(GAIN_TWOTHIRDS);  // 2/3x gain +/- 6.144V  1 bit = 3mV      0.1875mV (default)
  // ads.setGain(GAIN_ONE);        // 1x gain   +/- 4.096V  1 bit = 2mV      0.125mV
  // ads.setGain(GAIN_TWO);        // 2x gain   +/- 2.048V  1 bit = 1mV      0.0625mV
  // ads.setGain(GAIN_FOUR);       // 4x gain   +/- 1.024V  1 bit = 0.5mV    0.03125mV
  // ads.setGain(GAIN_EIGHT);      // 8x gain   +/- 0.512V  1 bit = 0.25mV   0.015625mV
ads1.setGain(GAIN_SIXTEEN);    // 16x gain  +/- 0.256V  1 bit = 0.125mV  0.0078125mV
ads2.setGain(GAIN_SIXTEEN);    // 16x gain  +/- 0.256V  1 bit = 0.125mV  0.0078125mV


// Initializing both ADS1115 modules
// Starting ads1 at 0x48 I²C position
if (!ads1.begin(0x48)) {
   Serial.println("Failed to initialize ADS1.");
   while (1);
  }

// Starting ads1 at 0x49 I²C position
if (!ads2.begin(0x49)) {
   Serial.println("Failed to initialize ADS2.");
   while (1);
  }
  

Serial.println("Sap-Flow sensor AGT, INIERNA-UMSNH");  
Serial.println("Adress1, Adress2");   // Just to check the adresses

Serial.print(I2C_ADDRESS_1);
Serial.print(", ");
Serial.println(I2C_ADDRESS_2);

Serial.println("Unixtime (s), Time (Y/M/D H:M:S), Dv1 (mV), Dv2 (mV), Vin");

   
} // End setup
// #############################################################



void loop() {
 // Reads the clock to the Arduino and calculates time variables
  UnixTime = now();
  setSyncProvider(RTC.get); 
  YearInMillenium = year();
  MonthInYear = month();
  DayInMonth = day();
  HoursInDay = hour();
  MinutesInHour = minute();
  SecondsInMinute = second();

  
  // Reading the first ADS1115; ads1
  int16_t results;
  int16_t results2;
  results = ads1.readADC_Differential_0_1();
  results2 = ads1.readADC_Differential_2_3();

  delay(250);
  
  
  // Reading the second ADS1115; ads2
  int16_t results3;
  int16_t results4;
  results3 = ads2.readADC_Differential_0_1();
  results4 = ads2.readADC_Differential_2_3();

  delay(250);
  
    /* Be sure to update this value based on the IC and the gain settings! */
  float multiplier = 0.1875F; /* ADS1115  @ +/- 6.144V gain (16-bit results) */

  int VoltValue = analogRead(A0);
  float VoltDec = (VoltValue/1023)*5;

  // Make a string for assembling the data to log:
    String dataString = "";
    dataString += String(UnixTime);
    dataString += ", ";
    dataString += String(YearInMillenium);
    dataString += "/";
    dataString += String(MonthInYear);
    dataString += "/";
    dataString += String(DayInMonth);
    dataString += " ";
    dataString += String(HoursInDay);
    dataString += ":";
    dataString += String(MinutesInHour);
    dataString += ":";
    dataString += String(SecondsInMinute);
    dataString += ", ";
    dataString += String(results*multiplier);
    dataString += ", ";
    dataString += String(results2*multiplier);
    dataString += ", ";
    dataString += String(results3*multiplier);
    dataString += ", ";
    dataString += String(results4*multiplier);
    // dataString += ", ";
    // dataString += String(VoltValue);

    // open the file to log and log the data line
    File dataFile = SD.open("data.csv", FILE_WRITE);
if (dataFile) {
      dataFile.println(dataString);
      dataFile.close();
    }
        else {
      Serial.println("data.csv ?");
    }
    delay(100);
    

    Serial.println(dataString);

  //delay(59500); /// 1 minute
   delay(5000); /// 5 sec


  
 }
Problem solved!!

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”