I am trying to figure out how to use the ADS1115 ADC with my RPi - using some example bash code for i2cset and i2cget I found in this forum, I'm getting inconsistent readings. There seems to be a pattern - so I assume I am doing something wrong in terms of setting up the conversion and/or reading the results. Channel 0 of the ADS1115 is connected to a 10kohm thermistor with a 10k resistor pulled up to 3.3v. The ADS1115 is running off the 3.3 volts from the Rpi. The output does change as the thermistor is heated.
I'm using the latest Raspbian kernel, which has the i2c driver included.
Here's my bash code (I tried averaging the readings):
#! /bin/bash s=0 n=10 for ((i=1; i<=n; i++)) do i2cset -y 1 0x48 1 0xC385 w # convert ADC input 0 r=`i2cget -y 1 0x48 0 w` # read ADC input 0 x=$((((((0xff00 & r)) / 256)) + $((((0x00ff & r)) * 256)))) # reverse words 0 and 1 echo reading $i: raw: $r decimal: $x s=$((s + x)) # sum done echo average: $((s / n)) # typical results - note that every second # reading is shifted. pi@raspberrypi ~/adc $ sudo ./conv0.sh reading 1: raw: 0x6e21 decimal: 8558 reading 2: raw: 0x2217 decimal: 5922 reading 3: raw: 0x6821 decimal: 8552 reading 4: raw: 0x7116 decimal: 5745 reading 5: raw: 0xa923 decimal: 9129 reading 6: raw: 0x3916 decimal: 5689 reading 7: raw: 0xc023 decimal: 9152 reading 8: raw: 0x8d1a decimal: 6797 reading 9: raw: 0xd11e decimal: 7889 reading 10: raw: 0xcb1a decimal: 6859 average: 7429
Anyone have any ideas as to what I am doing wrong? It looks like there is a bit out of place somewhere...
I am an old-school (rusty) C programmer and would prefer to write C code rather than bash scripts or Python (presumably it would be more efficient) so if anyone has any C examples I'd be grateful. Most of the references I've found are for the Arduino in Python...