I have a ADS1115 breakout board from adafruit connected to my RPi 3B connected via I2C. It is powered via the RPi 3.3V pin. The A0 input to the ADC is connected to a sensor that will produce out-voltages varying wildly between 0 and 3.3V. For outputs around ~1V I have made some experiments and checked that the ADC readings are good - at least on the order of magntude, but I am now looking into the lower range, on the order of mV.
Running the below code in Python 3.5
import Adafruit_ADS1x15 # pip3 install git+https://github.com/adafruit/Adafruit_Python_ADS1x15.git
from statistics import mean, stdev
from math import sqrt
adc = Adafruit_ADS1x15.ADS1115()
max_val = 2**15 # signed 16 bit number is read
gain_levels =[ # according to the library
(2/3,6.144),
(1 ,4.096),
(2 ,2.048),
(4 ,1.024),
(8 ,0.512),
(16 ,0.256),
]
n_samples = 20
for gain,max_volts in gain_levels:
readings = [adc.read_adc(channel=0,gain=gain,data_rate=8) for _ in range(n_samples)]
readings_in_volt = [max_volts * (reading / max_val) for reading in readings ]
avg = mean(readings_in_volt)
sem = stdev(readings_in_volt)/sqrt(n_samples)
res= "{gain:5.2f} {avg_mv:.2f} mV +- {ci_mv:.2f} mV".format(
gain=gain,
avg_mv=avg*1000,
ci_mv=1.96*sem*1000
)
print(res)
produces the following
0.67 54.00 mV +- 4.02 mV
1.00 56.70 mV +- 4.03 mV
2.00 52.25 mV +- 3.91 mV
4.00 64.30 mV +- 3.71 mV
8.00 29.26 mV +- 3.31 mV
16.00 29.13 mV +- 3.50 mV
Taking a reading with a multimeter gives 40.0 mV. As you can see, it seems that there is bias in the readings, and that the bias is gain dependant. I am very new to I2C communication and working with bit manpulation, so I am a little worried there is something off in either my own calculations, or in the gain settings in the library.
How would I go about to troubleshoot these inconsistent readings? Am I doing something strange converting the integer reading from the device to a voltage-value?
-- EDIT I think that my problem was a hardware problem on my part, and not a programming issue. Possible causes are:
- Frying the ADC chip some time earlier on
- Not providing the 0.000150 A current draw that the ADC needs.
- Using a different ground in the multimeter reading and the ADC reading. I dont have the original setup anymore, and cannot troubleshoot.
I will keep this question here, becuase of the good answer, even though I think my resolution was something different.