Using the AHT10 with the Raspberry Pi

Using the AHT10 with the Raspberry Pi

The AHT10 is a temperature and humidity sensor that communicates using the I2C protocol. It provides accurate readings, making it ideal for environmental monitoring, IoT projects, and weather stations. This guide explains how to set up and use the AHT10 sensor with a Raspberry Pi to measure temperature and humidity.


What You Will Need

  1. Raspberry Pi (any model with GPIO support, e.g., Pi 3, Pi 4)
  2. AHT10 Sensor Module
  3. Breadboard and Jumper Wires
  4. Python installed on your Raspberry Pi

Step 1: Wiring the AHT10 to the Raspberry Pi

The AHT10 uses the I2C protocol for communication.

Connections (I2C Mode)

AHT10 Pin Raspberry Pi Pin
VCC 3.3V (Pin 1)
GND Ground (Pin 6)
SDA SDA (Pin 3, GPIO2)
SCL SCL (Pin 5, GPIO3)

Step 2: Enable the I2C Interface

  1. Open the Raspberry Pi configuration tool:
    sudo raspi-config
    
  2. Navigate to Interface Options > I2C and enable it.
  3. Reboot the Raspberry Pi:
    sudo reboot
    

Step 3: Install Required Libraries

  1. Update your Raspberry Pi’s package list:
    sudo apt update && sudo apt upgrade -y
    
  2. Install the necessary tools and Python libraries:
    sudo apt install -y i2c-tools python3-pip
    pip3 install adafruit-circuitpython-ahtx0
    
  3. Verify the AHT10 is detected on the I2C bus:
    sudo i2cdetect -y 1
    
    You should see the device address (typically 0x38) in the output.

Step 4: Python Code to Read Data from the AHT10

Use the following Python script to read temperature and humidity data from the AHT10 sensor.

Python Code Example

import time
import board
import adafruit_ahtx0

# Initialize I2C and AHT10 sensor
i2c = board.I2C()
sensor = adafruit_ahtx0.AHTx0(i2c)

try:
    while True:
        temperature = sensor.temperature  # Read temperature in Celsius
        humidity = sensor.relative_humidity  # Read humidity in percentage

        print(f"Temperature: {temperature:.2f} °C")
        print(f"Humidity: {humidity:.2f} %")
        print("------------------------")

        time.sleep(2)  # Wait 2 seconds before the next reading
except KeyboardInterrupt:
    print("Exiting...")

Step 5: Applications of the AHT10

  1. Weather Monitoring: Use the AHT10 to collect real-time temperature and humidity data.
  2. Smart Home Projects: Monitor indoor climate for automation systems like HVAC or humidifiers.
  3. Environmental Research: Measure conditions in greenhouses, gardens, or outdoor setups.
  4. IoT Devices: Integrate the sensor with cloud platforms for remote data logging and visualization.

Troubleshooting

  1. Device Not Detected:

    • Check the wiring of the SDA and SCL pins.
    • Ensure the I2C interface is enabled on the Raspberry Pi.
  2. Inaccurate Readings:

    • Avoid placing the sensor near heat sources or areas with inconsistent airflow.
    • Verify the sensor is operating within its specified range.
  3. I2C Errors:

    • Ensure no other devices are conflicting with the AHT10’s I2C address.

Conclusion

The AHT10 sensor is a reliable and easy-to-use device for measuring temperature and humidity. By following this guide, you can integrate the AHT10 with your Raspberry Pi to build weather stations, monitor indoor climates, or add environmental sensing capabilities to your IoT projects. Experiment with different setups to explore its full potential!

Leave a comment

Notice an Issue? Have a Suggestion?
If you encounter a problem or have an idea for a new feature, let us know! Report a problem or request a feature here.