Lesson 35-1: Using the HTU21D Temperature Sensor

Lesson 35-1: Using the HTU21D Temperature Sensor

In this tutorial, we will explore how to use the HTU21D temperature and humidity sensor with an Arduino. The HTU21D is a compact sensor that communicates over I2C, making it easy to interface with microcontrollers. By the end of this lesson, you will be able to read temperature and humidity values and display them through the Arduino serial monitor.

HTU21D module

For this project, we will utilize the Adafruit library specifically designed for the HTU21D sensor. The library simplifies the process of reading sensor data, allowing us to focus on integrating the sensor into our projects. You can expect to see the sensor's temperature readings in Celsius, Fahrenheit, and Kelvin, as well as the relative humidity percentage (in video at 03:15).

Hardware Explained

The main component for this project is the HTU21D temperature and humidity sensor. This sensor operates on a supply voltage of 1.5 to 3.6 volts and consumes very little power, making it ideal for battery-operated devices. It communicates with the Arduino using I2C protocol, requiring only two data lines: SDA for data and SCL for the clock.

To power the sensor, we will connect it to either a 3.3V or 5V source, depending on the specific module you are using. The Adafruit version includes a voltage regulator, allowing it to work seamlessly with both voltage levels. This flexibility makes it a great choice for various applications.

Datasheet Details

ManufacturerTE Connectivity
Part numberHTU21D-F
Logic/IO voltage1.5 - 3.6 V
Supply voltage3.3 V (typ.)
Current consumption (idle)0.02 µA
Current consumption (measurement)450 µA (typ.)
Temperature range-40 to +125 °C
Humidity resolution0.04 %
Package6-pin DFN

  • Ensure proper voltage levels to avoid damaging the sensor.
  • Use pull-up resistors on the I2C lines if not included in the module.
  • Keep wires short to minimize interference with I2C communication.
  • Consider heat-sinking if using in high-temperature environments.
  • Check the sensor's orientation to ensure correct pin connections.

Wiring Instructions

Arduino wiring for HTU21DF light intesity sensor
Arduino wiring for HTU21DF light intesity sensor

To wire the HTU21D sensor to your Arduino, start by connecting the sensor's VCC pin to the 3.3V power pin on the Arduino. Next, connect the GND pin on the sensor to the ground (GND) pin on the Arduino. Then, connect the SDA pin on the sensor to the Arduino's analog pin A4, which serves as the I2C data line. Finally, connect the SCL pin on the sensor to analog pin A5, which acts as the I2C clock line. Make sure your connections are secure to avoid intermittent readings.

For clarity, if you are using a different board, ensure you identify the correct pins for SDA and SCL as they may vary. This wiring setup will allow the Arduino to communicate with the HTU21D sensor effectively (in video at 05:00).

Code Examples & Walkthrough

Below is an excerpt from the setup function that initializes the serial communication and checks if the sensor is found:

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

This code begins serial communication at a baud rate of 9600 and attempts to initialize the HTU21D sensor. If the sensor is not detected, it will print an error message and halt further execution.

The main loop of the program continuously reads the temperature and humidity values:

void loop() {
    Serial.print(getHTU('C'));
    Serial.println("C");
    Serial.print("Humidity:");
    Serial.print(getHTU('H'));
    Serial.println("%");
    delay(1000);
}

In this loop, we call the function getHTU with 'C' to get the temperature in Celsius and 'H' to get the humidity. The results are printed to the serial monitor every second. This allows for real-time observation of the sensor readings.

Additionally, we have a function getHTU that returns the temperature or humidity based on a character input:

float getHTU(char type) {
    float temp = htu.readTemperature();
    float rel_hum = htu.readHumidity();
    return (type == 'H') ? rel_hum : temp;
}

This function reads the temperature and humidity from the sensor and returns the appropriate value based on the type specified. This modular approach keeps the code clean and easy to maintain.

Demonstration / What to Expect

Upon successful setup and execution of the code, you should see the temperature and humidity values printed to the serial monitor. If you apply heat to the sensor, the temperature readings should increase accordingly, while humidity may decrease. If the temperature exceeds the sensor's maximum range, it may display unexpected results, such as zero (in video at 12:00).

Video Timestamps

  • 00:00 - Introduction
  • 03:15 - Sensor overview
  • 05:00 - Wiring instructions
  • 10:00 - Code walkthrough
  • 12:00 - Demonstration

Images

Arduino wiring for HTU21DF light intesity sensor
Arduino wiring for HTU21DF light intesity sensor
HTU21D module
HTU21D module
HTU21D module-back
HTU21D module-back
512-Lesson 35: Using HTU21D Temperature Sensor | Arduino Step-by-Step Course
Language: C++
Copied!

Things you might need

Files📁

Datasheet (pdf)