This tutorial is part of: Digital Relative Humidity & Temperature Sensor HTU21D
Videos related to Digital Relative Humidity & Temperature Sensor HTU21D. Links to other videos are below this article.
Displaying Temperature from an HTU21D on an LCD
In this tutorial, we will learn how to display temperature readings from the HTU21D temperature and humidity sensor on an LCD. The outcome will be a functional setup where the temperature can be viewed in degrees Celsius, Fahrenheit, and Kelvin, along with the relative humidity displayed on the same screen. This project will give you practical experience with I2C communication and basic sensor data handling. For a detailed visual guide, be sure to watch the video (in video at 00:00).
Hardware Explained
The main components for this project include the HTU21D sensor and the LCD display. The HTU21D is a digital humidity and temperature sensor that communicates over the I2C protocol. It provides precise measurements of temperature in degrees Celsius or Fahrenheit and relative humidity as a percentage. The sensor operates typically at a voltage of 3.3V but can also work with 5V systems. The LCD display we will use is a 16x2 LCD with an I2C interface. This type of display requires only two wires for data communication, making it easier to connect with microcontrollers like Arduino. The I2C interface allows multiple devices to be connected on the same bus, simplifying wiring and reducing the number of pins used.Datasheet Details
| Manufacturer | Adafruit |
|---|---|
| Part number | HTU21D-F |
| Logic/IO voltage | 3.3 V (typ.) |
| Supply voltage | 1.5 V to 3.6 V |
| Output current | 0.5 mA (typ.) |
| Peak current | 1.5 mA (max.) |
| Temperature range | -40 to 125 °C |
| Humidity range | 0 to 100 %RH |
| Package | DFN-6 |
| Notes / variants | None |
- Ensure correct voltage levels to avoid damage to the sensor.
- Use pull-up resistors on the SDA and SCL lines if necessary.
- Check I2C address with a scanner to ensure correct configuration.
- Handle the sensor's output carefully to avoid reading errors.
- Keep the sensor away from heat sources when taking measurements.
Wiring Instructions

Code Examples & Walkthrough
In the code, we start by including the necessary libraries for the HTU21D sensor and the LCD:#include
#include "Adafruit_HTU21DF.h"
#include
This sets up the environment for using both the sensor and the display. The `Adafruit_HTU21DF` library handles the sensor's functionality, while the `LiquidCrystal_I2C` library manages the LCD.
Next, we create an instance for both the sensor and the LCD:
Adafruit_HTU21DF htu = Adafruit_HTU21DF();
LiquidCrystal_I2C lcd(0x3F, 16, 2);
Here, htu is the object for the HTU21D sensor, and lcd is the object for the LCD display. The I2C address for the LCD is set to 0x3F, which you may need to verify based on your configuration.
In the setup() function, we initialize the LCD and check if the sensor is operational:
void setup() {
lcd.begin();
lcd.backlight();
if (!htu.begin()) {
lcd.print("Sensor missing");
while (1);
}
lcd.print("HTU21D Ready");
}
This code initializes the LCD and checks for the sensor's presence. If the sensor is not detected, it will display "Sensor missing" and halt the program.
Finally, in the loop() function, we continuously read the temperature and humidity values and display them:
void loop() {
lcd.clear();
lcdDisplay(0, 0, "Celsius: ", 10, 0, getHTU('C'), 'd');
lcdDisplay(0, 1, "Humidity: ", 10, 1, getHTU('H'), '%');
delay(5000);
}
In this excerpt, we clear the LCD, then call the lcdDisplay() function to show the temperature in Celsius and the humidity. The getHTU() function retrieves the temperature or humidity based on the character passed.
Demonstration / What to Expect
When the setup is complete and the code is uploaded to the Arduino, the LCD should display the current temperature in Celsius and the relative humidity. If everything is connected properly, you will see the values update every few seconds. Watch out for common pitfalls, such as reversed connections or incorrect I2C addresses, which can lead to sensor communication failures (in video at 05:00).Video Timestamps
- 00:00 - Introduction
- 01:30 - Wiring Instructions
- 03:15 - Code Explanation
- 04:50 - Demonstration
- 05:40 - Conclusion
This tutorial is part of: Digital Relative Humidity & Temperature Sensor HTU21D
- Lesson 35-1: Using the HTU21D Temperature Sensor
- Lesson 35-2: Using the HTU21D Temperature Sensor Custom Code
- Lesson 35: Using HTU21D Temperature Sensor with Arduino
- Lesson 36: Using the HTU21D Temperature Sensor with an LCD Arduino Step-by-Step Course
- Using Two More HTU21DF Humidity and Temperature Sensors with Arduino
- Displaying Temperature from an HTU21D as a Bar Graph on an LCD
- How to Use the HTU21DF Humidity and Temperature Sensor with Arduino (Basic Code)
- How to Use the HTU21DF Humidity and Temperature Sensor with Arduino (Custom Code)
213-Display Temperature from HTU21DF on LCD1602-I2C or LCD2004
Language: C++
Copied!
Things you might need
-
Amazon
-
eBayPurchase HTU21D from eBayebay.us
-
AliExpressPurchase HTU21D or SHT21 from AliExpresss.click.aliexpress.com
Resources & references
-
ExternalAdafruit HTU21D Library (GitHub)github.com
Files📁
Datasheet (pdf)
-
HTU21D_temerature_humidity_datasheet
HTU21D_temerature_humidity_datasheet.pdf0.96 MB