Adafruit CircuitPython Module Install
To use the DHT sensor with your Adafruit CircuitPython board you'll need to install the Adafruit_CircuitPython_DHT module on your board.
First make sure you are running the latest version of Adafruit CircuitPython for your board.
Next you'll need to install the necessary libraries to use the hardware--carefully follow the steps to find and install these libraries from Adafruit's CircuitPython library bundle. Our introduction guide has a great page on how to install the library bundle for both express and non-express boards.
Remember for non-express boards like the, you'll need to manually install the necessary libraries from the bundle:
- adafruit_dht.mpy
You can also download the adafruit_dht.mpy from its releases page on Github.
Before continuing make sure your board's lib folder or root filesystem has the adafruit_dht.mpy module copied over.
Wiring
DHT wiring is very simple:
- The left-most pin is power. We recommend powering from 5V (sometimes 3V is not enough) - this is OK even if you are using 3.3V logic
- The second pin is data. Connect a 10K pullup resistor from this pin to 3.3V. If you are using a DHT11 it's required. If you're using a DHT22 or AM2302 you can sometimes leave this off
- Skip the third pin
- The right-most pin is ground
Here's an example using a Trinket M0 - you can use any CircuitPython board, just check that the Data pin is pulseio
-capable.
Usage
To demonstrate the usage of the DHT sensor module you can connect to your board's serial REPL and run Python code to read the temperature and humidity.
Next connect to the board's serial REPL so you are at the CircuitPython >>> prompt.
Next import the board and adafruit_dht modules, these are necessary modules to initialize and access the sensor:
import board import adafruit_dht
You may also want to try powering the DHT sensor from 5V (we found sometimes it really needs more power) but still having the 10K pull-up resistor to 3.3V volts)
Now create an instance of either the DHT11 or DHT22 class, depending on the type of sensor you're using (for the AM2302 sensor use the DHT22 class). You must pass in the pin which is connected to the signal line, for example a DHT22 or AM2302 sensor connected to board pin D6
would need this code:
dht = adafruit_dht.DHT22(board.D6)
Note for a DHT11 sensor you'd instead use adafruit_dht.DHT11 in place of the adafruit_dht.DHT22 code above.
At this point you're all set and ready to start reading the temperature and humidity! You can do this by reading the temperature property which returns temperature in degrees Celsius:
dht.temperature
To read the humidity grab the value of the humidity property, it will return the percent humidity as a floating point value from 0 to 100%:
dht.humidity
In most cases you'll always get back a temperature or humidity value when requested, but sometimes if there's electrical noise or the signal was interrupted in some way you might see an exception thrown to try again. It's normal for these sensors to sometimes be hard to read and you might need to make your code retry a few times if it fails to read. However if you always get errors and can't ever read the sensor then double check your wiring (don't forget the pull-up resistor if needed!) and the power to the device.
Example Code
Here's a full example sketch which also manages error-retry logic (which will happen once in a while.
Don't forget to change the logic pin to whatever pin you're using! Then save this as main.py
on your CircuitPython board
# SPDX-FileCopyrightText: 2017 Limor Fried for Adafruit Industries # # SPDX-License-Identifier: MIT import time import adafruit_dht import board dht = adafruit_dht.DHT22(board.D2) while True: try: temperature = dht.temperature humidity = dht.humidity # Print what we got to the REPL print("Temp: {:.1f} *C \t Humidity: {}%".format(temperature, humidity)) except RuntimeError as e: # Reading doesn't always work! Just print error and we'll try again print("Reading from DHT failure: ", e.args) time.sleep(1)
If you are using a DHT11, change the code to use a adafruit_dht.DHT11(board.D2)
object.
Open the REPL to see the output! Breathe on the sensor to see it move temperature and humidity up (unless you are a White Walker in which case the temperature will go down)
Page last edited January 22, 2025
Text editor powered by tinymce.