Adding I2C IIC Analog to Digital ADC PGA Converter

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
soggycashew
Posts: 55
Joined: Sat Sep 18, 2021 10:21 pm

Adding I2C IIC Analog to Digital ADC PGA Converter

Post by soggycashew » Sat Jan 01, 2022 8:12 pm

Hello, im new to coding and micropython and the Pico only has 3 ADC pins and I need more for these Capacitive Soil Moisture Sensors I purchased. I also purchased these ADS1115 I2C IIC Analog-to-Digital ADC PGA Converters to add the extra ADC pins for more of the moisture sensors.

The code below works if I use the Pics's onboard ADC pins but what do I add to the code to get my ADC board to work with its 4 pins as well? I know how to connect it to the i2c on the Pico I just am lost from there. Thanks!

Code: Select all

from machine import ADC
import utime
########################################################################
Define GPIO Pins
adc0 = machine.ADC(machine.Pin(26))
adc1 = machine.ADC(machine.Pin(27))
adc2 = machine.ADC(machine.Pin(28))
########################################################################
Define Variables
wet = 28882 
dry = 56263
########################################################################
Define Functions
def percentage_value(value, in_min, out_max, out_min): 
    percentage_value = (value - in_min) / (out_max - out_min) * 100 
    return (str(round(percentage_value))) + "%"
########################################################################
while True:
print(percentage_value(adc0.read_u16(), dry, wet, dry))
utime.sleep(0.5)

davef
Posts: 813
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: Adding I2C IIC Analog to Digital ADC PGA Converter

Post by davef » Sat Jan 01, 2022 8:56 pm

Three links to ADS1115 libraries here:
https://awesome-micropython.com/

soggycashew
Posts: 55
Joined: Sat Sep 18, 2021 10:21 pm

Re: Adding I2C IIC Analog to Digital ADC PGA Converter

Post by soggycashew » Mon Jan 03, 2022 1:29 am

