What am I doing wrong with ADS1115
Posted: Sat Jul 03, 2021 3:35 pm
Following some code from cicciocb, which read from ADC0 on an ADS1115 module (which worked), I tried to read all 4 adc channels by changing this code:
Here's the whole program:
Note: I have a pot between 3V3 and A0 on the ADS1115. Each subsequent channel is connected to the prior one with 1K, and then A3 is connected to 0V with a 1K resistor.
to this:v = 0
for i = 0 to 100000
ADS1115_read v ' read from the module
print v * scale
pause 5000
next i
(v is dimensioned as an array.) I get all zeros for my output. What am I doing wrong?for i = 0 to 100000
for j=4 to 7
ADS1115_setup j, 1, 1
pause 1000
ADS1115_read v(j-3) ' read from the module
' pause 5000
next j
' print v * scale
wlog i;" ";v(1)*scale;" ";v(2)*scale;" ";v(3)*scale;" ";v(4)*scale
next i
Here's the whole program:
Code: [Local Link Removed for Guests]
' ADS1115 Driver for Annex
' datasheet http://www.ti.com/lit/ds/symlink/ads1115.pdf
' ADS1115 Registers
ADS1115_ADDRESS = &h48
ADS1115_CONV_REG = 0 : ADS1115_CONF_REG = 1
ADS1115_HI_T_REG = 2 : ADS1115_LO_T_REG = 3
dim BUFF_IN(2) ' buffer used for read/write to module
dim v(4) ' results array for ADCs 0-3
i2c.setup 4,5 ' set I2C bus
' Set the ADS1115 with :
' AINp = AIN0 and AINn = AIN1
' FSR = ±4.096 V
' 16 SPS
ADS1115_setup 0, 1, 1
' scale in volt
scale = 4.096 / 32768
for i = 0 to 100000
for j=4 to 7
ADS1115_setup j, 1, 1
pause 1000
ADS1115_read v(j-3) ' read from the module
' pause 5000
next j
' print v * scale
wlog i;" ";v(1)*scale;" ";v(2)*scale;" ";v(3)*scale;" ";v(4)*scale
next i
end
'---------------------------------------------------------
' INPUT MULTIPLEX :
' AINp is the input positive
' AINn is the input negative
'0 : AINp = AIN0 and AINn = AIN1
'1 : AINp = AIN0 and AINn = AIN3
'2 : AINp = AIN1 and AINn = AIN3
'3 : AINp = AIN2 and AINn = AIN3
'4 : AINp = AIN0 and AINn = GND
'5 : AINp = AIN1 and AINn = GND
'6 : AINp = AIN2 and AINn = GND
'7 : AINp = AIN3 and AINn = GND
'GAIN
'0 : FSR = ±6.144 V
'1 : FSR = ±4.096 V
'2 : FSR = ±2.048 V
'3 : FSR = ±1.024 V
'4 : FSR = ±0.512 V
'5 : FSR = ±0.256 V
'6 : FSR = ±0.256 V
'7 : FSR = ±0.256 V
'DATA RATE
'0 : 8 SPS
'1 : 16 SPS
'2 : 32 SPS
'3 : 64 SPS
'4 : 128 SPS
'5 : 250 SPS
'6 : 475 SPS
'7 : 860 SPS
sub ADS1115_setup(inp_mux, gain, rate)
local conf
conf = (inp_mux << 12) or (gain << 9) or (rate << 5) or 3 ' + disable comp
BUFF_IN(0) = (conf and &hff00) >> 8 : BUFF_IN(1) = conf and &hff
i2c.WriteRegArray ADS1115_ADDRESS, ADS1115_CONF_REG, 2, BUFF_IN()
end sub
sub ADS1115_read(ret)
i2c.ReadRegArray ADS1115_ADDRESS, ADS1115_CONV_REG, 2, BUFF_IN()
ret = BUFF_IN(0) << 8 + BUFF_IN(1)
if ret > 32768 then ret = ret - 65536
end sub