Source: Using Devices — Connected Objects

Note: This raw file is marked TODO/incomplete by the teacher.

AHT20 Temperature & Humidity Sensor Libraries

Two options for using the AHT20 I2C sensor:

Option 1: grove.py (Seeed library)

git clone https://github.com/Seeed-Studio/grove.py
cd grove.py
sudo pip3 install .

The main module: grove/grove_temperature_humidity_aht20.py

Note: Use step-by-step installation (not one-click) or it installs to wrong location.

Option 2: Adafruit libraries

pip install adafruit-circuitpython-ahtx0
pip install adafruit-extended-bus
  • adafruit-circuitpython-ahtx0 — sensor class
  • adafruit-extended-bus — provides I2C bus instance needed to instantiate sensor

Option 3: smbus2 direct (PREFERRED — grove.py is broken)

From A1: Seeed-grove.py library is no longer recommended — it does not work.

Critical: reTerminal uses I2C bus 4, NOT bus 1.

from smbus2 import SMBus
from time import sleep
 
class AHT20:
    def __init__(self, address=0x38, bus=4):   # bus=4 on reTerminal!
        self.address = address
        self.bus = SMBus(bus)
        self.bus.write_i2c_block_data(self.address, 0xBE, [0x08, 0x00])
        sleep(0.02)
 
    def read(self):
        self.bus.write_i2c_block_data(self.address, 0xAC, [0x33, 0x00])
        sleep(0.08)
        data = self.bus.read_i2c_block_data(self.address, 0x00, 7)
 
        humidity = ((data[1] << 16) | (data[2] << 8) | data[3]) >> 4
        humidity = humidity * 100 / 1048576.0
 
        temperature = ((data[3] & 0x0F) << 16) | (data[4] << 8) | data[5]
        temperature = temperature * 200 / 1048576.0 - 50
 
        return temperature, humidity   # (°C, %RH)
 
# Use one instance shared across sensor classes
aht20 = AHT20(bus=4)
temp, humi = aht20.read()

Install: pip install smbus2 (or uv add smbus2)

See Also