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.
Lesson 36: Using the HTU21D Temperature Sensor with an LCD Arduino Step-by-Step Course
The HTU21D temperature and humidity sensor is a popular choice for many Arduino projects due to its ease of use and accuracy. In this lesson, we will build a simple project that reads temperature and humidity data from the sensor and displays it on an LCD screen. By the end of this tutorial, you will have a working setup that continuously displays the temperature in Celsius, Fahrenheit, and Kelvin, as well as the relative humidity percentage. For further clarification, you can refer to the video (in video at mm:ss).Hardware Explained
The primary component of this project is the HTU21D temperature and humidity sensor, which communicates via I2C. This sensor operates within a voltage range of 1.5 to 3.6 volts, making it suitable for both 3.3V and 5V systems. It consumes very little power, typically only 0.02 microamperes when idle and 450 microamperes during measurement. In addition to the HTU21D, we will also use an LCD display, specifically the LCD1602 with I2C. This display allows for easy output of text data and requires only two pins for communication: SDA (data line) and SCL (clock line). The combination of these components will enable us to create an informative display for monitoring temperature and humidity levels.Datasheet Details
| Manufacturer | TE Connectivity |
|---|---|
| Part number | HTU21D-F |
| Logic/IO voltage | 1.5 - 3.6 V |
| Supply voltage | 3.3 V (typ.) |
| Output current (typ.) | 0.02 µA (idle), 450 µA (measurement) |
| Temperature range | -40 to +125 °C |
| Humidity range | 0 to 100 %RH |
| Resolution (Temperature) | 0.01 °C |
| Resolution (Humidity) | 0.04 %RH |
| Package | DFN-6 |
- Use a pull-up resistor for SDA and SCL lines if not integrated.
- Ensure correct power supply voltage to avoid damaging the sensor.
- Maintain proper wiring to prevent communication errors.
- Check the I2C address if the sensor does not respond.
- Use a delay between readings to prevent sensor overload.
- Ensure the LCD is compatible with I2C communication.
Wiring Instructions
To wire the HTU21D sensor and the LCD display, start with the power connections. Connect the left pin of the HTU21D to the 3.3V power source, and the second pin (often red) to the ground. Next, connect the SDA pin of the HTU21D to the Arduino's analog pin A4, and the SCL pin to analog pin A5. For the LCD1602 display, connect the VCC pin to the same 3.3V power source, and connect the GND pin to the ground. The SDA pin on the LCD should also connect to A4, and the SCL pin should connect to A5, allowing both components to share the I2C bus. Ensure all connections are secure to facilitate proper communication between the Arduino, sensor, and display.Code Examples & Walkthrough
The following code initializes the HTU21D sensor and the LCD display. In the setup function, the LCD is prepared for use and the sensor is checked for connectivity:void setup() {
lcd.begin();
lcd.backlight();
if (!htu.begin()) {
lcd.print("Robojax HTUD1DF");
lcd.setCursor(0,1);
lcd.print("sensor missing");
while (1);
} else {
lcd.print("Robojax HTUD1DF");
lcd.setCursor(0,1);
lcd.print("Demo");
}
delay(2000);
}
This excerpt checks if the sensor is connected properly. If not, it displays an error message on the LCD and halts the program. If the sensor is functioning, it shows a demo message for two seconds.
The loop function is where the main reading and display take place. Here, we call the `lcdDisplay` function to show the temperature in different units:
void loop() {
lcd.clear(); // clear previous values from screen
lcdDisplay(0, 0, "Celsius: ", 10, 0, getHTU('C'), 'd');
lcdDisplay(0, 1, "Fahrenheit: ", 10, 1, getHTU('F'), 'd');
delay(5000);
}
In this loop, the LCD is cleared, and temperature readings in Celsius and Fahrenheit are displayed. The `getHTU` function is called with the character 'C' for Celsius and 'F' for Fahrenheit, respectively.
Finally, the `getHTU` function is defined to read the temperature or humidity based on the input character:
float getHTU(char type) {
float temp = htu.readTemperature();
float rel_hum = htu.readHumidity();
if(type =='F') {
return temp * 9/5 + 32; // convert to Fahrenheit
} else if(type =='K') {
return temp + 273.15; // convert to Kelvin
} else if(type =='H') {
return rel_hum; // return relative humidity
} else {
return temp; // return Celsius
}
}
This function reads the temperature and humidity from the sensor and converts the temperature to the requested unit. Be sure to check the complete code loaded below the article for additional details.
Demonstration / What to Expect
Upon completing the wiring and uploading the code, you should see the temperature and humidity values displayed on the LCD. The readings will update every few seconds, reflecting the current conditions. If you apply heat to the sensor, you should notice the temperature rising accordingly, while the humidity should decrease slightly. Be mindful of the maximum temperature limit of the sensor; exceeding this may result in inaccurate readings or sensor failure (in video at mm:ss).Video Timestamps
- 00:00 - Introduction to the project
- 01:15 - Wiring instructions
- 03:30 - Code walkthrough
- 10:00 - Demonstration of the setup
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
- Using Two More HTU21DF Humidity and Temperature Sensors with Arduino
- Displaying Temperature from an HTU21D on an LCD
- 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)
511-Lesson 36: Using the HTU21D Temperature Sensor with an LCD and Arduino: A Step-by-Step Course
Language: C++
/*
* Robojax Arduino Step-by-Step Course
* Part 4: Temperature Sensors
* Lesson 36: HTU21D Temperature Sensor with LCD1602 and LCD2004 Display
* Display Temperature from HTU21DF on LCD1602-I2C or LCD2004
* Updated by Ahmad Shamshiri on July 13, 2019
Please watch video instructions here https://youtu.be/SrFJKbmiaPM
This code is available at http://robojax.com/course1/?vid=lecture36
with over 100 lectures free on YouTube. Watch it here http://robojax.com/L/?id=338
Get the code for the course: http://robojax.com/L/?id=339
If you found this tutorial helpful, please support me so I can continue creating
and make a donation using PayPal http://robojax.com/L/?id=64
*
This is an example for the HTU21D-F Humidity & Temp Sensor
Designed specifically to work with the HTU21D-F sensor from Adafruit
----> https://www.adafruit.com/products/1899
These displays use I2C to communicate; 2 pins are required to
interface
Watch Introduction to a 360 Servo video with code: https://youtu.be/b_xvu6wWafA
You can get the wiring diagram from my Arduino Course at Udemy.com
Learn Arduino step by step with all libraries, codes, and wiring diagrams all in one place.
Visit my course now http://robojax.com/L/?id=62
If you found this tutorial helpful, please support me so I can continue creating
content like this.
or make a donation using PayPal http://robojax.com/L/?id=64
* Code is available at http://robojax.com/learn/arduino
* This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.
* This code has been downloaded from Robojax.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <Wire.h>
#include "Adafruit_HTU21DF.h"
// Connect Vin to 3-5VDC
// Connect GND to ground
// Connect SCL to I2C clock pin (A5 on UNO)
// Connect SDA to I2C data pin (A4 on UNO)
Adafruit_HTU21DF htu = Adafruit_HTU21DF();
// start of settings for LCD1602 with I2C
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x3F, 16, 2);
// end of settings for LCD1602 with I2C
void setup() {
//Get the code for the course: http://robojax.com/L/?id=339
lcd.begin();
lcd.backlight();
if (!htu.begin()) {
lcd.print("Robojax HTUD1DF");
lcd.setCursor(0,1);
lcd.print("sensor missing");
while (1);
}else{
// initialize the LCD
lcd.print("Robojax HTUD1DF");
lcd.setCursor(0,1);
lcd.print("Demo");
}
delay(2000);
}
void loop() {
//Get the code for the course: http://robojax.com/L/?id=339
lcd.clear();// clear previous values from screen
lcdDisplay(
// to print Celsius:
0, // character 0
0, // line 0
"Celsius: ",
// to print Celsius
10, // character 10
0, // line 0
getHTU('C'),
'd'
);
lcdDisplay(
// to print fahrenheit:
0, // character 0
1, // line 1
"Fahrenheit: ",
// to print Fahrenheit
10, // character 9
1, // line 0
getHTU('F'),
'd'
);
delay(5000);
lcdDisplay(
// to print Kelvin:
0, // character 0
0, // line 0
"Kelvin: ",
// to print Celsius
9, // character 10
0, // line 0
getHTU('K'),
'k'
);
lcdDisplay(
// to print humidity text
0, // character 0
1, // line 1
"Humidity: ",
// to print humidity
10, // character 9
1, // line 1
getHTU('H'),
'%'
);
delay(5000);
}
/*
* @brief returns temperature or relative humidity
* @param "type" is character
* C = Celsius
* K = Kelvin
* F = Fahrenheit
* H = Humidity
* @return returns one of the values above
* Usage: to get Fahrenheit type: getHTU('F')
* to print it on serial monitor Serial.println(getHTU('F'));
* Written by Ahmad Shamshiri on July 13, 2019
* in Ajax, Ontario, Canada
* www.Robojax.com
*/
float getHTU(char type)
{
//Get the code for the course: http://robojax.com/L/?id=339
float value;
float temp = htu.readTemperature();
float rel_hum = htu.readHumidity();
if(type =='F')
{
value = temp *9/5 + 32;//convert to Fahrenheit
}else if(type =='K')
{
value = temp + 273.15;//convert to Kelvin
}else if(type =='H')
{
value = rel_hum;//return relative humidity
}else{
value = temp;// return Celsius
}
return value;
}//
/*
* lcdDisplay(int tc, int tr, String title, int vc, int vr, float value)
* displays value and title on LCD1602
* How to use:
* If you want to display: "Voltage: 13.56mV" starting from the first character
* on the second row.
* use:
* lcdDisplay(0, 1, "Voltage: ", 13.56,'d')
*
* 'd' is degree symbol
* tc is character number (0)
* tr is row in the lcd (1)
* title is the text (Voltage:)
* vc value for character
* vr value for row or line
* value is the value (13.56)
*/
void lcdDisplay(int tc, int tr, String title, int vc, int vr, float value,char symbol)
{
// Robojax.com LCD1602 for HTU21D Demo
lcd.setCursor (tc,tr); //
lcd.print(title);
lcd.setCursor (vc,vr); //
lcd.print(value);
if(symbol == 'd')
{
lcd.print((char)223);
}else if(symbol =='%')
{
lcd.print("%");
}else if(symbol =='k')
{
lcd.print("K");
}
// Robojax.com LCD1602 for HTU21D Demo
}
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
-
ExternalDifference between HTU21DF and HTU21D (image)robojax.com
-
External
Files📁
Datasheet (pdf)
-
HTU21D_temerature_humidity_datasheet
HTU21D_temerature_humidity_datasheet.pdf0.96 MB