HTU21D Humidity Sensor

temperature modules humidity

Interested in sensing environmental data? Let's have a look at another humidity and temperature sensor: the HTU21D.

HTU21D Humidity Sensor

Last week, we looked at the DTH11 and the DHT22 humidity and temperature sensors. This time we will look at a different, much smaller digital temperature and humidity sensor: the HTU21D. The HTU21D is manufactured by TE Connectivity and is not only smaller, but also a bit more accurate than the DHT sensors. It features ± 0.3 °C temperature accuracy under normal operating conditions and a typical accuracy of ± 2 % for the relative humidity. The best results can be acquired in the temperature range from 5 °C to 60 °C and a humidity range between 20 % and 80 % relative humidity. The sensor itself, can operate from -40 °C to 125 °C, however. Its maximum humidity range is from 0 % to 100 % relative humidity. Above 80 °C, however, high humidities might damage the sensor.

As you can see this makes the sensor a perfect fit for indoor applications just like the DHT modules. Compared to the DHT11 and the DHT22, the HTU21D performs a lot faster and has an adjustable resolution to further increase the measuring speed or reduce the energy consumption. For most Arduino projects these differences are no big deal anyway, however. What makes this sensor interesting is its size and the improved accuracy. In this tutorial we will look at the GY-21 module with a HTU21D sensor on it. It is only 10 x 13 mm in size. There are variations of the GY-21 module, that use the pin-to-pin compatible SHT21 or Si7021 sensors. These are almost identical, but use a different timing and the temperature correction formula that is applied to the HTU21D humidity readings is not documented for them. For this reason there is no guarantee that the code from this tutorial will work with these sensors and that the calculated temperature and humidity values are correct.

Connecting the Sensor

The HTU21D uses the I2C bus to transmit its data. I2C is very common serial bus for inter IC communication and supported by most microcontrollers and of course also by the Arduino Uno. It uses a data line (SDA) and a clock line (SCL). These pins need to be connected to the respective pins of the Arduino. This means for connecting this module just connect SDA to SDA and SCL to SCL of the Arduino. Besides that the module needs to be powered, for this connect VIN to 5V and GND to GND. This is shown in the picture below.

Connecting the HTU21D Module to the Arduino Uno

Arduino Library

For communicating with the sensor we need an Arduino Library. There are different libraries available for the HTU21 and the similar SHT21 and Si7021 sensors. The libraries from Sparkfun and Adafruit can be found in the Library Manager and can be recommended as they are easy to use. The problem is that neither of them implements the temperature correction formula for the humidity reading. Adafruit's library does not allow to change the resolution either. There is another library from the sensor's manufacturer, but it is hard to use. For this reason I decided to implement my own library for the purpose of this tutorial. You can find the download link below. The ZIP archive can be imported into the Arduino IDE by clicking Sketch > Include Library > Add .ZIP Library .... If you have any issues with the library, report them on GitHub. You can also find the source code for the library there.

Example Code

Let's have a look at the example code. It measures temperature and humidity every 5 seconds. To use the library you need to include the HTU21.h header file. You can then create a sensor instance like this:

#include <HTU21D.h>
HTU21D sensor;

You need to initialize the I2C communication in the setup procedure by calling sensor.begin(). In the main loop we can use the method measure to conduct a measurement and read back the result. If the measurement was conducted successfully this method returns true. If the connection to the sensor is faulty and the measurement data got corrupted it returns false. After a successful measurement you can access the result by calling getTemperature and getHumidity.

#include <HTU21D.h>

HTU21D sensor;

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

void loop() {
  if(sensor.measure()) {
    float temperature = sensor.getTemperature();
    float humidity = sensor.getHumidity();
    
    Serial.print("Temperature (°C): ");
    Serial.println(temperature);
    
    Serial.print("Humidity (%RH): ");
    Serial.println(humidity);
  }

  delay(5000);
}

If you upload the code to the Arduino, you can see the measurement values getting printed out in the serial console like this:

Temperature (°C): 20.86
Humidity (%RH): 58.35

If you want to change the resolution call the setResolution method in setup:

void setup() {
  Serial.begin(9600);
  sensor.begin();
  sensor.setResolution(RESOLUTION_RH12_T14);
}

In most cases you don't really want to change the resolution as the default setting is the best resolution of 14 bits for temperature and 12 bits for the humidity reading. Nevertheless, here are the available resolutions:

Temperature Humidity Parameter for setResolution
14 bit 12 bit RESOLUTION_RH12_T14
12 bit 8 bit RESOLUTION_RH8_T12
13 bit 10 bit RESOLUTION_RH10_T13
11 bit 11 bit RESOLUTION_RH11_T11

References

Previous Post Next Post