LCD I2C Interface & DHT11 Sensor

Introduction

In this tutorial, we will interface a 16×2 LCD with I2C module and DHT11 sensor with ESP32.

The ESP32 will read temperature and humidity values from DHT11 sensor and display them on LCD.

Required Components

ComponentQuantity
ESP32 Board1
16×2 LCD with I2C Module1
DHT11 Sensor1
Breadboard1
Jumper WiresFew

DHT11 Sensor

DHT11 is a temperature and humidity sensor.

  • Measures temperature in °C
  • Measures humidity in %
  • Simple digital output

LCD I2C Module

I2C module reduces LCD wiring using only 2 communication pins.

LCD I2C PinESP32 Connection
VCC5V
GNDGND
SDAGPIO 21
SCLGPIO 22

DHT11 Connection

DHT11 PinESP32 Connection
VCC3.3V
GNDGND
DATAGPIO 4

Install Required Libraries

Install the following libraries from Arduino IDE Library Manager:

  • DHT Sensor Library
  • LiquidCrystal I2C
  • Adafruit Unified Sensor

Open:

Sketch > Include Library > Manage Libraries

ESP32 LCD + DHT11 Program

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "DHT.h"

#define DHTPIN 4
#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);

LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {

  lcd.init();
  lcd.backlight();

  dht.begin();

}

void loop() {

  float temp = dht.readTemperature();
  float hum = dht.readHumidity();

  lcd.clear();

  lcd.setCursor(0,0);
  lcd.print("Temp: ");
  lcd.print(temp);
  lcd.print(" C");

  lcd.setCursor(0,1);
  lcd.print("Hum: ");
  lcd.print(hum);
  lcd.print(" %");

  delay(2000);

}

Program Explanation

CodePurpose
dht.readTemperature()Reads temperature value
dht.readHumidity()Reads humidity value
lcd.setCursor()Sets LCD cursor position
lcd.print()Displays text on LCD

Output

After uploading the code:

  • LCD displays temperature value
  • LCD displays humidity value
  • Values update automatically every 2 seconds

Important Notes

  • Common LCD I2C address is 0x27.
  • If LCD does not work, try address 0x3F.
  • DHT11 is slower compared to DHT22.
  • Use proper USB cable for stable ESP32 operation.

Buy On Amazon

Scroll to Top