Ross46
Posts: 22
Joined: Thu Mar 15, 2018 10:22 pm

read all four ads1115 inputs

Fri Sep 07, 2018 10:23 am

hi all

ive been testing with an ads1115 to measure some analog accelerometers, however i'm having an issue.

the code below is unfortunately from an arduino instead of a Pi as this is what i could find the code on with registers i mostly understand. but it is in C so i'm hoping someone can help.

Code: Select all

/*

Read pot on AIN1 convert value to voltage
then display on Arduino serial monitor.

By Lewis Loflin [email protected]
http://www.sullivan-county.com/main.htm
Electronics website:
http://www.bristolwatch.com/index.htm 

*/

#include <Wire.h> // specify use of Wire.h library
#define ASD1115 0x48

unsigned int val = 0;
byte writeBuf[3];
byte buffer[3];
float t_micro;

const float VPS = 4.096 / 32768.0; // volts per step

void setup()   {

  Serial.begin(115200); 
  Wire.begin(); // begin I2C

  // ASD1115
  // set config register and start conversion
  // ANC1 and GND, 4.096v, 128s/

  writeBuf[0] = 1;    // config register is 1
  
  // writeBuf[1] = 0b11010000; // 0xC2 single shot off <== NEW -  single conversion/ AIN1 & GND/ 6.144V/ Continuous (0)
  
  writeBuf[1] = 0b11010010; // 0xC2 single shot off <== ORIGINAL - single conversion/ AIN1 & GND/ 4.096V/ Continuous (0)
  
  // bit 15 flag bit for single shot
  // Bits 14-12 input selection:
  // 100 ANC0; 101 ANC1; 110 ANC2; 111 ANC3
  // Bits 11-9 Amp gain. Default to 010 here 001 P19
  // Bit 8 Operational mode of the ADS1115.
  // 0 : Continuous conversion mode
  // 1 : Power-down single-shot mode (default)


  //writeBuf[2] = 0b10000101; // bits 7-0  0x85 //8 SPS
  //writeBuf[2] = 0b10000101; // bits 7-0  0x85 //128 SPS
  writeBuf[2] = 0b11100101; // bits 7-0  0x85 //869 SPS 
  
  // Bits 7-5 data rate default to 100 for 128SPS
  // Bits 4-0  comparator functions see spec sheet.

  // setup ADS1115
  Wire.beginTransmission(ASD1115);  // ADC 
  Wire.write(writeBuf[0]); 
  Wire.write(writeBuf[1]);
  Wire.write(writeBuf[2]);  
  Wire.endTransmission();  

  delay(500);

}  // end setup

void loop() {

  buffer[0] = 0; // pointer
  Wire.beginTransmission(ASD1115);  // DAC
  Wire.write(buffer[0]);  // pointer
  Wire.endTransmission();

  Wire.requestFrom(ASD1115, 2);
  buffer[1] = Wire.read();  // 
  buffer[2] = Wire.read();  // 
  Wire.endTransmission();  

  // convert display results
  val = buffer[1] << 8 | buffer[2]; 

t_micro = micros();

  if (val > 32768) val = 0;
  Serial.print(t_micro/1000000.0,6);
  Serial.print("\t");
  Serial.print(val * VPS);
  Serial.print("\t");
  Serial.println(val);
  
} // end loop




My question is this. How do you set up the ADC so you can measure 4 single ended inputs? I understand most of what is being done to the config register, but not the declaring inputs.

Code: Select all

  writeBuf[1] = 0b11010010; // 0xC2 single shot off <== ORIGINAL - single conversion/ AIN1 & GND/ 4.096V/ Continuous (0)
  
  // bit 15 flag bit for single shot
  // Bits 14-12 input selection:
  // 100 ANC0; 101 ANC1; 110 ANC2; 111 ANC3
  // Bits 11-9 Amp gain. Default to 010 here 001 P19
  // Bit 8 Operational mode of the ADS1115.
  // 0 : Continuous conversion mode
  // 1 : Power-down single-shot mode (default)
How would you go about configuring all the inputs so you can use all 4 pins? From what i can see this code is only selecting 'ANC1'. Is this as a reference point for the ADC to compare the voltage to or do you have to repeatedly configure the register each time?

Any help would be greatly appreciated

Thanks

danjperron
Posts: 3096
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: read all four ads1115 inputs

Fri Sep 07, 2018 1:34 pm

Maybe the best way is to check the python code from adafruit.

it is written in C for python then it will be easy to use the C parts of it.

git clone https://github.com/adafruit/Adafruit_ADS1X15

and check the source code.

Return to “C/C++”