ESP32 - Temperature Humidity Sensor - LCD

We will learn how to:

Hardware Required

1×ESP-WROOM-32 Dev Module
1×Micro USB Cable
1×LCD I2C
1×Temperature and Humidity Sensor DHT11
Or 1×Temperature and Humidity Sensor DHT22
n×Jumper Wires
Please note: These are affiliate links. If you buy the components through these links, We may get a commission at no extra cost to you. We appreciate it.

About DHT11, DHT22 and LCD

If you do not have knowledge of DHT11, DHT22 temperature sensor and LCD (pinout, how they work, how to connect them to ESP32, how to program for them...), learn about them in the following tutorials:

Wiring Diagram

ESP32 - DHT11 Module Wiring

ESP32 DHT11 temperature and humidity LCD Wiring Diagram

Image is developed using Fritzing. Click to enlarge image

ESP32 - DHT22 Module Wiring

ESP32 DHT22 temperature and humidity LCD Wiring Diagram

Image is developed using Fritzing. Click to enlarge image

ESP32 Code - DHT11 Sensor - LCD I2C

/* * Created by esp32io.com * * This example code is in the public domain * * Tutorial page: https://esp32io.com/tutorials/esp32-temperature-humidity-sensor-lcd */ #include <LiquidCrystal_I2C.h> #include "DHT.h" #define DHTPIN 2 #define DHTTYPE DHT11 LiquidCrystal_I2C lcd(0x3F, 16, 2); // I2C address 0x3F, 16 column and 2 rows DHT dht(DHTPIN, DHTTYPE); void setup() { dht.begin(); // initialize the sensor lcd.init(); // initialize the lcd lcd.backlight(); // open the backlight } void loop() { delay(2000); // wait a few seconds between measurements float humi = dht.readHumidity(); // read humidity float tempC = dht.readTemperature(); // read temperature lcd.clear(); // check if any reads failed if (isnan(humi) || isnan(tempC)) { lcd.setCursor(0, 0); lcd.print("Failed"); } else { lcd.setCursor(0, 0); // start to print at the first row lcd.print("Temp: "); lcd.print(tempC); // print the temperature lcd.print((char)223); // print ° character lcd.print("C"); lcd.setCursor(0, 1); // start to print at the second row lcd.print("Humi: "); lcd.print(humi); // print the humidity lcd.print("%"); } }

Quick Steps

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE
  • Connect the ESP32 board to your PC via a micro USB cable
  • Open Arduino IDE, select the right ESP32 board and COM port
  • On Arduino IDE, Go to Tools Manage Libraries
  • ESP32 add library
  • Search “DHT”, then find the DHT sensor library by Adafruit
  • Click Install button to install the library.
  • ESP32 DHT sensor library
  • You will be asked for intalling some other library dependencies
  • Click Install All button all library dependencies.
  • ESP32 Adafruit Unified sensor library
  • Search “LiquidCrystal I2C”, then find the LiquidCrystal_I2C library by Frank de Brabander
  • Click Install button to install LiquidCrystal_I2C library.
  • ESP32 LiquidCrystal I2C library
  • Copy the above code and paste it to Arduino IDE
  • Click Upload button on Arduino IDE to compile and upload code to ESP32 board
  • Make enviroment around sensor hotter or colder
  • See the result in LCD

If LCD displays nothing, see Troubleshooting on LCD I2C

ESP32 Code - DHT22 Sensor - LCD I2C

/* * Created by esp32io.com * * This example code is in the public domain * * Tutorial page: https://esp32io.com/tutorials/esp32-temperature-humidity-sensor-lcd */ #include <LiquidCrystal_I2C.h> #include "DHT.h" #define DHTPIN 2 #define DHTTYPE DHT22 LiquidCrystal_I2C lcd(0x3F, 16, 2); // I2C address 0x3F, 16 column and 2 rows DHT dht(DHTPIN, DHTTYPE); void setup() { dht.begin(); // initialize the sensor lcd.init(); // initialize the lcd lcd.backlight(); // open the backlight } void loop() { delay(2000); // wait a few seconds between measurements float humi = dht.readHumidity(); // read humidity float tempC = dht.readTemperature(); // read temperature lcd.clear(); // check if any reads failed if (isnan(humi) || isnan(tempC)) { lcd.setCursor(0, 0); lcd.print("Failed"); } else { lcd.setCursor(0, 0); // start to print at the first row lcd.print("Temp: "); lcd.print(tempC); // print the temperature lcd.print((char)223); // print ° character lcd.print("C"); lcd.setCursor(0, 1); // start to print at the second row lcd.print("Humi: "); lcd.print(humi); // print the humidity lcd.print("%"); } }

※ NOTE THAT:

Code for DHT11 and DHT22 are identical except for one line of code. Library for DHT11 and DHT22 are the same.

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

※ NOTE THAT:

Note that this tutorial is incomplete. We will post on our Facebook Page when the tutorial is complete. Like it to get updated.

※ NOTICES

  • We are AVAILABLE for HIRE. See how to hire us to do your project
  • If this tutorial is useful for you, please support us to make more tutorials.
  • We spent a lot of time and effort to create the content for this tutorial, please respect our work! Please do not copy the content to share on other websites. Howerver, please feel free to share the link of this tutorial anywhere