Gambas + PIGPIO + I2C + ADS1115 A/D Chip

Ask about the individual Gambas components here.
User avatar
Askjerry
Posts: 64
Joined: Saturday 18th August 2018 9:06pm
Location: Kyle, Texas, USA
Contact:

Re: Gambas + PIGPIO + I2C + ADS1115 A/D Chip

Post by Askjerry »

BriggTrim ->

No, there is no documentation on how to use this chip with Gambas and PIGPIO. Once I figure it out, or someone else does and tells me how... I will post the results and code here.

Jerry
User avatar
Askjerry
Posts: 64
Joined: Saturday 18th August 2018 9:06pm
Location: Kyle, Texas, USA
Contact:

Re: Gambas + PIGPIO + I2C + ADS1115 A/D Chip

Post by Askjerry »

Still no answer here on reading the ADS1115...

I realize that the PIGPIO can also be used with C and other languages but the calls will remain the same. Does anyone here have code in another language that reads the ADS1115 using PIGPIO? If so, please post your code... I may be able to use what you have to successfully read the chip from within GAMBAS. If not... I may be able to call a routine in another language if needed.

Thanks,
Jerry
User avatar
Askjerry
Posts: 64
Joined: Saturday 18th August 2018 9:06pm
Location: Kyle, Texas, USA
Contact:

Re: Gambas + PIGPIO + I2C + ADS1115 A/D Chip

Post by Askjerry »

Still no reply... wow... I really want to get this project moving.
It looks like my option is to program an Atmel AVR in BASCOM and then read it via SPI... that is sort of Rube Goldberg... and I didn't want to interface one chip with another... because my subscribers are likely not going to have BASCOM, and they will end up using AVRDUDE to program one chip to read another... uggg.

Python has Adifruit routines that work... is it possible to make a call to PYTHON???

I remember in the old TRS-80 days... yeah, I'm freaking old... we could write a BASIC program and put an ASM call to pull an assembly language routine in. Is there a way to pull in the Adifruit and/or Python routine?

I REALLY need to read this ADS1115 chip.

Jerry
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Gambas + PIGPIO + I2C + ADS1115 A/D Chip

Post by cogier »

I REALLY need to read this ADS1115 chip.
I am not really qualified to answer this, as I don't even know what a sADS1115 chip is. However, my thoughts are that any library for Python is probably written in C or C#. If this is the case, then Gambas should be able to access it. The Gambas help for this is here.
User avatar
Askjerry
Posts: 64
Joined: Saturday 18th August 2018 9:06pm
Location: Kyle, Texas, USA
Contact:

Re: Gambas + PIGPIO + I2C + ADS1115 A/D Chip

Post by Askjerry »

With the help of Guido Lutterbach I am about 85% able to get this chip to work.
He sent me a python code which works and uses no fancy library to function.
http://smartypies.com/projects/ads1115- ... 115runner/
I was able to understand enough of it that I am now getting changing values when I turn the potentiometer. :D

There is something called Big Endian and Little Endian which basically means that for some reason if you want to send ABCD sometimes you need to send CDAB because of how the Raspberry Pi handles things internally.
(It makes no sense to me why this happens... but it does.)

To accomplish this feat, he uses this line of code in Python... value = (value>>8 | value<<8) &=0xFFFF

I found this reference... but I don't understand what it is trying to tell me.
https://gambaswiki.org/wiki/comp/gb/stream/byteorder

Can someone here tell me what I need to do to accomplish this in Gambas?
(Translate the above into a command or short routine in Gambas.)

He is also doing some sort of 2's complement function to take the returned value and make it into a readable decimal... those are...

LEtoBE

Code: Select all

def LEtoBE(c):
  c = swap2Bytes(c)
    if(c >= 2**15):
      c= c-2**16
  return c 
BEtoLE

Code: Select all

def BEtoLE(c):
  if(c < 0):
    c= 2**16 + c
  return swap2Bytes(c)
Eventually I will learn enough python to fully understand this... but for the moment... if someone can translate... that would be freaking awesome.

Guido breaks it all down here... I'm just slowly getting it piece by painful piece.
http://smartypies.com/projects/ads1115- ... nd-python/

I did learn that I was using the wrong method to send the configurations... not realizing that the i2cWriteWordData( x , y , z) was NOT asking for the chip address again... I feel so silly now... but instead asking for:

