I’m trying to get a 4 channel 16 bit ADC (<LINK_TEXT text=“https://www.seeedstudio.com/4-Channel-1 … -2829.html”>https://www.seeedstudio.com/4-Channel-16-Bit-ADC-for-Raspberry-Pi-ADS1115-p-2829.html</LINK_TEXT>) working on a Raspberry Pi 4.
I went through the wiki and everything works as far as the install and the ads1115.sh script in pi-hats.
My problem is getting some C code to interface with it. For example, this code fails at the
test on line 26…
</s>ioctl(fd, I2C_SLAVE, ads_address) < 0<e>
[code]///////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <sys/types.h> // open
#include <sys/stat.h> // open
#include <fcntl.h> // open
#include <unistd.h> // read/write usleep
#include <sys/ioctl.h>
#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)val4.096/32768.0);
close(fd);
return 0;
}[/code]
Does anyone have some C code that does an analog in conversion that they know works with this device on a Raspberry Pi 4?
Thanks
J