Adafruit AHT20 with MicroPython on Pico Lipo error

I’ve been trying to get an Adafruit AHT20 sensor working in MicroPython using the library ahtx0.py from

import utime
from machine import Pin, I2C

import ahtx0
sda=machine.Pin(4) # 0
scl=machine.Pin(5) # 1
i2c=machine.I2C(0,sda=sda, scl=scl, freq=400000)
print(i2c.scan())

i2c = I2C(scl=Pin(5), sda=Pin(4), id = 0x38)

# Create the sensor object using I2C
sensor = ahtx0.AHT20(i2c)

while True:
    print("\nTemperature: %0.2f C" % sensor.temperature)
    print("Humidity: %0.2f %%" % sensor.relative_humidity)
    utime.sleep(5)

It finds the sensor, but the program dies on line 10 when trying to create the sensor object with the following error message.

>>> %Run -c $EDITOR_CONTENT
[56]
Traceback (most recent call last):
  File "<stdin>", line 10, in <module>
ValueError: I2C(56) doesn't exist
>>>

Can anyone help?

Do you have other devices on your I2C-bus? The device has rather weak pullups and the builtin pullups of the pico are even weaker. I assume that you use the Stemma/QT connector, so this would rule out a connection issue.

Yes, I’m using the Stemma/QT connector with only the one item on the I2C bus.

Would extra pull-ups on GP4 and GP5 help? (I’m assuming the Stemma/QT pins are connected to the ‘leg’ pins.) If so, what would be a suitable resistor?

Since you only use a single device, I doubt that it is a pullup-issue. BTW: I use a Badger2040 with the AHT20 without extra pullups and that works fine. But I use the CircuitPython library but the MicroPython lib should be just fine. And I think the error isn’t even triggered directly in the library, it triggers when the I2C-object is opened.

So here are some more ideas: use the alternative plug in the AHT20. Use a different cable.

Cracked it!

import utime
from machine import Pin, I2C

import ahtx0

i2c = I2C(id=0,scl=Pin(5), sda=Pin(4))
# Create the sensor object using I2C
sensor = ahtx0.AHT20(i2c)

while True:
    print("\nTemperature: %0.2f C" % sensor.temperature)
    print("Humidity: %0.2f %%" % sensor.relative_humidity)
    utime.sleep(1)

Needs the id value. Scan not needed.

>>> %Run -c $EDITOR_CONTENT
[56]

Temperature: 18.80 C
Humidity: 72.40 %

Temperature: 18.78 C
Humidity: 72.40 %

Temperature: 18.81 C
Humidity: 72.35 %

Temperature: 18.80 C
Humidity: 72.35 %

Hope this helps others.

Just waiting for my Galactic Unicorn to be delivered so I can try it with this sensor.

Nice to see. You actually had the correct id the first time you created the I2C-object, that is why the scan worked.

BTW: that is one of the reasons I prefer CircuitPython. You just use board.I2C() and you don’t have to worry about pins and bus-numbers. And if you decide to change the MCU, your code still works. (To be honest, the original pico is one notable exception since I2C can be on almost any pins, but CircuitPython for Pimoroni’s pico-lipo does define board.I2C()).

No CP for the Pimoroni Galactic Unicorn just MP. I was just testing out the sensor in MP before the new board arrived - which is fantastic.

I get the following:

MPY: soft reboot
Traceback (most recent call last):
  File "<stdin>", line 8, in <module>
  File "/lib/ahtx0.py", line 50, in __init__
  File "/lib/ahtx0.py", line 59, in reset
OSError: [Errno 5] EIO

Using ‘MicroPython v1.24.0 on 2024-10-25; Raspberry Pi Pico with RP2040’. ANy ideas why that might happen? I’m using the same code from @Tonygo2’s post where he says he cracked it.

Try running the I2C scanner to check the connections and that the sensor can be found.

import machine
sda=machine.Pin(20)  # or 4
scl=machine.Pin(21)   # or 5
i2c=machine.I2C(0,sda=sda, scl=scl, freq=400000)

print('Scan i2c bus...')
devices = i2c.scan()

if len(devices) == 0:
    print("No i2c device !")
else:
    print('i2c devices found:',len(devices))

for device in devices:
    print("Decimal address: ",device," | Hexa address: ",hex(device))

To check you have the sda and scl correct.

I’ve just run the cracked it program and library on a Pimoroni Explorer with latest .uf2 and it works.

I think I had a wiring issue. i2c.scan wasn’t finding anything. Unhooked everything and hooked it back up and it works. For some reason, the error message made me believe it was a library problem rather than a wiring problem. Thanks for looking.

Glad you have it working now. The problem with Picos is that we have too many choices of pin pairs for I2C connections where most other microprocessor boards limit the sda and scl pins available.

Hm… not really. Most Espressif chips (and that is a huge share of the market) have full peripheral multiplexing like the Pico. And STM32 also has multiplexing.

What we do have: some form-factors like Feather always put the same functions on the same pins, regardless of the chip. So a RP2040 Feather will have the same pinout as an ESP32-S3 Feather. This is definitely less confusing and you usually also find labels on the boards which also helps. And programs are more portable.