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.
How to Use the HTU21DF Humidity and Temperature Sensor with Arduino (Basic Code)
In this tutorial, we will learn how to use the HTU21DF temperature and relative humidity sensor with an Arduino. This sensor is known for its accuracy and low power consumption, making it ideal for various applications. By the end of this tutorial, you will be able to read temperature and humidity values and display them on the Arduino Serial Monitor. For additional clarification, be sure to check the video at (in video at 00:00).
Hardware Explained
The HTU21DF sensor is a digital humidity and temperature sensor that communicates using the I2C protocol. It can measure temperatures ranging from -40°C to +125°C and relative humidity from 0% to 100%. The sensor operates with a supply voltage of 3.3V, making it suitable for battery-powered applications.
Two pins are essential for communication: SDA (data line) and SCL (clock line). The sensor also has a low power consumption mode, drawing only 0.04 µA when idle and approximately 400 µA during measurements. This feature makes it particularly useful in energy-sensitive projects.
Datasheet Details
| Manufacturer | TE Connectivity (formerly MEAS) |
|---|---|
| Part number | HTU21DF |
| Logic/IO voltage | 3.3 V |
| Supply voltage | 1.5 – 3.6 V |
| Idle current consumption | 0.04 µA |
| Measurement current consumption | 400 µA |
| Temperature range | -40 to +125 °C |
| Humidity range | 0 to 100 % |
| Resolution | 0.04 % (humidity), 0.01 °C (temperature) |
| Package | 6-pin LGA |
- Ensure proper connections to avoid damage.
- Keep power supply within specified voltage limits (1.5 to 3.6 V).
- Use pull-up resistors on the SDA and SCL lines if necessary.
- Be cautious of reversed polarity when connecting power.
- Allow the sensor some time to stabilize after powering on.
Wiring Instructions
To connect the HTU21DF sensor to the Arduino, follow these steps:
First, connect the left pin of the sensor to the 3.3V supply of the Arduino. The second pin, which is usually marked in red, should be connected to the ground (GND). Next, connect the SDA pin of the sensor to pin A4 on the Arduino, and connect the SCL pin to pin A5. This setup will allow the Arduino to communicate with the sensor using the I2C protocol.
Ensure all connections are secure to prevent any communication issues. If you are using a breadboard, double-check the wiring to maintain proper connections. In the video, alternative wiring methods are discussed at (in video at 01:30).
Code Examples & Walkthrough
Below is a brief overview of the code used to interface with the HTU21DF sensor. First, the necessary libraries are included:
#include
#include "Adafruit_HTU21DF.h"
This code initializes the I2C communication and creates an instance of the HTU21DF sensor as htu.
Next, we set up the serial communication in the setup() function:
void setup() {
Serial.begin(9600);
if (!htu.begin()) {
Serial.println("Couldn't find sensor!");
while (1);
}
}
Here, the sensor is initialized, and if it fails to connect, a message is printed, and the program halts.
In the loop() function, we read the temperature and humidity values:
void loop() {
float temp = htu.readTemperature();
float rel_hum = htu.readHumidity();
Serial.print("Temp: "); Serial.print(temp); Serial.print(" C");
Serial.print("\t\t");
Serial.print("Humidity: "); Serial.print(rel_hum); Serial.println(" %");
delay(500);
}
The temperature is stored in the variable temp, and the relative humidity is stored in rel_hum. These values are printed to the Serial Monitor every 500 milliseconds, allowing you to observe changes in real time.
For the complete code, please refer to the full program loaded below the article.
Demonstration / What to Expect
Once everything is wired and the code is uploaded, open the Serial Monitor to see the temperature and humidity readings. You should see the values update every half second. If you apply heat to the sensor using a heat gun, you will notice the temperature readings increase while the humidity readings decrease (in video at 05:00).
Be aware that if the temperature exceeds the maximum limit of 125°C, the readings may become inaccurate or reset. It's essential to keep the sensor within its specified operating range to ensure accurate measurements.
Video Timestamps
- 00:00 - Introduction
- 01:30 - Wiring Instructions
- 05:00 - Demonstration
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
- Displaying Temperature from an HTU21D as a Bar Graph on an LCD
- How to Use the HTU21DF Humidity and Temperature Sensor with Arduino (Custom Code)
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