Built-in project · Raspberry Pi Pico

MicroPython DHT11 and LCD1602 on a Pico

MicroPython on a Raspberry Pi Pico: a DHT11 on GP5 and an I2C LCD1602 on GP14 and GP15 show the temperature and humidity, refreshed every two seconds. The whole build, a Raspberry Pi Pico 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 $12.05. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Reading a DHT sensor from MicroPython
  • An I2C LCD with a helper module
  • A pull-up on a one-wire data line
  • Catching errors with try and except
  • Difficulty: Intermediate
  • Time: About 25 minutes
  • Board: Raspberry Pi Pico
  • Parts: 7

How it works

A temperature and humidity sensor on GP5, with a 10k pull-up to 3.3 V, is read every two seconds. The code uses the dht.DHT11 class. An LCD1602 with an I2C backpack on bus 1 (SDA GP14, SCL GP15) shows temperature on the first line and humidity on the second, as whole numbers.

The helper file lcd1602.py talks to the backpack at address 0x27. It sends each byte as two 4-bit halves and pads each line to 16 characters.

If a read fails, the screen shows DHT11 error and tries again after two seconds. The program also prints each reading to the console.

Parts list

7 parts, plus the jumper wires. They retail for about $12.05; on Mokxi they're free.

Kits that can build it

This starter kit has 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 pinConnects to
Temperature and humidity, DHT22 pin DATARaspberry Pi Pico pin GP5
Resistor, 10k Ω pin 2Raspberry Pi Pico pin GP5
Character LCD with I2C backpack pin SDARaspberry Pi Pico pin GP14
Character LCD with I2C backpack pin SCLRaspberry Pi Pico pin GP15
Temperature and humidity, DHT22 pin VCC3.3 V
Temperature and humidity, DHT22 pin GNDGround
Resistor, 10k Ω pin 13.3 V
Character LCD with I2C backpack pin GNDGround
Character LCD with I2C backpack pin VCC5 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.

main.py · Raspberry Pi Pico
# The DHT11's readings on the LCD1602: the sensor on GP5, the LCD on I2C
# bus 1 (SDA GP14, SCL GP15), the temperature on the first line and the
# humidity on the second, every two seconds.
from machine import I2C, Pin
import dht
import time
from lcd1602 import LCD1602

sensor = dht.DHT11(Pin(5))
i2c = I2C(1, sda=Pin(14), scl=Pin(15), freq=400000)
lcd = LCD1602(i2c, 0x27)
time.sleep(1)

while True:
    try:
        sensor.measure()
        t, h = sensor.temperature(), sensor.humidity()
        lcd.show("Temp:  %d C" % t, "Humid: %d %%" % h)
        print("temperature", t, "humidity", h)
    except OSError:
        lcd.show("DHT11 error")
    time.sleep(2)
lcd1602.py · Raspberry Pi Pico
# 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 DHT11 error. This project
    Check the data wire on GP5 and the 10k pull-up from the data line to 3.3 V.
  • The LCD is blank. This project
    SDA is GP14 and SCL is GP15. The code expects the backpack at address 0x27 and 5 V power.
  • The numbers are whole numbers only. This project
    The code prints with %d, so there are no decimals.
  • 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.