Why doesn't this code work? What has changed?

176 views
Skip to first unread message

davep...@gmail.com

unread,
Oct 28, 2020, 5:05:01 PM10/28/20
to Annex WiFi RDS
I am trying to implement usage of an ads1115 and using the code provided I get a syntax error.

' 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

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

 

v = 0

for i = 0 to 100000

 ADS1115_read v  ' read from the module

 print v * 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

davep...@gmail.com

unread,
Oct 29, 2020, 12:01:30 PM10/29/20
to Annex WiFi RDS
The syntax error is this line:
i2c.WriteRegArray ADS1115_ADDRESS, ADS1115_CONF_REG, 2, BUFF_IN() 

I have loaded the software right from the ANEX web site under the ADS1115 demo. 

davep...@gmail.com

unread,
Oct 29, 2020, 2:10:29 PM10/29/20
to Annex WiFi RDS
I also noticed that the statement:
2c.ReadRegArray ADS1115_ADDRESS, ADS1115_CONV_REG, 2, BUFF_IN()
reports a syntax error. Something has changed since the web site documentation has been made available I guess?

davep...@gmail.com

unread,
Oct 29, 2020, 2:55:38 PM10/29/20
to Annex WiFi RDS
So, acording to the update 1.4.x the statements:
i2c.WriteRegArray   
I2c.ReadRegArray  
have been eliminated because of redundancy with using IO BUFFERS. However I see NO documentation on using IO BUFFERS. This documentation seems to be somewhat out of date....

PeterN

unread,
Oct 29, 2020, 3:13:57 PM10/29/20
to Annex WiFi RDS

davep...@gmail.com

unread,
Oct 29, 2020, 3:57:43 PM10/29/20
to Annex WiFi RDS
Why Yes, This file is quite enhanced from the version made available on the web site. I'll have to study as I am an OLD guy.... Thanks Peter...

Bugs

unread,
Oct 29, 2020, 6:04:45 PM10/29/20
to Annex WiFi RDS
This was a version of the sample program that I modified a few weeks ago - offered as possible example but NOT fully tested:-

CODE: xxxx.bas

' 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

i2c.setup 4,5 ' set I2C bus

' Set the ADS1115 with :
' AINp = AIN0 and AINn = AIN1
' FSR = ±4.096 V
' 16 SPS

ADS1115_setup 4, 1, 1

' scale in volt
scale = 4.096 / 32768
v = 0
for i = 0 to 100000
ADS1115_read v ' read from the module
WLOG v * 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
iobuff.dim(0,2) = (conf and &hff00) >> 8 , conf and &hff
i2c.write_iobuff(0), ADS1115_ADDRESS, ADS1115_CONF_REG
pause 250
end sub

sub ADS1115_read(ret)

'use the IO Buffer 0 for reading
i2c.read_iobuff(0), ADS1115_ADDRESS, ADS1115_CONV_REG, 2
ret = iobuff.read(0, 0) << 8 + iobuff.read(0, 1)

davep...@gmail.com

unread,
Oct 30, 2020, 9:29:53 AM10/30/20
to Annex WiFi RDS

Bug's, Thank you for steering me right. I will give it a try today. Thanks Peter, as this is just what I was looking for... Once again, Thanks to all...

davep...@gmail.com

unread,
Oct 31, 2020, 11:33:03 AM10/31/20
to Annex WiFi RDS
Well, I have made some headway on this project. I have written a few routines that I hope will help some others. They are for implementation of an LM-73 temperature sensor on the I2C bus. Many thanks to Bug's and Peter for the direction. The program uses ESPNOW to transfer the data.
TEMP TEST.txt

cicciocb

unread,
Nov 1, 2020, 6:32:51 AM11/1/20
to Annex WiFi RDS
Thanks for your contribution.

For information, in the documentation, there is a complete ADS1115  example using io buffers here :

davep...@gmail.com

unread,
Nov 1, 2020, 12:17:22 PM11/1/20
to Annex WiFi RDS
The routine I have written seems to work on it's own but when incorporating it into another program the same code comes up with a "Bad subscript line 138" error.  TEMPERATURE = (iobuff.read(0, 0) << 8) + iobuff.read(0, 1)

