Arduino UNO R4 - DHT22 - LCD
We'll learn programming an Arduino UNO R4 to read temperature and humidity from a DHT22 sensor and show these on an I2C LCD display.
Hardware Preparation
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.
Additionally, some of these links are for products from our own brand, DIYables.
Overview of DHT22 and LCD
Learn about the DHT22 sensor and LCD, including their pinout, functions, and programming, in the tutorials below:
Wiring Diagram
This image is created using Fritzing. Click to enlarge image
Arduino UNO R4 Code - DHT22 Sensor - LCD I2C
/*
* This Arduino UNO R4 code was developed by newbiely.com
*
* This Arduino UNO R4 code is made available for public use without any restriction
*
* For comprehensive instructions and wiring diagrams, please visit:
* https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-dht22-lcd
*/
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#define DHT22_PIN 2 // The Arduino Uno R4 pin connected to DHT22 module
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27 (from DIYables LCD), 16 column and 2 rows
DHT dht22(DHT22_PIN, DHT22);
void setup()
{
dht22.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 = dht22.readHumidity(); // read humidity
float tempC = dht22.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:
The LCD I2C address can differ based on the manufacturer. In our code, we used the address 0x27 as provided by the manufacturer DIYables.
Detailed Instructions
Follow these instructions step by step:
- If this is your first time using the Arduino Uno R4 WiFi/Minima, refer to the tutorial on setting up the environment for Arduino Uno R4 WiFi/Minima in the Arduino IDE.
- Connect the Arduino Uno R4 to the DHT22 module and LCD I2C according to the provided diagram.
- Connect the Arduino Uno R4 board to your computer using a USB cable.
- Launch the Arduino IDE on your computer.
- Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port.
- Click on the Libraries icon on the left side of the Arduino IDE.
- Type "DHT" in the search box and look for the DHT sensor library by Adafruit.
- Click the Install button to add the library.
- You will need to install additional library dependencies.
- Click the Install All button to install all library dependencies.
- Search for "LiquidCrystal I2C" and choose the LiquidCrystal_I2C library by Frank de Brabander. Click the Install button to install the library.
- Copy the code above and open it in Arduino IDE.
- Click the Upload button in Arduino IDE to upload the code to Arduino UNO R4.
- Change the temperature around the sensor to either hotter or colder.
- Observe the result on the LCD.
If the LCD screen shows no information, please check here: Troubleshooting on LCD I2C