ESP32 UNO - DHT22 - LCD

Need a thermometer and hygrometer that runs on its own? With the ESP32 Uno DHT22 LCD combo, the AM2302 sensor measures the air and a 16x2 I2C character screen prints the result. The ESP32 Uno form board grabs a fresh pair of values every 2 seconds, with 0.1 degree steps, so even small changes in the room are easy to spot.

By the end, you can:

  1. Hook the DHT22 module and a 16x2 I2C screen up to the ESP32 Uno headers
  2. Add the Adafruit DHT and DIYables_LCD_I2C libraries in Arduino IDE
  3. Get Celsius and percent humidity readings with Arduino code
  4. Print the two values on separate rows of the LCD I2C screen
ESP32 Uno DHT22 LCD

Parts List

1×ESP32 Uno-form Board
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×LCD I2C Display 3.3V
1×Alternatively, LCD I2C 20x4
1×DHT22 Temperature Humidity Sensor Module
1×Alternatively, DHT22 Sensor
1×Jumper Wires

Or you can buy the following kits:

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 .

Buy Note: Most LCD I2C displays available on the market are 5V displays. They may work with this board, but it is unsafe and can damage the board. We strongly recommend using the 3.3V LCD I2C display recommended above.

DHT22 Sensor and LCD I2C at a Glance

Read this part to know what each module brings to the build.

Item DHT22 (AM2302) Module LCD I2C 16x2
Role here Senses the air Shows the numbers
Signal Single data wire with timed pulses Two wires, SDA and SCL
Range -40 to 80°C and 0 to 100% RH 2 rows of 16 characters
Accuracy About ±0.5°C and ±2 to 5% RH Not applicable
Power 3.3V to 6V 5V

Good to know: the DHT22 can only give one new sample every 2 seconds, which is why the sketch waits 2000 ms in each loop.

Want the pinout and the basic sketch of each part on its own? Start with these pages:

ESP32 Uno Pinout

A header label such as D2 is only a position. The sketch needs the GPIO number behind it, so look up D2, SDA and SCL on this drawing first.

ESP32 Uno pinout diagram

How to Connect

After this part, the DHT22 sits on D2 and the screen sits on the I2C pins.

The wiring diagram between ESP32 Uno DHT22 LCD

This image is created using Fritzing. Click to enlarge image

※ NOTE THAT:

The wiring diagram may not show the correct wiring for the ESP32 Uno. The LCD backpack pulls SDA and SCL up to 5V, but ESP32 Uno pins are 3.3V only. The picture shows the LCD wired straight to SDA and SCL. We suggest a level converter between the LCD and the board, as the hardware list shows. We will update the diagram soon. Until then, follow the wiring table and the text on this page, not the picture.

Module Pin ESP32 Uno Pin
DHT22 VCC (+) 3.3V
DHT22 GND (-) GND
DHT22 DATA (OUT) D2 (GPIO26)
LCD VCC 5V
LCD GND GND
LCD SDA SDA (GPIO21)
LCD SCL SCL (GPIO22)

Good to know: the A4 and A5 holes on this board are input-only analog pins with no I2C role. The screen goes on the separate SDA and SCL pins.

Good to know: the DHT22 module already has a pull-up on DATA. If you use a bare 4-pin AM2302, add a 10 kΩ resistor between DATA and 3.3V.

WARNING

The ESP32 Uno runs at 3.3V, and its pins are NOT 5V tolerant. Supply the DHT22 from 3.3V so the DATA pull-up cannot push 5V into GPIO26. The LCD backpack needs 5V, and most backpacks tie SDA and SCL to 5V through their own resistors. To protect the board, add a bidirectional 3.3V/5V level converter on SDA and SCL, or remove the backpack pull-up resistors.

ESP32 Uno Code - DHT22 and LCD I2C

The sketch reads the DHT22 on GPIO26 every 2 seconds and shows the temperature on the top row and the humidity on the bottom row.

/* * This ESP32 Uno code was developed by newbiely.com * * This ESP32 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-uno/esp32-uno-dht22-lcd */ #include <DIYables_LCD_I2C.h> #include <DHT.h> #define DHT22_PIN 26 // DHT22 DATA wire goes to D2 (GPIO26) DIYables_LCD_I2C lcd(0x27, 16, 2); // DIYables screen: address 0x27, 16 chars on 2 rows DHT dht22(DHT22_PIN, DHT22); void setup() { dht22.begin(); // get the DHT22 ready lcd.init(); // start the screen on SDA (GPIO21) and SCL (GPIO22) lcd.backlight(); // turn on the screen light } void loop() { delay(2000); // give the DHT22 time before the next reading float humi = dht22.readHumidity(); // humidity in percent float tempC = dht22.readTemperature(); // temperature in degrees Celsius lcd.clear(); // a NaN value means the reading did not work if (isnan(humi) || isnan(tempC)) { lcd.setCursor(0, 0); lcd.print("Failed"); } else { lcd.setCursor(0, 0); // top row, first column lcd.print("Temp: "); lcd.print(tempC); // write the temperature lcd.print((char)223); // character 223 is the degree sign lcd.print("C"); lcd.setCursor(0, 1); // bottom row, first column lcd.print("Humi: "); lcd.print(humi); // write the humidity lcd.print("%"); } }

