Raspberry Pi - DHT11 - LCD

This tutorial instructs you how to use Raspberry Pi to read temperature and humidity DHT11 from sensor and display them on LCD I2C.

Hardware Preparation

1×Raspberry Pi 4 Model B
1×LCD I2C
1×DHT11 Temperature and Humidity Sensor
1×DHT11 Temperature Humidity Sensor Module (short)
1×Jumper Wires
1×(Optional) Screw Terminal Adapter for Raspberry Pi
1×(Optional) Power Adapter for Raspberry Pi 4B
1×(Optional) Plastic Case for Raspberry Pi 4B

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of DHT11 and LCD

If you are unfamiliar with the DHT11 temperature humidity sensor and LCD (including pinout, how it works, and how to program), the following tutorials can help you learn:

Wiring Diagram

The wiring diagram between Raspberry Pi and DHT11 temperature and humidity LCD

This image is created using Fritzing. Click to enlarge image

Raspberry Pi Code - DHT11 Sensor - LCD I2C

Detailed Instructions

  • Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
  • Make sure your Raspberry Pi is connected to the same local network as your PC.
  • Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
  • If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
  • Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows. See to how connect your PC to Raspberry Pi via SSH.
  • Make sure you have the RPi.GPIO library installed. If not, install it using the following command:
sudo apt-get update sudo apt-get install python3-rpi.gpio
pip install lcddriver
  • Install the library for DHT11 temperature and humidity sensor by running the following command:
sudo pip3 install Adafruit_DHT
  • Create a Python script file DHT11_LCD.py and add the following code:
""" This Raspberry Pi code was developed by newbiely.com This Raspberry Pi code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-dht11-lcd """ import lcddriver import Adafruit_DHT from time import sleep # Constants DHT_PIN = 20 # GPIO pin connected to DHT11 sensor LCD = lcddriver.lcd() def read_dht11_sensor(): try: humidity, temperature = Adafruit_DHT.read_retry(Adafruit_DHT.DHT11, DHT_PIN) return humidity, temperature except Exception as e: print(f"Error reading DHT11 sensor: {e}") return None, None def display_lcd(temperature, humidity): LCD.lcd_clear() LCD.lcd_display_string(f"Temp: {temperature:.1f}C", 1) LCD.lcd_display_string(f"Humi: {humidity:.1f}%", 2) try: while True: humidity, temperature = read_dht11_sensor() if humidity is not None and temperature is not None: display_lcd(temperature, humidity) else: print("Failed to read DHT11 sensor.") sleep(2) except KeyboardInterrupt: pass finally: LCD.lcd_clear()
  • Save the file and run the Python script by executing the following command in the terminal:
python3 DHT11_LCD.py

The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.

  • Change the temperature of the environment around the sensor.
  • Check out the result on the LCD.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!