i2cWriteWordData( Currently Open Bus , REGISTER TO WRITE , WORD TO GET WRITTEN)

So for now, I am going through the datasheet... writing down the BITS to make the CONFIGURATION, then manually swapping the BYTES and putting it into the code.

The numbers coming out are changing... and sometimes gibberish... but at least I know now that I need to post-process before dumping them into a TEXTBOX.

I think I found at least a partial answer... a command called CShort (Expr AS Variant) getting closer at least... then eventually I'll replace this thread with an exact step-by-step to help out anyone else from going through this!

Thanks in advance!
Jerry
User avatar
Askjerry
Posts: 64
Joined: Saturday 18th August 2018 9:06pm
Location: Kyle, Texas, USA
Contact:

Re: Gambas + PIGPIO + I2C + ADS1115 A/D Chip

Post by Askjerry »

AFTER 5 MONTHS I GOT IT!!!!

Code: Select all

Public Sub BTN_POLL_ADS_P1_Click()
	intReply = i2cOpen(1, DEVICE_ADDRESS, 0) 
	If intReply = 0 Then DSP_01.text = "OPEN" Else DSP_01.text = "ERROR"
'-----------------------------------------------------------------------------
' For all word values below, they need to be byteswapped due to Big Endian
' Example: &HFF7F (Low Threshold) needs to be sent as &H7FFF& below.
'-----------------------------------------------------------------------------
' Reset the ADS1115
	intReply = i2cWriteByteData(0, RESET_ADDRESS, RESET_COMMAND)
' Set Low Threshold
	intReply = i2cWriteWordData(0, POINTER_LOW_THRESHOLD, &H7FFF&)
' Set High Threshold
	intReply = i2cWriteWordData(0, POINTER_HIGH_THRESHOLD, &H8000&)
' Specify the configuration and start conversion
	intReply = i2cWriteWordData(0, POINTER_CONFIGURATION, &H8383&)
'-----------------------------------------------------------------------------
	Wait 0.020
'-----------------------------------------------------------------------------
' Pull the raw data from the ADS1115 in native format
	ADS_RAW = i2cReadWordData(0, POINTER_CONVERSION)
'-----------------------------------------------------------------------------
'Swap 2 Bytes
	ADS_FIXED = (Lsr(ads_raw, 8) Or Lsl(ADS_RAW, 8)) And &Hffff&
'-----------------------------------------------------------------------------
' LE to BE
	If ADS_fixed >= 2 ^ 15 Then 
  		ADS_FIXED = ADS_fixed - (2 ^ 16)
	Endif
'-----------------------------------------------------------------------------
' Display the corrected data into the textbox.
	DSP_03.text = ADS_FIXED
'-----------------------------------------------------------------------------
' Close the process so we can read/write to another device.
	intReply = i2cClose(0)
	If intReply = 0 Then DSP_02.text = "CLOSED" Else DSP_02.text = "ERROR"
End
On a 3.3v setup I am getting readings from 2 to 26,500 which means I'm getting somewhere around 0.000125v accuracy. (Theoretical)
Probably way more than I need to read a wind gauge... but hey... IT WORKS!!!!

WHOOO!
User avatar
Askjerry
Posts: 64
Joined: Saturday 18th August 2018 9:06pm
Location: Kyle, Texas, USA
Contact:

Re: Gambas + PIGPIO + I2C + ADS1115 A/D Chip

Post by Askjerry »

I wanted to do an update... calculating the HEX numbers to send to the chip has been a pain in the posterior... so I made a helpful spreadsheet to figure them out... it does all the work for you.

For an example, I wanted to configure the chip to use Port 0 to Port 1 in differential mode, then connected Port 1 to ground, and Port 0 to the center tap of a potentiometer... the result was &h8383 as shown in the spreadsheet. You simply fill out the multiple questions in the lower left based on the chart... and it provides the programming number for the configuration. :D
ADS1115 Programming Table.xlsx
Spreadsheet to calculate values
(15.76 KiB) Downloaded 123 times
ADS1115 Programming Table.pdf
PDF for printing as a desk reference
(119.47 KiB) Downloaded 134 times
Then I created a simple demo program with comments to get you started.
Let me know if it is helpful to you!
ADS1115-TEST-JULY-2023.zip
Sample GAMBAS QT file
(25.49 KiB) Downloaded 138 times
Unzip the above and copy the folder to wherever you keep your GAMBAS projects.


Thanks,
Jerry
Post Reply