Aliases: HTU21D-F, HTU21DF, HTU21, GY-21. A cheap, common climate sensor from TE Connectivity (it was Measurement Specialties). You ask for one reading at a time, wait while it measures, then read two bytes of data and a CRC. The resolution is yours to pick, trading bits for speed.
Pins
| 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. |
The address is fixed at 0x40: one HTU21D per I2C bus, or an I2C switch such as the
TCA9548A for more.
Properties
temperature (degrees C, -40 to 125) and humidity (%RH, 0 to 100) are the weather;
the two sliders change them while the circuit runs, and the next measurement reports them.
Reading it on an Arduino
With Adafruit's library, which Mokxi ships as Adafruit_HTU21DF.h:
#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() {
Serial.print(htu.readTemperature());
Serial.print(" C ");
Serial.print(htu.readHumidity());
Serial.println(" %");
delay(1000);
}
begin() soft-resets the chip and checks its user register reads 0x02, the value
after reset. Each read sends a command, waits the measuring time out and checks the CRC;
a reading that fails comes back as NAN.
How it talks
One-byte commands, from the HTU21D(F) datasheet:
- Power on and soft reset (
0xFE) take up to 15 ms; until then the address is not acknowledged. A soft reset restores the defaults but keeps the heater bit. - Measure temperature with
0xE3(Hold Master) or0xF3(No Hold Master), humidity with0xE5or0xF5. In No Hold mode the sensor refuses a read header (no ACK) until the measurement is done. - Read three bytes: data MSB, data LSB, CRC. The master may stop after two and skip
the CRC. The two lowest bits are status: bit 1 is
0for a temperature and1for a humidity. Clear them before converting. - CRC-8: polynomial x^8 + x^5 + x^4 + 1 (
0x131), starting from0x00. The sheet's example:0x683Agives0x7C. - Convert:
RH = -6 + 125 x S / 65536andT = -46.85 + 175.72 x S / 65536. - User register:
0xE7reads it,0xE6writes it. Bits 7 and 0 set the resolution, bit 6 is End of Battery (set when the supply is under 2.25 V, updated after each measurement), bit 2 turns on the heater, bit 1 disables OTP reload. Bits 3 to 5 are reserved: read the register first and write them back unchanged.
| Bits 7, 0 | Humidity | Temperature | Longest measurement (RH, T) |
|---|---|---|---|
00 (default) |
12 bits | 14 bits | 16 ms, 50 ms |
01 |
8 bits | 12 bits | 3 ms, 13 ms |
10 |
10 bits | 13 bits | 5 ms, 25 ms |
11 |
11 bits | 11 bits | 8 ms, 7 ms |
What it does not model
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.
Common mistakes
Reading straight after the command: the result is not there for up to 50 ms, and the sensor refuses the read until it is. Forgetting to clear the status bits before converting, which adds up to 0.008 °C or 0.004 %RH. Putting two on one bus: the address is fixed. Mixing it up with the SHT21 or Si7021, which share its address and much of its command set but are different parts with their own libraries.