What am I doing wrong with ADS1115

Recurrent H/W and software problems
Post Reply
lyizb
Posts: 93
Joined: Fri Feb 12, 2021 8:23 pm
Has thanked: 36 times
Been thanked: 23 times

What am I doing wrong with ADS1115

Post by lyizb »

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:
v = 0
for i = 0 to 100000
ADS1115_read v ' read from the module
print v * scale
pause 5000
next i
to this:
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
(v is dimensioned as an array.) I get all zeros for my output. What am I doing wrong?
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
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.
User avatar
cicciocb
Site Admin
Posts: 1889
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 405 times
Been thanked: 1261 times
Contact:

Re: What am I doing wrong with ADS1115

Post by cicciocb »

Hi,
I'm not really sure but I think that is not possible to returns value from SUB inside arrays.

Probably you already found the solution but you could simply do :

Code: [Local Link Removed for Guests]

w = 0
for i = 0 to 100000
  for j=4 to 7
    ADS1115_setup j, 1, 1
    pause 1000
    ADS1115_read w ' read from the module
    v(j-3) = w ' put in the array
    ' pause 5000
  next j
  ' print v * scale
  wlog i;" ";v(1)*scale;" ";v(2)*scale;" ";v(3)*scale;" ";v(4)*scale
next i
lyizb
Posts: 93
Joined: Fri Feb 12, 2021 8:23 pm
Has thanked: 36 times
Been thanked: 23 times

Re: What am I doing wrong with ADS1115

Post by lyizb »

Very sorry in being so late in responding. Summer intervened.

Thank you very much--yes, your suggestion fixed the problem. Here is a range of readings:

Code: [Local Link Removed for Guests]

0 2.62925 1.97425 1.315125 0.658125
1 2.62925 1.974625 1.31425 0.656875
2 2.62875 1.9735 1.31425 0.657
3 2.628375 1.980625 1.31525 0.656875
4 2.628625 1.97325 1.3135 0.655625
5 2.627875 2.293125 1.647875 0.82425
6 3.295875 2.474875 1.64875 0.82475
7 3.2975 2.475875 1.649 0.82475
8 3.299625 2.477625 1.649 0.82525
9 3.30025 2.478125 1.650375 0.8255
10 3.300625 2.47825 1.650625 0.8255
11 3.300625 2.2035 1.34975 0.674875
12 2.70025 2.027375 1.350625 0.675625
13 2.700875 2.027875 1.349125 0.576875
14 2.28125 1.712875 1.1405 0.569875
15 2.28175 1.713 1.139125 0.413125
16 1.65825 1.2435 0.828125 0.4135
17 1.363375 0.82325 0.547875 0.273
18 1.0985 0.74875 0.48275 0.24025
19 0.967875 0.726125 0.48275 0.240125
20 0.967875 0.726125 0.48275 0.24025
21 0.967875 0.726125 0.48275 0.24
22 0.96775 0.725625 0.482 0.239125
And here is the code:

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
 
w=0
for i = 0 to 100000
  for j=4 to 7
    ADS1115_setup j, 1, 1  
    pause 1000
    ADS1115_read w ' read from the module
    v(j-3) = w ' put in the array
  next j
  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
Post Reply