Lesson 36: Using the HTU21D Temperature Sensor with an LCD Arduino Step-by-Step Course

Lesson 36: Using the HTU21D Temperature Sensor with an LCD Arduino Step-by-Step Course

The HTU21D temperature and humidity sensor is a popular choice for many Arduino projects due to its ease of use and accuracy. In this lesson, we will build a simple project that reads temperature and humidity data from the sensor and displays it on an LCD screen. By the end of this tutorial, you will have a working setup that continuously displays the temperature in Celsius, Fahrenheit, and Kelvin, as well as the relative humidity percentage. For further clarification, you can refer to the video (in video at mm:ss).

Hardware Explained

The primary component of this project is the HTU21D temperature and humidity sensor, which communicates via I2C. This sensor operates within a voltage range of 1.5 to 3.6 volts, making it suitable for both 3.3V and 5V systems. It consumes very little power, typically only 0.02 microamperes when idle and 450 microamperes during measurement. In addition to the HTU21D, we will also use an LCD display, specifically the LCD1602 with I2C. This display allows for easy output of text data and requires only two pins for communication: SDA (data line) and SCL (clock line). The combination of these components will enable us to create an informative display for monitoring temperature and humidity levels.

Datasheet Details

ManufacturerTE Connectivity
Part numberHTU21D-F
Logic/IO voltage1.5 - 3.6 V
Supply voltage3.3 V (typ.)
Output current (typ.)0.02 µA (idle), 450 µA (measurement)
Temperature range-40 to +125 °C
Humidity range0 to 100 %RH
Resolution (Temperature)0.01 °C
Resolution (Humidity)0.04 %RH
PackageDFN-6

  • Use a pull-up resistor for SDA and SCL lines if not integrated.
  • Ensure correct power supply voltage to avoid damaging the sensor.
  • Maintain proper wiring to prevent communication errors.
  • Check the I2C address if the sensor does not respond.
  • Use a delay between readings to prevent sensor overload.
  • Ensure the LCD is compatible with I2C communication.

Wiring Instructions

To wire the HTU21D sensor and the LCD display, start with the power connections. Connect the left pin of the HTU21D to the 3.3V power source, and the second pin (often red) to the ground. Next, connect the SDA pin of the HTU21D to the Arduino's analog pin A4, and the SCL pin to analog pin A5. For the LCD1602 display, connect the VCC pin to the same 3.3V power source, and connect the GND pin to the ground. The SDA pin on the LCD should also connect to A4, and the SCL pin should connect to A5, allowing both components to share the I2C bus. Ensure all connections are secure to facilitate proper communication between the Arduino, sensor, and display.

Code Examples & Walkthrough

The following code initializes the HTU21D sensor and the LCD display. In the setup function, the LCD is prepared for use and the sensor is checked for connectivity:
void setup() {
  lcd.begin();
  lcd.backlight();
  if (!htu.begin()) {
      lcd.print("Robojax HTUD1DF");
      lcd.setCursor(0,1);
      lcd.print("sensor missing"); 
      while (1);
  } else {
      lcd.print("Robojax HTUD1DF");
      lcd.setCursor(0,1);
      lcd.print("Demo"); 
  }
  delay(2000);   
}
This excerpt checks if the sensor is connected properly. If not, it displays an error message on the LCD and halts the program. If the sensor is functioning, it shows a demo message for two seconds. The loop function is where the main reading and display take place. Here, we call the `lcdDisplay` function to show the temperature in different units:
void loop() {
   lcd.clear(); // clear previous values from screen
   lcdDisplay(0, 0, "Celsius: ", 10, 0, getHTU('C'), 'd');  
   lcdDisplay(0, 1, "Fahrenheit: ", 10, 1, getHTU('F'), 'd');     
   delay(5000);
}
In this loop, the LCD is cleared, and temperature readings in Celsius and Fahrenheit are displayed. The `getHTU` function is called with the character 'C' for Celsius and 'F' for Fahrenheit, respectively. Finally, the `getHTU` function is defined to read the temperature or humidity based on the input character:
float getHTU(char type) {
   float temp = htu.readTemperature();
   float rel_hum = htu.readHumidity();
   if(type =='F') {
       return temp * 9/5 + 32; // convert to Fahrenheit 
   } else if(type =='K') {
       return temp + 273.15; // convert to Kelvin
   } else if(type =='H') {
       return rel_hum; // return relative humidity
   } else {
       return temp; // return Celsius
   }
}
This function reads the temperature and humidity from the sensor and converts the temperature to the requested unit. Be sure to check the complete code loaded below the article for additional details.

Demonstration / What to Expect

Upon completing the wiring and uploading the code, you should see the temperature and humidity values displayed on the LCD. The readings will update every few seconds, reflecting the current conditions. If you apply heat to the sensor, you should notice the temperature rising accordingly, while the humidity should decrease slightly. Be mindful of the maximum temperature limit of the sensor; exceeding this may result in inaccurate readings or sensor failure (in video at mm:ss).

Video Timestamps

  • 00:00 - Introduction to the project
  • 01:15 - Wiring instructions
  • 03:30 - Code walkthrough
  • 10:00 - Demonstration of the setup
511-Lesson 36: Using the HTU21D Temperature Sensor with an LCD and Arduino: A Step-by-Step Course
Language: C++
Copied!

Things you might need

Files📁

Datasheet (pdf)