davep...@gmail.com

unread,
Nov 1, 2020, 3:53:12 PM11/1/20
to Annex WiFi RDS
Cicciocb, I have taken the code as presented and find there are still error's " Syntax error line 127 " . I have re-flashed the LOLIN Node MCU with ANNEX 1.4.2.3 with NO luck.
On Sunday, November 1, 2020 at 6:32:51 AM UTC-5 cicciocb wrote:

davep...@gmail.com

unread,
Nov 2, 2020, 11:29:22 AM11/2/20
to Annex WiFi RDS
Cicciocb, I have tried loading the same code on an ESP8266-01 as well as an LOLIN v0.1 node MCU module with no luck. I keep getting BAD Subscript error in line 74. Here is the exact program. It is if there is some kind of precedent that needs to be conformed to or is there?

davep...@gmail.com

unread,
Nov 2, 2020, 11:32:08 AM11/2/20
to Annex WiFi RDS
I'm Sorry, I didn't attach the code....

' ADS1115 Driver for Annex
' ADS1115 Registers

ADS1115_ADDRESS = &h49
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
i2c.setup 0,2 ' set I2C bus

' Set the ADS1115 with :
'    AINp = AIN0 and AINn = AIN1
'    FSR = ±2.048 V
'    8 SPS
ADS1115_setup 0, 2, 0

' scale in volt
scale = 2.048 / 32768
v = 0

for i = 0 to 100000
 ADS1115_read v  ' read from the module
 print v * 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
 'use the IO Buffer 0 for TRANSFERS
 iobuff.dim(0,2) =  (conf and &hff00) >> 8 , conf and &hff
 i2c.write_iobuff(0), ADS1115_ADDRESS, ADS1115_CONF_REG
end sub

sub ADS1115_read(ret)
 'use the IO Buffer 0 for reading
 i2c.read_iobuff(0), ADS1115_ADDRESS, ADS1115_CONV_REG, 2
 ret = iobuff.read(0, 0) << 8 + iobuff.read(0, 1)
 if ret > 32768 then ret = ret - 65536
end sub

Bugs

unread,
Nov 2, 2020, 1:56:01 PM11/2/20
to Annex WiFi RDS
Just tried it and got the same error with my ADS1115 module.
The difference (in my case) was the address - mine is &48 not &h49.
ADS1115_ADDRESS = &h48 worked as expected.
Worth running the I2C scanner program in the help file to check.



davep...@gmail.com

unread,
Nov 2, 2020, 2:13:32 PM11/2/20
to Annex WiFi RDS
Well I had to change the address of the ADS1115 so it can reside with an LM-73 which is on the same bus. What I don't understand is, both snippets of code worked just fine the other day and for some reason since yesterday they don't work.

davep...@gmail.com

unread,
Nov 2, 2020, 2:16:20 PM11/2/20
to Annex WiFi RDS
Bug's, What version of ANNEX are you running? I have flashed my modules with ANNEX 1.42.3.

Bugs

unread,
Nov 2, 2020, 2:28:32 PM11/2/20
to Annex WiFi RDS
I use the latest ( 1.42.3) on any bench test modules -  but my permanent boxed modules are left alone with whatever they were programmed with at the time.
I was also able to get the line 74 error by swapping the SDA and SCL pins when wiring the module so it just indicates no comms with the ADS1115.

davep...@gmail.com

unread,
Nov 2, 2020, 4:18:56 PM11/2/20
to Annex WiFi RDS
Well, I'll try playing with the hardware but I don't think that will do as like I said, All of the routines were working before.

cicciocb

unread,
Nov 3, 2020, 2:32:36 AM11/3/20
to annex_w...@googlegroups.com
Helo dave,
as Bugs says, the errors you have means no communication.
Probably the I2C device is not recognised properly.

Try to run a I2C scan snippet to check what are the I2C addresses recognised.

tymone...@gmail.com

unread,
Nov 3, 2020, 3:12:09 AM11/3/20
to Annex WiFi RDS
ADS1115 working code for K type thermocouple


