MicroPython DHT sensor: temperature and humidity, on an ESP32-DevKitC V4
MicroPython on an ESP32: the dht module reads a DHT22 on GPIO 13, with a 10k pull-up, and prints temperature and humidity every two seconds. The whole build, an ESP32-DevKitC V4 and 2 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Beginner About 15 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $14.05. On Mokxi they're free.
What you'll learn
- Reading a DHT sensor from MicroPython
- Pull-up resistor on a data line
- Catching errors with try and except
- Formatting numbers in printed text
- Difficulty: Beginner
- Time: About 15 minutes
- Board: ESP32-DevKitC V4
- Parts: 5
How it works
The sensor's DATA pin goes to GPIO 13, with a 10k resistor pulling it up to 3.3 V. It also gets 3.3 V and ground. The program asks for a reading every two seconds. The sensor needs that long to be ready again.
Each time, the program tells the sensor to measure, then prints the temperature and humidity with one decimal place. If the sensor does not answer, the code catches the error and prints a message instead of stopping. The part here is a DHT22. A DHT11 is wired the same way, and the code comment says how to switch.
Parts list
5 parts, plus the jumper wires. They retail for about $14.05; on Mokxi they're free.
- 1 × ESP32-DevKitC V4
- 1 × Temperature and humidity, DHT22
- 1 × Resistor, 10k ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 2 × Power, 3.3 V
Kits that can build it
These 3 starter kits have every part in the circuit. Each kit's page lists what else it can build.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Temperature and humidity, DHT22 pin DATA | ESP32-DevKitC V4 pin 13 |
| Resistor, 10k Ω pin 2 | ESP32-DevKitC V4 pin 13 |
| Temperature and humidity, DHT22 pin VCC | 3.3 V |
| Temperature and humidity, DHT22 pin GND | Ground |
| Resistor, 10k Ω pin 1 | 3.3 V |
The code
The MicroPython code exactly as the editor opens it. Change a line there and press Run: the board runs it on the official MicroPython firmware, with nothing to compile.
# Hygrothermograph: a DHT sensor on GPIO 13 with a 10k pull-up. Ask it for
# a reading every two seconds, as fast as it can answer. The kit's sensor
# is a DHT11; this one is a DHT22, which is wired the same way and gives
# tenths of a degree. With a DHT11, write dht.DHT11 instead.
from machine import Pin
import dht
import time
sensor = dht.DHT22(Pin(13))
while True:
try:
sensor.measure()
print("Temperature: %.1f C Humidity: %.1f %%" % (sensor.temperature(), sensor.humidity()))
except OSError:
print("No answer from the sensor")
time.sleep(2)
Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- "No answer from the sensor" prints every time This project
Check that DATA goes to GPIO 13, that the 10k resistor ties DATA to 3.3 V, and that VCC and GND are connected. - Readings look coarse or wrong for your sensor This project
The code uses the DHT22 class. If your real part is a DHT11, change the class in the code to match. - Readings are NaN or fail, or never appear. Temperature and humidity
Put a 10 k ohm pull-up between DATA and VCC (bare sensors need it). A 4-pin DHT22 has pin 3 unused. Check the data pin number in your code. - Values freeze or errors show up when you read often. Temperature and humidity
The DHT22 can only be read about once every 2 seconds. Wait between reads, and check for NaN before using the numbers. - The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure. - A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground. - The board is not found, or upload fails with a connecting message. ESP32-DevKitC V4
Use a USB cable that carries data, not charge only. Pick the ESP32 Dev Module board. If it still fails, hold BOOT while uploading starts, then release. - Some pins never read right, or the board will not boot with a wire attached. ESP32-DevKitC V4
GPIO34 to 39 are input only and have no pull-ups. GPIO6 to 11 run the flash, so leave them alone. GPIO0, 2 and 12 are boot pins; avoid pulling them the wrong way at reset.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.