※ NOTE THAT:

Not every LCD I2C board uses the same address. This sketch is set to 0x27, which matches the DIYables screen. Other boards often use 0x3F, so change the number if the backlight is on but no text shows.

Upload and Test

  1. First time with this board? Set it up with ESP32 Uno - Getting Started before you continue.
  2. Build the circuit by following the wiring table above.
  3. Plug in the USB Type-C cable between the ESP32 Uno and your computer.
  4. Select the board in Arduino IDE: pick ESP32 Dev Module and the COM port of the board (CH340).
  5. Go to the Library Manager: it is the books icon (Libraries) in the side bar of the IDE.
  6. Add the DHT library: enter "DHT" in the search field, find DHT sensor library by Adafruit, and hit Install.
  • Search for DHT sensor library created by Adafruit and click the Install button.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Library Manager
Type:
All
Topic:
All
DHT sensor library by Adafruit
Arduino library for DHT11, DHT22, etc Temp & Humidity Sensors More info
1.4.6
INSTALL
Newbiely.ino
···
1 void setup() {
Output
Serial Monitor
Ln 1, Col 1
ESP32 Dev Module on COM15
1
  1. Add its helpers: a window lists the libraries it depends on. Accept them all with Install All.
  • Search for Adafruit Unified Sensor created by Adafruit and click the Install button.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Library Manager
Type:
All
Topic:
All
Adafruit Unified Sensor by Adafruit
A unified sensor abstraction layer used by many Adafruit sensor libraries. More info
1.1.14
INSTALL
Newbiely.ino
···
1 void setup() {
Output
Serial Monitor
Ln 1, Col 1
ESP32 Dev Module on COM15
1
  1. Add the LCD library: enter "DIYables LCD I2C", find DIYables_LCD_I2C by DIYables, and hit Install.
  • Search for DIYables LCD I2C created by DIYables.io and click the Install button.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Library Manager
Type:
All
Topic:
All
DIYables LCD I2C by DIYables.io
This library is designed for HD44780-based I2C LCD displays. It provides LiquidCrystal-compatible API plus helper functions (text alignment, progress bars, predefined custom characters). Supports multiple I2C buses (Wire, Wire1, Wire2) for advanced boards like Arduino Giga, Due, and ESP32. Compatible with all Arduino-based platforms including Arduino Uno, Mega, Nano, ESP32, ESP8266, STM32, and Raspberry Pi Pico. More info
1.0.0
INSTALL
Newbiely.ino
···
1 void setup() {
Output
Serial Monitor
Ln 1, Col 1
ESP32 Dev Module on COM15
1
  1. Put the sketch in the IDE: copy the code above into an empty sketch.
  2. Upload it: press Upload and wait until the IDE says the upload is done.
  3. Warm or cool the sensor: pinch it between two fingers, or hold it near an open fridge for a moment.
  4. Watch the LCD: the screen shows something like the rows below and updates every 2 seconds.
Moment Top Row Bottom Row
Right after power-up Temp 22.40°C Humi 48.70%
Sensor held in your fingers Temp 25.90°C Humi 54.30%
Near the open fridge Temp 19.80°C Humi 61.20%
  1. Pro tip: the DHT22 reacts slowly to heat. Give it 20 to 30 seconds to settle after you move it to a new spot before you note a value.

Screen stays blank? See the LCD I2C troubleshooting section.

Video Tutorial

See the ESP32 Uno build in action in the video below.

How the Sketch Works

After this part, you know which line to edit for each change.

Code Part What It Does
#define DHT22_PIN 26 Tells the library that DATA is on D2 (GPIO26)
DHT dht22(DHT22_PIN, DHT22) Creates the sensor object and sets the sensor type to DHT22
lcd.init() and lcd.backlight() Start the screen over I2C and light it up
dht22.readHumidity() and dht22.readTemperature() Return the newest humidity (%) and temperature (°C) as float numbers
isnan(humi) or isnan(tempC) Catches a bad reading and prints "Failed" in its place
lcd.print((char)223) Prints the ° symbol from the LCD font

Every 2 seconds the screen is wiped and filled again with two fresh lines.

ESP32 Uno vs UNO R4 for This Project

Moving this build from the UNO R4 to the ESP32 Uno changes only a few things.

Item Arduino UNO R4 ESP32 Uno
Logic voltage 5V 3.3V
DHT22 data pin in code 2 26 (same D2 header spot)
LCD bus pins SDA and SCL, or A4 and A5 SDA GPIO21 and SCL GPIO22 only
Best DHT22 supply Either 5V or 3.3V 3.3V only
Libraries Adafruit DHT and DIYables_LCD_I2C No change
Wireless WiFi on the R4 WiFi only WiFi and Bluetooth Classic and BLE
Board to select in the IDE Arduino UNO R4 WiFi or Arduino UNO R4 Minima ESP32 Dev Module

The wires stay in the same header spots, and only the pin number in code is different. The one thing to watch is the 5V pull-up on the LCD I2C lines.

Go Wireless: Log the Climate on Your Phone

After this part, you have a plan to see the readings away from the desk.

The ESP32 Uno has WiFi and Bluetooth on the chip, so the LCD does not have to be the only output. Send the same temperature and humidity to a web page or a phone app. A live graph makes slow trends easy to see, like a room that gets damp at night.

Troubleshooting

Problem Possible Cause Solution
The board has no COM port in the IDE CH340 driver not installed, or the cable only charges Install the CH340 driver and swap to a USB cable that has data lines
Upload ends with "Failed to connect" or hangs at "Waiting for download" The chip did not switch to download mode Press and hold BOOT while the upload starts, then release it
LCD stays dark with no backlight No 5V on the LCD, or loose GND Check the LCD VCC wire on 5V and the GND wire
Light is on but the rows are empty Address is not 0x27, or contrast is set too low Find the address with an I2C scanner, update the code, then adjust the contrast screw
Top row says "Failed" DHT22 is not answering Check DATA on D2 (GPIO26) and the 3.3V and GND wires, and add a 10 kΩ pull-up on a bare sensor
Error "DHT.h - No such file" when you compile Adafruit library missing Install DHT sensor library together with Adafruit Unified Sensor
Strange symbols appear on the screen SDA or SCL wire loose, or wires too long Press the jumpers in firmly and keep the I2C wires short
Reading is a degree or two above room temperature Heat from the board or regulator reaches the sensor Place the DHT22 a few centimeters away from the board on longer wires

FAQ

Why pick a DHT22 instead of a DHT11 for this display?

The DHT22 covers -40 to 80°C and the full 0 to 100% humidity range. It is also more accurate, about ±0.5°C, and shows one decimal place. The DHT11 is cheaper but rougher. The wiring is the same, so you can swap one for the other.

Is 3.3V enough to power the DHT22 on the ESP32 Uno?

Yes. Any supply from 3.3V up to 6V is fine for this sensor. Using 3.3V keeps the DATA line at a level the ESP32 Uno can take. With 5V, the module pull-up would put 5V on GPIO26.

Which pins are SDA and SCL on the ESP32 Uno?

They are the two pins labeled SDA (GPIO21) and SCL (GPIO22) at the end of the digital header. A4 and A5 have no I2C job here. lcd.init() uses GPIO21 and GPIO22 by default, so you add no pin code.

Does the LCD need a level converter on the ESP32 Uno?

It is a good idea. The backpack pulls SDA and SCL up to its 5V supply, and the ESP32 Uno pins are 3.3V only. A small bidirectional converter fixes this. Taking off the backpack pull-up resistors also works.

How do I show °F on the LCD?

Pass true to the function: dht22.readTemperature(true). Then change the "C" that comes after the degree sign to "F".

Can I move the DHT22 to a different pin?

Yes. Most digital pins work, such as D4 (GPIO17) or D6 (GPIO27). Put the GPIO number in DHT22_PIN, not the D label. Avoid D8 (GPIO12), D10 (GPIO5) and A0 (GPIO2), because they are boot strapping pins.

Does the DHT22 use the analog input of the ESP32 Uno?

No. The DHT22 sends digital pulses on one wire, and the library decodes them. So the 12-bit ADC is not used, and there is no ADC setup line in this sketch.

What changes in the code compared to the UNO R4 version?

Only the pin number. The UNO R4 sketch uses pin 2, and the ESP32 Uno sketch uses 26 for the same D2 header spot. Both libraries run on both boards without changes.

Why do I need a CH340 driver for this board?

The ESP32 Uno talks to your computer through a CH340 USB-to-serial chip. Some computers do not have its driver, and then no COM port shows up until you install it.

Can I add a second DHT22 to the same LCD?

Yes. Wire it to another digital pin and create a second DHT object with that pin. Then show the two sensors on the two rows, or switch between them every few seconds.

What if my project needs more pins than the Uno headers give?

Look above the power header: the ESP32 Uno has a small row of extra holes there. GPIO32 and GPIO33 give I/O, PWM and analog on ADC1, so their analog reads still work while WiFi is on. GPIO15 gives I/O, PWM and analog on ADC2. To use one, solder a pin header into the hole and type its GPIO number in the sketch. For this build, GPIO32 or GPIO33 is the best spot for a second DHT22 or a light sensor. Keep two warnings in mind. First, GPIO15 and GPIO0 (the IO0 pin at the unused Uno NC position) are boot strapping pins: if a module pulls them the wrong way, the board may not boot or take new code, so try GPIO32/33 first. Second, leave the six holes below the digital header alone (SD0 to SD3, CMD, CLK = GPIO6 to GPIO11). They are tied to the flash memory chip, so wiring anything there makes the board crash.

※ 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!