Hi Stephen,
Thanks again for helping me with sensors. During last few days I have been trying to work with PH sensor but I cannot understand how to convert the following python code to c or c++.
Would you please if it is possible help me with that.
I am using ADS 1115 and I know that there is a prebuilt c library for that.
import sys
sys.path.append(ââŚ/â)
import time
ADS1115_REG_CONFIG_PGA_6_144V = 0x00 # 6.144V range = Gain 2/3
ADS1115_REG_CONFIG_PGA_4_096V = 0x02 # 4.096V range = Gain 1
ADS1115_REG_CONFIG_PGA_2_048V = 0x04 # 2.048V range = Gain 2 (default)
ADS1115_REG_CONFIG_PGA_1_024V = 0x06 # 1.024V range = Gain 4
ADS1115_REG_CONFIG_PGA_0_512V = 0x08 # 0.512V range = Gain 8
ADS1115_REG_CONFIG_PGA_0_256V = 0x0A # 0.256V range = Gain 16
from DFRobot_ADS1115 import ADS1115
from DFRobot_PH import DFRobot_PH
ads1115 = ADS1115()
ph = DFRobot_PH()
ph.begin()
while True :
#Read your temperature sensor to execute temperature compensation
temperature = 25
#Set the IIC address
ads1115.setAddr_ADS1115(0x48)
#Sets the gain and input voltage range.
ads1115.setGain(ADS1115_REG_CONFIG_PGA_6_144V)
#Get the Digital Value of Analog of selected channel
adc0 = ads1115.readVoltage(0)
#Convert voltage to PH with temperature compensation
PH = ph.readPH(adc0[ârâ],temperature)
print âTemperature:%.1f ^C PH:%.2fâ %(temperature,PH)
time.sleep(1.0)
My code so far is like this
#include <wiringPi.h>
#include
#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 <ads1115.h>
#include <linux/i2c-dev.h>
//#define BASE_I2C 456
using namespace std;
int main() {
int fd;
int ads_address = 0x49;
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] = 0x00; // config register is 1
buf[1] = 0x02;
buf[2] = 0x04;
buf[3] = 0x06;
buf[4] = 0x08;
buf[5] = 0x0A;
//printf("Conversion %02x \n", buf[5]);
if (write(fd, buf, 3) != 3) {
perror("Write to register 1");
exit(-1);
}
I really appreciate your help