ESP8266 - Humidity Sensor - LCD

This tutorial instructs you how to use ESP8266 to read temperature and humidity from DHT22 sensor and display them on LCD I2C.

Hardware Preparation

1×ESP8266 NodeMCU
1×Micro USB Cable
1×LCD I2C
1×DHT22 Temperature and Humidity Sensor
1×Jumper Wires
1×(Optional) 5V Power Adapter for ESP8266
1×(Optional) ESP8266 Screw Terminal Adapter

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 (including pinout, how it works, and how to program), the following tutorials can help:

Wiring Diagram

ESP8266 - DHT22 and LCD Wiring Diagram

The wiring diagram between ESP8266 NodeMCU and DHT22 temperature and humidity LCD

This image is created using Fritzing. Click to enlarge image

See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.

ESP8266 Code - DHT22 Sensor - LCD I2C

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-humidity-sensor-lcd */ #include <DHT.h> #include <LiquidCrystal_I2C.h> #define DHT22_PIN D7 // The ESP8266 pin D7 connected to DHT22 sensor 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 DHT sensor lcd.init(); // Initialize the LCD I2C display lcd.backlight(); // open the backlight } void loop() { float humi = dht22.readHumidity(); // read humidity float temperature_C = dht22.readTemperature(); // read temperature lcd.clear(); // check whether the reading is successful or not if (isnan(temperature_C) || isnan(humi)) { lcd.setCursor(0, 0); lcd.print("Failed"); } else { lcd.setCursor(0, 0); // display position lcd.print("Temp: "); lcd.print(temperature_C); // display the temperature lcd.print("°C"); lcd.setCursor(0, 1); // display position lcd.print("Humi: "); lcd.print(humi); // display the humidity lcd.print("%"); } // wait a 2 seconds between readings delay(2000); }

※ NOTE THAT:

The I2C address of LCD may differ depending on the manufacturer. In our code, we have used 0x27 as specified by DIYables manufacturer.

Detailed Instructions

To get started with ESP8266 on Arduino IDE, follow these steps:

  • Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
  • Wire the components as shown in the diagram.
  • Connect the ESP8266 board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
  • Connect an USB cable from the ESP8266 to the PC.
  • Open the Arduino IDE and select the correct board and port.
  • Click to the Libraries icon on the left bar of the Arduino IDE.
  • Search for “DHT” and locate the Adafruit DHT sensor library.
  • Press the Install button to complete the installation.
ESP8266 NodeMCU DHT sensor library
  • You will be prompted to install additional library dependencies.
  • Click the Install All button to install all necessary libraries.
ESP8266 NodeMCU Adafruit Unified sensor library
  • Search for “LiquidCrystal I2C”, then locate the LiquidCrystal_I2C library created by Frank de Brabander.
  • Click the Install button to install the LiquidCrystal_I2C library.
ESP8266 NodeMCU LiquidCrystal I2C library
  • Copy the code above and open it with the Arduino IDE.
  • Click the Upload button in the Arduino IDE to compile and upload the code to the ESP8266.
  • Change the temperature of the environment around the sensor.
  • Check out the results on the LCD.

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

Video Tutorial