tomdean
Posts: 28
Joined: Tue Feb 19, 2013 4:02 am

ADS1115 Problem with C Code

Wed Mar 06, 2013 9:44 pm

I have an ADS1115 from adafruit connected to my rpi i2c 1.
I am using ain0 single ended.
I use a breadboard and a jumper to connect ain0 either to ground or vdd.

I can read the ads1115 from the command line with i2cset and i2cget. Ok.
I can read the ads1115 with the adafruit python code. Ok.
I can read the ads1115 with C code with one problem. If I ground ain0, I can read approx 0v. If I then connect ain0 to vdd, the first time I execute the code, I still get 0v. The next time I execute the code, I get 3.299v as I expect.
There must be some simple problem I am overlooking. But, what?

Code: Select all

///////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <sys/types.h> // open
#include <sys/stat.h>  // open
#include <fcntl.h>     // open
#include <unistd.h>    // read/write usleep
#include <stdlib.h>    // exit
#include <inttypes.h>  // uint8_t, etc
#include <linux/i2c-dev.h> // I2C bus definitions
////////////////////////////////////////////////
// main
int main() {
  int fd;
  int ads_address = 0x48;
  uint8_t buf[10];
  int16_t val;

  // open device on /dev/i2c-1 the default on Raspberry Pi B
  if ((fd = open("/dev/i2c-1", O_RDWR)) < 0) {
	printf("Error: Couldn't open device! %d\n", fd);
	return 1;
  }

  // connect to ads1115 as i2c slave
  if (ioctl(fd, I2C_SLAVE, ads_address) < 0) {
	printf("Error: Couldn't find device on address!\n");
	return 1;
  }
  
  ///////////////////////////////
  // set config register and start conversion
  // AIN0 and GND, 4.096v, 128s/s
  buf[0] = 1;    // config register is 1
  buf[1] = 0xc3;
  buf[2] = 0x85;
  if (write(fd, buf, 3) != 3) {
	perror("Write to register 1");
	exit(-1);
  }
  //////////////////////////////
  // wait for conversion complete
  do {
	if (read(fd, buf, 2) != 2) {
	  perror("Read conversion");
	  exit(-1);
	}
  } while (buf[0] & 0x80 == 0);
  //////////////////////////////
  // read conversion register
  buf[0] = 0;   // conversion register is 0
  if (write(fd, buf, 1) != 1) {
	perror("Write register select");
	exit(-1);
  }
  if (read(fd, buf, 2) != 2) {
	perror("Read conversion");
	exit(-1);
  }
  //////////////////////////////
  // convert output and display results
  val = (int16_t)buf[0]*256 + (uint16_t)buf[1];
  printf("Conversion %02x %02x %d %f\n",
		 buf[0], buf[1], val, (float)val*4.096/32768.0);

  close(fd);

  return 0;
}

tomdean
Posts: 28
Joined: Tue Feb 19, 2013 4:02 am

Re: ADS1115 Problem with C Code

Fri Mar 08, 2013 5:09 am

Has anyone worked with C and the ADS1115?

totoharibo
Posts: 4647
Joined: Thu Jan 24, 2013 8:43 am

Re: ADS1115 Problem with C Code

Fri Mar 08, 2013 7:12 am

have a look in the MagPi.
There are articles about using I2C with Python or C.

tomdean
Posts: 28
Joined: Tue Feb 19, 2013 4:02 am

Re: ADS1115 Problem with C Code

Fri Mar 08, 2013 11:17 pm

Several articles about using python and i2c.
None using C.

rvbglas
Posts: 1
Joined: Wed Apr 12, 2017 10:02 pm

Re: ADS1115 Problem with C Code

Wed Apr 12, 2017 10:08 pm

Just give ADS some time to react to write and clear "ready" bit (or you just read previous result, maybe different pin, yes). C code is way faster than Python or shell.

Code: Select all

  if (write(fd, buf, 3) != 3) {
   perror("Write to register 1");
   exit(-1);
  }
  
  usleep(10000);

  //////////////////////////////
  // wait for conversion complete
  do {
   if (read(fd, buf, 2) != 2) {
     perror("Read conversion");
     exit(-1);
   }
Works like a charm now ;).

User avatar
gordon@drogon.net
Posts: 2024
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK

Re: ADS1115 Problem with C Code

Thu Apr 13, 2017 2:18 pm

Old thread resurrection, but there is now a driver in wiringPi for the ads1115:

Code: Select all

  gpio -x ads1115:200:0x48 aread 200
for a quick test (reads channel 0), or in C

Code: Select all

#include <ads1115.h>
#define MY_BASE 2222
...
  ads1115Setup (MY_BASE, 0x48) ;
  int ch0 = analogRead (MY_BASE + 0) ;
  int ch3 = analogRead (MY_BASE + 3) ;
// and so on
you can read the differential channels too and change the gains/samples if required - probably best to see the source code for the runes on them.

-Gordon
--
Gordons projects: https://projects.drogon.net/

User avatar
Zili
Posts: 7
Joined: Wed Jan 13, 2016 2:48 pm

Re: ADS1115 Problem with C Code

Thu Feb 01, 2018 1:52 pm

careful guys,
the while on the waiting loop is wrong.
try this way:

Code: Select all

do {
	if (read(fd, buf, 2) != 2) {
	  perror("Read conversion");
	  exit(-1);
	}
  } while (!(buf[0] & 0x80));

bandejiya
Posts: 2
Joined: Wed Jan 09, 2019 6:38 am

Re: ADS1115 Problem with C Code

Wed Jan 09, 2019 7:15 am

Zili wrote:
Thu Feb 01, 2018 1:52 pm
careful guys,
the while on the waiting loop is wrong.
try this way:

Code: Select all

do {
	if (read(fd, buf, 2) != 2) {
	  perror("Read conversion");
	  exit(-1);
	}
  } while (!(buf[0] & 0x80));
should not it be buf[1] instead of buf[0] as the status bit in the status register is 15th bit?

Return to “C/C++”