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 as a Bar Graph on an LCD
In this tutorial, we will learn how to display temperature readings from an HTU21D temperature and humidity sensor on an LCD screen in the form of a bar graph. The readings will include values in Celsius, Fahrenheit, Kelvin, and relative humidity, providing a comprehensive overview of the environmental conditions. This project will help you understand how to wire the components correctly and implement the necessary code to make everything work seamlessly.

For those who want a visual guide, be sure to check out the video associated with this tutorial (in video at 00:00).
Hardware Explained
The main components of this project include the HTU21D sensor and the LCD 1602 display. The HTU21D is a digital humidity and temperature sensor that communicates via I2C. It provides accurate readings of temperature and humidity, which can be easily accessed through its library. The LCD 1602 display, on the other hand, shows the readings in a human-readable format, allowing for quick assessments of environmental conditions.
The HTU21D uses a simple I2C interface, making it easy to connect and communicate with microcontrollers like Arduino. The LCD 1602 display also uses I2C, which simplifies the wiring by reducing the number of pins needed to connect to the Arduino. This allows for a cleaner setup while still providing clear visual output.
Datasheet Details
| Manufacturer | Adafruit |
|---|---|
| Part number | HTU21D |
| Logic/IO voltage | 3.3 V (typ.) |
| Supply voltage | 1.5 – 3.6 V |
| Temperature range | -40 to 125 °C |
| Humidity range | 0 to 100 %RH |
| Resolution | 0.01 °C / 0.04 %RH |
| Communication | I2C |
| Package | 4-pin LGA |
- Use 3.3 V to power the HTU21D; connecting to 5 V can damage it.
- Ensure proper pull-up resistors are used on the I2C lines (SDA and SCL).
- Keep the sensor away from heat sources during testing.
- Check for correct I2C address using an I2C scanner sketch.
- Ensure the LCD is correctly initialized with the right address.
Wiring Instructions

To wire the HTU21D sensor and the LCD 1602 display, follow these steps closely:
First, connect the HTU21D sensor. Connect the first pin (VCC) to the 3.3 V pin on the Arduino. The second pin (GND) should be connected to the ground (GND) of the Arduino. The third pin (SDA) is connected to the A4 pin on the Arduino, and the fourth pin (SCL) connects to the A5 pin. Make sure to use the correct wire colors for clarity, using red for VCC, black for GND, orange for SDA, and yellow for SCL.
Next, for the LCD 1602 display, connect the VCC pin to the 5 V pin on the Arduino and the GND pin to the ground. The SDA pin of the LCD should be connected to the same A4 pin used for the HTU21D, and the SCL pin should connect to the A5 pin. This setup allows both the sensor and the display to communicate via the I2C protocol.
Code Examples & Walkthrough
In the code, the first step is to include the necessary libraries for the sensor and the LCD. The line #include is essential for I2C communication, while #include "Adafruit_HTU21DF.h" initializes the HTU21D sensor. The next line creates an instance of the HTU21D class:
Adafruit_HTU21DF htu = Adafruit_HTU21DF();
This line sets up the sensor for use in the program, allowing us to call its methods later to read temperature and humidity values.
Within the setup() function, we initialize the serial monitor and the LCD display. The following lines check if the sensor is working correctly:
if (htu.begin()) {
lcd.print("HTU21DF Bargraph");
} else {
lcd.print("missing HTU21DF");
while(1); // Pause forever.
}
If the sensor fails to initialize, the program will display an error message and halt execution. This is crucial for debugging and ensures that the sensor is connected properly.
In the loop() function, temperature readings are taken and displayed. The line float T = getHTU('H'); retrieves the temperature or humidity based on the parameter passed. The retrieved value is then displayed on the LCD:
lcd.setCursor (0,1);
lcd.print(T); // print
lcd.print((char)223); // prints degree symbol
lcd.print("C"); //
This code positions the cursor on the second line of the LCD and prints the temperature value along with the degree symbol. The use of (char)223 is a handy trick to display the degree symbol on the LCD.
Demonstration / What to Expect
Once everything is set up and the code is uploaded, you should see the temperature readings displayed on the LCD as a bar graph. The readings will update continuously, reflecting the current temperature and humidity. If you blow hot air onto the sensor, you will see the temperature rise rapidly, confirming that the system works as expected. Be cautious of reversed polarity or incorrect wiring, as these can lead to malfunction or damage (in video at 02:30).
Video Timestamps
- 00:00 - Introduction
- 01:30 - Wiring Explanation
- 02:30 - Code Walkthrough
- 04:00 - Demonstration
- 05:30 - 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 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)
Resources & references
-
ExternalAdafruit HTU21D Library (GitHub)github.com
-
ExternalHTU21D Datasheet (PDF)cdn-shop.adafruit.com
Files📁
Datasheet (pdf)
-
HTU21D_temerature_humidity_datasheet
HTU21D_temerature_humidity_datasheet.pdf0.96 MB