Hi,
Does anybody know of C example code for reading the four analogue inputs on the ADS1015?
The python code on the adafruit github works fine, but my project is in C and I'm struggling to figure it out.
I think the main problem is setting all the configuration settings before an attempt is made to read it.
I'm trying to marry up the integers written in the python code with the datasheet, but if anyone has walked this path before, that would be a great help.
Thanks
ADS1015 C code
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- adafruit_support_bill
- Posts: 88094
- Joined: Sat Feb 07, 2009 10:11 am
Re: ADS1015 C code
The Arduino code is in C++, but sticks pretty close to plain vanilla C. https://github.com/adafruit/Adafruit_ADS1X15
-
- Posts: 3
- Joined: Thu Jan 23, 2014 12:30 pm
Re: ADS1015 C code
Thanks Bill,
I hadn't found that code, I was only focusing on the Raspberry Pi stuff.
if I try and reduce the program to a few lines, does it basically involve writing a config byte to the register 0x00, then reading two bytes from that register?
I hadn't found that code, I was only focusing on the Raspberry Pi stuff.
if I try and reduce the program to a few lines, does it basically involve writing a config byte to the register 0x00, then reading two bytes from that register?
- adafruit_support_bill
- Posts: 88094
- Joined: Sat Feb 07, 2009 10:11 am
Re: ADS1015 C code
That is pretty much it. Most of the work is in figuring out which bits to set.
-
- Posts: 3
- Joined: Thu Jan 23, 2014 12:30 pm
Re: ADS1015 C code
Ok, thanks.
Now that I know that I think the datasheet makes a bit more sense too.
Now that I know that I think the datasheet makes a bit more sense too.
- brianp
- Posts: 5
- Joined: Tue Aug 21, 2012 7:46 am
Re: ADS1015 C code
I'm curious to see if this worked out for you or anyone else that may have tried it. I too am trying to adapt the C++ library provided to work in a c based project and am having a tough time getting the i2c working. I'm using the wiringpii2c library and have not been able to get any meaningful responses back from the 1015. I am able to run the Adafruit python script for the 1015 and get good results from it so I believe my device and hardware setup are both ok.
I thought I'd start here. If this is a dead thread, I'll start a new topic with some code snippets and more detail to see if I can get some extra eyes looking at this.
Brian
I thought I'd start here. If this is a dead thread, I'll start a new topic with some code snippets and more detail to see if I can get some extra eyes looking at this.
Brian
- jhansen317
- Posts: 1
- Joined: Fri Feb 05, 2016 7:02 pm
Re: ADS1015 C code
I was running into the same issues described above (also on a raspberry pi) - I managed to get intelligible readings, at least for single-ended reads so far, by using the wiringPi i2c library (https://projects.drogon.net/raspberry-p ... c-library/) along with all the constants handily #defined in the c++ library linked to above. Thanks to lots of help from my more qualified boss, turns out the trick is that you have to reverse the byte order of the data that you're sending and receiving with ntohs():
I hope this helps!
Code: Select all
uint16_t readADC_SingleEnded(int fd, uint8_t channel)
{
uint16_t config = ADS1015_REG_CONFIG_CQUE_NONE | // Disable the comparator (default val)
ADS1015_REG_CONFIG_CLAT_NONLAT | // Non-latching (default val)
ADS1015_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val)
ADS1015_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val)
ADS1015_REG_CONFIG_DR_1600SPS | // 1600 samples per second (default)
ADS1015_REG_CONFIG_MODE_SINGLE; // Single-shot mode (default)
config |= ADS1015_REG_CONFIG_PGA_6_144V;
// Set single-ended input channel
switch (channel)
{
case (0):
config |= ADS1015_REG_CONFIG_MUX_SINGLE_0;
break;
case (1):
config |= ADS1015_REG_CONFIG_MUX_SINGLE_1;
break;
case (2):
config |= ADS1015_REG_CONFIG_MUX_SINGLE_2;
break;
case (3):
config |= ADS1015_REG_CONFIG_MUX_SINGLE_3;
break;
}
// Set 'start single-conversion' bit
config |= ADS1015_REG_CONFIG_OS_SINGLE;
wiringPiI2CWriteReg16(fd, ADS1015_REG_POINTER_CONFIG, ntohs(config));
delay(ADS1015_CONVERSIONDELAY);
return ntohs(wiringPiI2CReadReg16(fd, ADS1015_REG_POINTER_CONVERT) >> 4);
}
- scottd72
- Posts: 1
- Joined: Tue Jan 30, 2018 1:54 am
Re: ADS1015 C code
jhansen317's code helped me a lot, but I believe it has a bug in readADC_SingleEnded...namely, the final shift right of 4 bits is in the wrong place. The return statement should read:
return ntohs(wiringPiI2CReadReg16(fd, ADS1015_REG_POINTER_CONVERT)) >> 4;
return ntohs(wiringPiI2CReadReg16(fd, ADS1015_REG_POINTER_CONVERT)) >> 4;
Please be positive and constructive with your questions and comments.