Want to keep a log of the climate in your greenhouse, build a humidor control system, or track temperature and humidity data for a weather station project? The HTU21D temperature and humidity sensor may be the right choice for you!

This sensor is factory-calibrated and needs no external components to work. So with just a few connections and some Arduino code, you can start measuring relative humidity and temperature right away.

Hardware Overview

The module carries a low-cost, easy to use, highly accurate, digital temperature & humidity sensor, from MEAS Switzerland – HTU21D.

htu21d module hardware overview

The small size of the module allows it to be mounted into just about anything, such as thermostats, humidistats, indoor weather stations and similar devices, for monitoring and controlling temperature/humidity.

The HTU21D sensor is capable of reading humidity over the full range of 0 to 100% RH with a typical accuracy of ±2% over the range of 5% to 95% RH.

The maximum temperature range of the HTU21D is -40 to 125°C. It has a typical accuracy of ±0.3°C over the range of 0 to 70°C.

In its normal mode for humidity and temperature measurement, the sensor has a resolution of 0.7% RH and 0.040°C with conversion times of 2ms and 11ms, respectively. For more demanding requirements the sensor allows you to increase the resolution at the expense of increased conversion time. In maximum resolution mode the HTU21D can provide 0.04% RH with a conversion time of 14ms and 0.01°C with a conversion time of 44ms.

Warning:

The HTU21D sensor has a small window that exposes the polymer sensing film responsible for measuring temperature and humidity. It is advisable to prevent liquids, dust or other contaminants from coming into contact with it as it may affect the accuracy of the sensor.

Some breakout boards include the HTU21D-F (filtered version) which come equipped with a hydrophobic PTFE filter (a tiny white cover on the IC). This filter blocks contaminants but allows water vapor to pass through, keeping your sensor safe from water damage while still proving accurate sensor readings.

Power Requirement

The module comes with a 3.3V precise voltage regulator and voltage level translator, so you can use it with your favorite 3.3V or 5V microcontroller without worry.

htu21d module voltage regulator translator

The HTU21D consumes less than 0.5mA during measurements and less than 0.14µA during sleep mode. This low power consumption allow the implementation in battery driven devices such as handsets, wearables or smart watches.

I2C Interface

The HTU21D is a I2C sensor, meaning it uses the two I2C data/clock wires available on most microcontrollers, and can share those pins with other I2C sensors as long as they don’t have an address collision.

The sensor has a fixed I2C address and is set to 0x40HEX. A multiplexer/Mux is required to communicate to multiple HTU21D sensors on a single bus.

Technical Specifications

Here are the complete specifications:

Power supply3.3V to 5.5V
Current draw~0.5mA (during measurements)
~0.14µA (during sleep mode)
Minimum Sampling Period50ms
Humidity Range0 to 100 %RH
Humidity Accuracy±2% over the range of 5% to 95% RH
Temperature Range-40ËšC to +125ËšC
Temperature Accuracy±0.3˚C at 25°C

For more details, please refer below datasheet.

HTU21D Module Pinout

Now let’s have a look at the pinout.

htu21d module pinout

VCC is the power pin. You can connect it to 3.3V or 5V output from your Arduino.

GND is the common ground for power and logic.

SCL is also the I2C clock pin, connect to your microcontrollers I2C clock line.

SDA is also the I2C data pin, connect to your microcontrollers I2C data line.

Wiring up a HTU21D Module to an Arduino

Wiring up the HTU21D humidity sensor is very easy!

There are only four pins that need to be hooked up in order to start using the sensor. One for VCC, one for GND, and two data lines for I2C communication.

Connect the SCL pin to the I2C clock pin and the SDA pin to the I2C data pin on your Arduino. Note that each Arduino Board has different I2C pins which should be connected accordingly. On the Arduino boards with the R3 layout, the SDA (data line) and SCL (clock line) are on the pin headers close to the AREF pin. They are also known as A5 (SCL) and A4 (SDA).

The following illustration shows the wiring.

wiring connecting htu21d module with arduino

Library Installation

To get your sensor up and running, you will need to install the Adafruit HTU21DF. It is available from the Arduino library manager.

To install the library navigate to the Sketch > Include Library > Manage Libraries… Wait for Library Manager to download libraries index and update list of installed libraries.

manage libraries

Filter your search by typing ‘HTU21D‘ and install the library.

adafruit htu21d library installation

The Adafruit_HTU21DF library uses the Adafruit Bus IO helper library internally to abstract away I2C & SPI transactions and registers. So, search the library manager for adafruit bus and install that as well.

adafruit busio library installation

Arduino Code – Reading Temperature and Humidity

Below is a basic Arduino sketch. Go ahead and upload it to your Arduino. You will see the current temperature and humidity in your room!

#include <Wire.h>
#include "Adafruit_HTU21DF.h"

Adafruit_HTU21DF htu = Adafruit_HTU21DF();

void setup() {
  Serial.begin(9600);

  if (!htu.begin()) {
    Serial.println("Couldn't find sensor!");
    while (1);
  }
}

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);
}

Once your code is uploaded, open the serial terminal at 9600bps. You should see something like the output below. Try breathing on the sensor to see both humidity and temperature values change!

htu21d sensor arduino output

Code Explanation:

The code is quite straightforward. At the beginning, Wire.h and Adafruit_HTU21DF.h libraries are included and an Adafruit_HTU21DF object is created in the global space.

#include <Wire.h>
#include "Adafruit_HTU21DF.h"

Adafruit_HTU21DF htu = Adafruit_HTU21DF();

In the setup, we initialize the serial communication with PC and call the begin() function.

The htu.begin() function initializes the sensor. This function returns True if the sensor was found and responded correctly and False if it was not found.

void setup() {
  Serial.begin(9600);

  if (!htu.begin()) {
    Serial.println("Couldn't find sensor!");
    while (1);
  }
}

Once initialized, you can access object’s (htu) methods using the dot operator.

htu.readTemperature() returns floating point (decimal + fractional) temperature reading in °C. You can convert to Fahrenheit by multiplying by 1.8 and adding 32.

htu.readHumidity() returns humidity reading, also as a floating point value between 0 and 100 (this reads % humidity)

void loop() {
    float temp = htu.readTemperature();
    float rel_hum = htu.readHumidity();
    Serial.print("Temperature: "); Serial.print(temp); Serial.print(" C");
    Serial.print("\t");
    Serial.print("Humidity: "); Serial.print(rel_hum); Serial.println(" \%");
    delay(500);
}

Login
ADS CODE