Ok, I'm not getting this at all. I installed the ad1x15.py onto my Pico and created the below example to try and read one capacitive soil sensor on the ADS1115 boards A0 pin. When I run it I get a number 32767 in a loop and never changes. Now when I didn't have the ADS1115 board and used the (Code in post #1) Picos ADC pin 0 with the capacitive soil sensor and printed using print(adc0.read_u16()) I got a reading that moved when I put in water or my hand etc. How do I get it to do that using the ADS1115 board?

Code: Select all

import machine
from machine import I2C
import utime 
from ads1x15 import ADS1115

address = 72
gain = 5

i2c = I2C(0, sda=machine.Pin(0), scl=machine.Pin(1), freq=400000)

ads4 = ADS1115(i2c, address, gain)
# ads5 = ADS1115(i2c, address, gain)
# ads6 = ADS1115(i2c, address, gain)
# ads7 = ADS1115(i2c, address, gain)

ads4_val = ads4.read()

while True:
     
    print(ads4_val)
    utime.sleep(0.5)

davef
Posts: 813
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: Adding I2C IIC Analog to Digital ADC PGA Converter

Post by davef » Mon Jan 03, 2022 3:37 am

When I used a ADS1015 breakout board on a RaspberryPi I had 10K pullups off SDA and SCH to 3V3.

soggycashew
Posts: 55
Joined: Sat Sep 18, 2021 10:21 pm

Re: Adding I2C IIC Analog to Digital ADC PGA Converter

Post by soggycashew » Mon Jan 03, 2022 10:34 am

Im a self learning beginner so i dont have any 10K Ohm 1 Watt Resistors so I ordered some. Im not getting your response though, (10K pullups off SDA and SCH to 3V3). The way i have it wired is SDA to SDA, SCH to SCH and JDD to 3v3. So are you saying run extra wires from the two SDa and SCH to the 3v3 with the 10k between.

User avatar
Roberthh
Posts: 3668
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Adding I2C IIC Analog to Digital ADC PGA Converter

Post by Roberthh » Mon Jan 03, 2022 11:54 am

Did you wire GND to GND as well?

soggycashew
Posts: 55
Joined: Sat Sep 18, 2021 10:21 pm

Re: Adding I2C IIC Analog to Digital ADC PGA Converter

Post by soggycashew » Mon Jan 03, 2022 4:45 pm

Yes sorry forgot to write that one...

rkompass
Posts: 66
Joined: Fri Sep 17, 2021 8:25 pm

Re: Adding I2C IIC Analog to Digital ADC PGA Converter

Post by rkompass » Mon Jan 03, 2022 5:25 pm

Yes, you have to wire these pull-up resistors from SDA and SCL (=SCH?) to 3.3V. The different devices (e.g. many ADS1115s) on the two I2C wires cannot produce a short-circuit, even if they try to send data simultaneously, because every device just connects the wire to ground if it tries to output a 0, and does not connect, if it tries to output a 1.
So if many devices respond simultaneously, all bits which are connected to 0 by any device become 0 in the end. Without short-circuit.
The pull-up resisitors make sure that there appears a logical 1 if no device connects the wire to ground.

But even if it is not an electrical problem of short-circut anymore, we don't want different devices to respond simultaneously on the same SDA/SCL lines because their data become mingled up in the way described above.
Therefore there exist I2C addresses. Every device has to have a unique address and it should know of this address.
In the ADS1115 datasheet www.ti.com/lit/ds/symlink/ads1115.pdf on page 34 you see how to tell any of the four ADS1115s which address to have (The pull-up resistors are also shown in the schematics). This happens by connecting the ADDR pin of the ADS1115 to either GND, VDD (3.3 V), SCL or SDA. After that your ADS1115s have addresses 0b1001000 (=72 =0x48), 0b1001001 (=73 =0x49), 0b1001010 (=74 =0x4A), and 0b1001011 (=75 =0x4B).
Now you can test if the four devices respond - each one separately - to your request to show up.
This is done by scanning the IIC bus:

Code: Select all

i2c2 = I2C(2, freq=100000)  # or I2C(2, scl=B10, sda=B9, freq=100000)
i2clist = i2c2.scan()
for z in i2clist:
    print('Found Contoller on I2C-Bus 2 at:   {:s}'.format(hex(z)))
You should see four lines printed with the controller address shown in hex.

If you have come so far, you can instantiate four adc objects like this:

Code: Select all

adc0 = ADS1115(i2c2, 0x48, 1)  # Gain = 1, 32768 ^⁼ 4.096 v
adc1 = ADS1115(i2c2, 0x49, 1)  # Gain = 1, 32768 ^⁼ 4.096 v
adc2 = ADS1115(i2c2, 0x4A, 1)  # Gain = 1, 32768 ^⁼ 4.096 v
adc3 = ADS1115(i2c2, 0x4B, 1)  # Gain = 1, 32768 ^⁼ 4.096 v
Now you should be able to read all 4 channels of each of the four adcs like:

Code: Select all

while True:
	print('adc0  A0: {:7.6f}V \n'.format(adc0.raw_to_v(adc0.read(0, 0))))
	print('adc0  A1: {:7.6f}V \n'.format(adc0.raw_to_v(adc0.read(0, 1))))
	print('adc0  A2: {:7.6f}V \n'.format(adc0.raw_to_v(adc0.read(0, 2))))
	print('adc0  A3: {:7.6f}V \n'.format(adc0.raw_to_v(adc0.read(0, 3))))
	      - - -    more lines to come here    - - -
	print('adc3  A3: {:7.6f}V \n'.format(adc3.raw_to_v(adc3.read(0, 3))))
Cheers

soggycashew
Posts: 55
Joined: Sat Sep 18, 2021 10:21 pm

Re: Adding I2C IIC Analog to Digital ADC PGA Converter

Post by soggycashew » Tue Jan 04, 2022 3:51 pm

I ordered the resistors ill give it another go when they come in and get back to you! I went off the Texas Instruments PDF ADS1115 you linked for wireing.

soggycashew
Posts: 55
Joined: Sat Sep 18, 2021 10:21 pm

Re: Adding I2C IIC Analog to Digital ADC PGA Converter

Post by soggycashew » Fri Jan 07, 2022 12:51 am

Thank you so much it works perfectly!!! One thing though this is for moisture sensors and I need to go from 0% to 100%. What I just use read () instead of voltage?

Post Reply