Datasheet

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:
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:
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%:
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
import board
import adafruit_dht
dht = adafruit_dht.DHT22(board.D6)
dht.temperature
dht.humidity
© Adafruit Industries https://learn.adafruit.com/adafruit-gemma-m0 Page 135 of 186