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.