' Annex32 WiFi 1.41 beta 5
' For termocuple type K  0 - 1000 celsius 4 chanel signal to GDN  1000 celsius = 41 mV calibrator 41,22 mV
'table error
 '50  48  100 99  150 149  200 199  250 248  300 299  300  300 350 350  400 402  506  600 611  700 715  800 818  900 917  1000 1014
I2C.SETUP 21, 22 ' sda scl


' 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

' Set the ADS1115 with :

'    AINp = AIN0 and AINn = AIN1

'    FSR = ±4.096 V
                            
'    16 SPS

ADS1115_setup 4 , 6 , 2   '       od  4 do 7       wejscie napiecie próbki                                                     
                     
'FOR Thermocouple typ K

v = 0
do

for  chanel = 4 to 7
ADS1115_setup chanel , 6 , 2
pause 1500
ADS1115_read v 
'REMARKS  for real - ambilet temp
if v > 0 then 'Measment temp < 0
wlog "Temp K", CINT( v/5.2) ,"C" , "chanel" ,chanel
else 
Wlog "temp < 0 + ambilet temp"
end if
next chanel

if chanel = 8 then
chanel = 4
end if
loop
 '
  i2c.begin  ADS1115_ADDRESS
  I2c.write ADS1115_CONF_REG
  I2c.write BUFF_IN(0)
  I2c.write BUFF_IN(1)
  i2c.end  
'
' wlog i2c.end ," if 0  kal  0K"
 '
  i2c.begin  ADS1115_ADDRESS
  I2c.write ADS1115_CONV_REG
  i2c.reqfrom  ADS1115_ADDRESS, 2
  BUFF_IN(0) = i2c.read
  BUFF_IN(1) = i2c.read
  i2c.end 
end sub

 

sub ADS1115_read(ret)

 i2c.begin  ADS1115_ADDRESS
  I2c.write ADS1115_CONV_REG
  i2c.reqfrom  ADS1115_ADDRESS, 2
  BUFF_IN(0) = i2c.read
 BUFF_IN(1) = i2c.read
 i2c.end 
 ret = BUFF_IN(0) << 8 +  BUFF_IN(1)
 if ret > 32768 then ret = ret - 65536
 
end sub


cicciocb

unread,
Nov 3, 2020, 9:21:42 AM11/3/20
to Annex WiFi RDS
Hello Dave, 
I just tested the AD1115 example the help file and it works properly.
So probably the problem comes from your H/W installation.

To tymonelektryk :
Thanks for your code, but I have some questions :

- Where is done the error correction ? (I see the table that is in the comment)
- How do you manage the cold junction compensation?

cicciocb

cicciocb

unread,
Nov 3, 2020, 11:20:46 AM11/3/20
to Annex WiFi RDS
Hi all,
just to close this discussion, I modified a little bit the example in order to show an error in case of missing or wrongly connected module.
This is the code that will be also updated in the next release of the help file :


CODE: ADS1115.bas

' 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
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
 
v = 0
for i = 0 to 100000
 ADS1115_read v  ' read from the module
 print v * scale
next i
 
 'use the IO Buffer 0 for writing
 iobuff.dim(0,2) =  (conf and &hff00) >> 8 , conf and &hff
 i2c.write_iobuff(0), ADS1115_ADDRESS, ADS1115_CONF_REG
end sub
 
sub ADS1115_read(ret)
 'use the IO Buffer 0 for reading
 i2c.read_iobuff(0), ADS1115_ADDRESS, ADS1115_CONV_REG, 2
 if iobuff.len(0) = 0 then
   print "No communication"
   ret = 0
 else
   ret = iobuff.read(0, 0) << 8 + iobuff.read(0, 1)
 end if

tymone...@gmail.com

unread,
Nov 3, 2020, 1:24:17 PM11/3/20
to Annex WiFi RDS
The difference results from the calibrator set value to the measured value
without cold junction compensation
look at the photo   
cold ends  of the  thermocouple  must be compensated  with an additional thermometer,  Ds18B20

cicciocb

unread,
Nov 4, 2020, 3:00:27 AM11/4/20
to Annex WiFi RDS
It looks interesting, it could worth to develop a function for the correction based on a table with linear interpolation.
However, the errors seems below 2% that is not so bad.

One value is missing in your table (the 500 one)
 
Reply all
Reply to author
Forward
0 new messages