Part

Temperature and humidity, HTU21D

Temperature and humidity over I2C at 0x40 on the GY-21, hold and no-hold commands, a CRC on each reading, for Adafruit_HTU21DF.h.

See every part
  • 4 pins
  • 2 settings
  • In 1 project
Drawn live by the editor's own code, at the size you see it.
Temperature and humidity, HTU21D as drawn in the Mokxi simulator
At a glance

The Temperature and humidity, HTU21D in brief

What it is, and every setting the properties panel gives it.

The HTU21D is a temperature and humidity sensor from TE Connectivity, often seen on a GY-21 breakout board. It talks over I2C at the fixed address 0x40, and every reading comes back as two data bytes plus a CRC checksum. You ask for one reading at a time, wait while it measures, then read the bytes. The resolution is your choice, trading bits for speed. Mokxi simulates the part with two sliders, temperature in degrees Celsius and humidity in percent relative humidity, that you can move while the circuit runs. The next measurement reports the new values. The model also checks the CRC and returns NAN if a reading fails, and begin() fails with no answer at 0x40 if SDA, SCL or power are not connected. Clock stretching in Hold Master mode is not modeled, and the heater bit switches and reads back but does not warm the reading. One HTU21D per I2C bus, unless you add a switch like the TCA9548A.

Family
Sensors
Pins
4
temperature
22
humidity
45
Pins

4 pins

Named as the editor names them, so a wire lands where you expect it.

Pin
What it does
VIN
Supply, 3.3 or 5 V on the GY-21 (the chip itself runs from 1.5 to 3.6 V, and the board's regulator feeds it 3.3 V).
GND
Ground.
SCL
I2C clock. The board carries the pull-ups and level shifters.
SDA
I2C data.
How to use it

How to use the HTU21D temperature sensor with an Arduino

The built-in project HTU21D temperature and humidity has this part wired to an Arduino Uno. A GY-21 HTU21D board sits on the Uno's I2C pins and is read with the Adafruit_HTU21DF library. The project runs beside these words and opens in the editor as it is drawn here. Wiring: VIN to 5V, GND to GND, SDA to A4, SCL to A5.

The sketch asks for a temperature and a humidity reading every second and prints both on the serial monitor. With the default settings, temperature 22 and humidity 45, the serial monitor shows: "HTU21D found at 0x40" and then "temperature 21.99 C, humidity 45.0 %RH".

Move the sliders while it runs. After changing the settings to temperature 30 and humidity 70, the serial monitor shows: "HTU21D found at 0x40" and then "temperature 30.00 C, humidity 70.0 %RH". Only the numbers changed. The sensor was found the same way both times.

Things to try from the sketch: unplug SDA, and begin() gets no answer at 0x40, so the sketch says so. Then work out the dew point from the two readings.

Part pin
Goes to
VIN
Arduino Uno R3 pin 5V
GND
Arduino Uno R3 pin GND
SDA
Arduino Uno R3 pin A4
SCL
Arduino Uno R3 pin A5
HTU21D temperature and humiditylive0.000 s 0.00x
Click to open it in the editor
This is the simulator itself, running here. Click anything to open it in the editor.
sketch.ino · Arduino Uno R3
// An HTU21D temperature and humidity sensor on the GY-21 board.
//
// The HTU21D answers at 0x40. Each reading is a command, a wait while it
// measures (up to 50 ms for a 14-bit temperature, 16 ms for a 12-bit
// humidity) and three bytes back: two of data and a CRC that
// Adafruit_HTU21DF checks for you.
//
//   GY-21  VIN -> 5V, GND -> GND, SCL -> A5, SDA -> A4
//
// The board has its own 3.3 V regulator and pull-ups, so 5 V on VIN is fine.
// Every second the sketch prints both readings on the serial monitor. Move
// the temperature and humidity sliders while it runs.
//
// THINGS TO TRY. Unplug SDA: begin() gets no answer at 0x40 and the sketch
// says so. Then work out the dew point from the two readings.

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

Adafruit_HTU21DF htu = Adafruit_HTU21DF();

void setup() {
  Serial.begin(9600);
  if (!htu.begin()) {
    Serial.println("No HTU21D at 0x40: check SDA, SCL and power.");
    while (1) {}
  }
  Serial.println("HTU21D found at 0x40");
}

void loop() {
  float temp = htu.readTemperature();
  float humidity = htu.readHumidity();
  Serial.print("temperature ");
  Serial.print(temp, 2);
  Serial.print(" C, humidity ");
  Serial.print(humidity, 1);
  Serial.println(" %RH");
  delay(1000);
}
Common errors

Common Temperature and humidity, HTU21D errors and fixes

What goes wrong
The fix
begin() finds no sensor and prints a message.
Check SDA, SCL and power. The sensor answers only at address 0x40, so SDA and SCL must be wired right.
A reading comes back as NAN.
The CRC check failed or the wiring is loose. Make sure SDA, SCL, VIN and GND are all connected.
Read during a measurement is refused in No Hold mode.
Wait for the measurement to finish before reading. The sensor gives no ACK until it is done.
Only one HTU21D works on the bus.
The address is fixed at 0x40. Use an I2C switch such as the TCA9548A to add more sensors.
Questions

Questions and answers

what is the htu21d i2c address

It is fixed at 0x40. That means one HTU21D per I2C bus, unless you add an I2C switch like the TCA9548A.

can i power the gy-21 board with 5 volts

Yes. The board has its own 3.3 V regulator and pull-ups, so 5 V on VIN is fine.

which arduino library works with the htu21d

Use Adafruit's library. Mokxi ships it as Adafruit_HTU21DF.h. Call begin(), then readTemperature() and readHumidity().

can i change temperature and humidity while the simulation runs

Yes. The temperature and humidity sliders change them while the circuit runs, and the next measurement reports the new values.

How it is modeled

What is true about the Temperature and humidity, HTU21D, here

In its help article’s own words: what the simulation gets right, and where it stops.

Not modeled

Clock stretching in Hold Master mode: no simulated board waits for it, so 0xE3 and 0xE5 behave like their No Hold twins and a read during the measurement is refused. The heater's warming (the bit switches and reads back, but the reading stays the slider's), the diagnostic open and short circuit codes, and the accuracy figures (the real part is typically within 0.3 °C and 2 %RH). The datasheet does not say whether a result can be read twice; here it is read once.

From HTU21D temperature and humidity sensor, in full.

Projects

See the Temperature and humidity, HTU21D in a project

One built-in project uses it. It opens in the editor, already wired.

Boards

The boards it is shown on

This board has a built-in project with the Temperature and humidity, HTU21D in it.

Wire up the Temperature and humidity, HTU21D

Open the editor and push it into the breadboard. It is free, and it runs on your own machine.