Arduino Nano - Humidity Sensor - LCD

This tutorial instructs you how to use Arduino Nano to read the temperature and humidity from DHT22 sensor and display it on LCD I2C.

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
1×LCD I2C
1×DHT22 Temperature and Humidity Sensor
1×Jumper Wires
1×(Optional) 9V Power Adapter for Arduino Nano
1×(Recommended) Screw Terminal Adapter for Arduino Nano

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
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. We appreciate your support.

Overview of DHT22 and LCD

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

Wiring Diagram

Arduino Nano - DHT22 and LCD Wiring Diagram

The wiring diagram between Arduino Nano and DHT22 temperature and humidity LCD

This image is created using Fritzing. Click to enlarge image

Please note that the power from the 5V pin of the Arduino Nano may not be enough for both the DHT22 and the LCD. If you see the LCD display nothing, please add extra power to the LCD and DHT22 as shown in the wiring diagram below.

The wiring diagram between Arduino Nano and DHT22 temperature humidity LCD external power

This image is created using Fritzing. Click to enlarge image

Arduino Nano Code - DHT22 Sensor - LCD I2C

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-humidity-sensor-lcd */ #include <LiquidCrystal_I2C.h> #include "DHT.h" #define DHT_PIN 2 // The Arduino Nano pin connected to DHT22 sensor #define DHT_TYPE DHT22 LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27 (from DIYables LCD), 16 column and 2 rows DHT dht22(DHT_PIN, DHT_TYPE); void setup() { dht22.begin(); // initialize the sensor lcd.init(); // Initialize the LCD I2C display lcd.backlight(); // open the backlight } void loop() { delay(2000); // wait a few seconds between measurements float humi = dht22.readHumidity(); // read humidity float temperature_C = dht22.readTemperature(); // read temperature lcd.clear(); // check if any reads failed if (isnan(humi) || isnan(temperature_C)) { lcd.setCursor(0, 0); lcd.print("Failed"); } else { lcd.setCursor(0, 0); // start to print at the first row lcd.print("Temp: "); lcd.print(temperature_C); // 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 address of the LCD can differ depending on the manufacturer. We have used 0x27 in our code, which is specified by DIYables manufacturer.

Detailed Instructions

  • Connect an USB cable between the Arduino Nano and the PC.
  • Open the Arduino IDE, choose the correct board and port.
  • Click to the Libraries icon on the left bar of the Arduino IDE.
  • Search for “DHT” and locate the DHT sensor library by Adafruit.
  • Press the Install button to install the library.
Arduino Nano DHT sensor library
  • You will be prompted to install some other library dependencies.
  • Click the Install All button to install all necessary library dependencies.
Arduino Nano Adafruit Unified sensor library
  • Search for “LiquidCrystal I2C” and locate the LiquidCrystal_I2C library by Frank de Brabander.
  • Then, click the Install button to install the library.
Arduino Nano LiquidCrystal I2C library
  • Copy the code above and open it with the Arduino IDE.
  • Click the Upload button in the Arduino IDE to send the code to the Arduino Nano.
  • Change the temperature of the environment around the sensor.
  • Check out the result on the LCD.

If the LCD does not show anything, check Troubleshooting on LCD I2C for help.

Video Tutorial