MicroPython DHT readings on an I2C LCD, on an ESP32-DevKitC V4
MicroPython on an ESP32: a DHT22 on GPIO 18 shows temperature and humidity on an I2C LCD1602 (SDA 13, SCL 14), every two seconds. The whole build, an ESP32-DevKitC V4 and 3 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.
Intermediate About 25 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $18.05. On Mokxi they're free.
What you'll learn
- Reading a DHT temperature and humidity sensor
- An I2C LCD driven by a small helper module
- A pull-up resistor on a data line
- Catching errors with try and except
- Difficulty: Intermediate
- Time: About 25 minutes
- Board: ESP32-DevKitC V4
- Parts: 7
How it works
A DHT22 sensor on GPIO 18, with a 10k pull-up to 3.3 V, measures temperature and humidity. An LCD1602 with an I2C backpack is on SDA 13 and SCL 14. Every two seconds the program measures, then shows temperature on the top line and humidity on the bottom, with one decimal each. It also prints them.
The helper file lcd1602.py talks to the backpack chip at address 0x27 and sends each byte as two 4-bit halves. It pads each line to 16 characters so old text is cleared.
If a reading fails, the program shows Sensor error and tries again two seconds later. The comment notes a kit DHT11 would use dht.DHT11 instead.
Parts list
7 parts, plus the jumper wires. They retail for about $18.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.
- 1 × Character LCD with I2C backpackIn Claw Machine Kit, Vending Machine Kit, Rhythm Game Kit.
- 2 × Power, 3.3 V
- 1 × Power, 5 V
Kits that can build it
These 2 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 |
|---|---|
| Character LCD with I2C backpack pin SCL | ESP32-DevKitC V4 pin 14 |
| Character LCD with I2C backpack pin SDA | ESP32-DevKitC V4 pin 13 |
| Temperature and humidity, DHT22 pin DATA | ESP32-DevKitC V4 pin 18 |
| Resistor, 10k Ω pin 2 | ESP32-DevKitC V4 pin 18 |
| Temperature and humidity, DHT22 pin VCC | 3.3 V |
| Temperature and humidity, DHT22 pin GND | Ground |
| Resistor, 10k Ω pin 1 | 3.3 V |
| Character LCD with I2C backpack pin GND | Ground |
| Character LCD with I2C backpack pin VCC | 5 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 on an LCD: the DHT sensor on GPIO 18, the LCD1602 on
# I2C (SDA 13, SCL 14). Temperature on the top line, humidity below. The
# sensor here is a DHT22; with the kit's DHT11, write dht.DHT11.
from machine import I2C, Pin
from lcd1602 import LCD1602
import dht
import time
sensor = dht.DHT22(Pin(18))
lcd = LCD1602(I2C(0, sda=Pin(13), scl=Pin(14), freq=400000))
while True:
try:
sensor.measure()
t, h = sensor.temperature(), sensor.humidity()
lcd.show("Temp: %.1f C" % t, "Humi: %.1f %%" % h)
print("temp %.1f humi %.1f" % (t, h))
except OSError:
lcd.show("Sensor error")
time.sleep(2)
# lcd1602.py: a 16x2 character LCD on a PCF8574 I2C backpack.
# The backpack's P0 is RS, P2 is E, P3 is the backlight and P4 to P7 are
# the display's D4 to D7, so every byte is sent as two 4-bit halves.
import time
class LCD1602:
def __init__(self, i2c, addr=0x27):
self.i2c = i2c
self.addr = addr
self.light = 0x08
time.sleep_ms(50)
for half in (0x30, 0x30, 0x30, 0x20): # wake up, then 4-bit mode
self._half(half, 0)
time.sleep_ms(5)
self.command(0x28) # two lines, 5x8 dots
self.command(0x0C) # display on, no cursor
self.command(0x06) # move right after a character
self.clear()
def _send(self, bits):
self.i2c.writeto(self.addr, bytes([bits | self.light]))
def _half(self, bits, rs):
self._send(bits | rs | 0x04) # E high...
self._send(bits | rs) # ...and low: the LCD reads it
def _byte(self, value, rs):
self._half(value & 0xF0, rs)
self._half((value << 4) & 0xF0, rs)
def command(self, value):
self._byte(value, 0)
if value < 4:
time.sleep_ms(2) # clear and home are slow
def clear(self):
self.command(0x01)
def move_to(self, col, row):
self.command(0x80 | (col + 0x40 * row))
def write(self, text):
for ch in text:
self._byte(ord(ch), 1)
def show(self, line1, line2=""):
self.move_to(0, 0)
self.write("%-16s" % line1[:16])
self.move_to(0, 1)
self.write("%-16s" % line2[:16])
Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The screen says Sensor error. This project
Check the data wire on GPIO 18 and the 10k pull-up to 3.3 V. If you use a DHT11, change dht.DHT22 to dht.DHT11. - The LCD shows nothing. This project
SDA goes to GPIO 13 and SCL to GPIO 14, with the LCD on 5 V. Do not swap them. - The values look odd. This project
The sensor class must match your part, DHT22 or DHT11. - 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 screen stays blank or shows only a row of blocks. Character LCD with I2C backpack
The I2C address is usually 0x27 or 0x3F. Run an I2C scanner to find yours. Then turn the contrast trimmer on the backpack slowly until text appears.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.