Lesson 35: Using HTU21D Temperature Sensor with Arduino

Lesson 35: Using HTU21D Temperature Sensor with Arduino

This project guides you through using an Arduino to read temperature and humidity data from an HTU21D-F sensor. This sensor is inexpensive and widely available, making it perfect for a variety of projects. The sensor outputs data via I2C, a common digital communication protocol. Learning to use this sensor and I2C will open up many possibilities for your projects.

HTU21D module

Practical Applications:

  • Building a smart home environmental monitor
  • Creating a weather station
  • Developing a climate-controlled enclosure for sensitive electronics
  • Monitoring temperature and humidity in a greenhouse or terrarium

Hardware/Components

You will need the following components:

  • Arduino board (e.g., Uno, Nano)
  • HTU21D-F temperature and humidity sensor (in video at 00:22)
  • Connecting wires
  • Breadboard (optional, but recommended)

Wiring Guide

The wiring is straightforward. Connect the sensor to the Arduino as follows (in video at 02:28):

  • Sensor VCC to Arduino 3.3V
  • Sensor GND to Arduino GND
  • Sensor SDA to Arduino A4
  • Sensor SCL to Arduino A5

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

Code Explanation

The provided code uses the Adafruit HTU21D-F library (in video at 03:03) to interact with the sensor. The key parts are:


// These are the lines that you can configure:
const int inPin =A0;//can change
const int iteration = 1000; //can change (see video at 00:22)
const float LM35_FACTOR =0.01;// do not change

inPin: Specifies the analog pin connected to the temperature sensor. You can change this to suit your wiring.

iteration: This variable controls the number of readings taken to average the temperature. Increasing it improves accuracy but slows down the readings (in video at 00:22).

The getHTU() function (in video at 08:46) allows you to retrieve the temperature in Celsius, Fahrenheit, or Kelvin, or the humidity, by passing a character ('C', 'F', 'K', or 'H') as an argument. The code then uses this function to display the data on the serial monitor.


float getTemperature(char type) {
  // ... (code to read temperature and humidity) ...
  if (type == 'F') {
    value = averageTemperature * 9 / 5 + 32; // Fahrenheit
  } else if (type == 'K') {
    value = averageTemperature + 273.15; // Kelvin
  } else {
    value = averageTemperature; // Celsius
  }
  return value;
}

Live Project/Demonstration

The video demonstrates the sensor's functionality (in video at 07:34). The serial monitor displays the temperature and humidity readings. The presenter also shows how the code can trigger actions based on temperature thresholds (in video at 06:45).

Chapters

  • [00:00] Introduction
  • [00:22] Sensor Overview
  • [01:22] Datasheet Review
  • [02:28] Wiring
  • [03:03] Library Installation
  • [04:05] Code Explanation (Part 1)
  • [08:33] Code Explanation (Part 2)
  • [07:34] Live Demonstration

Images

thumb_robojax_HTU21DF_types-1756423961-8524
thumb_robojax_HTU21DF_types-1756423961-8524
Arduino wiring for HTU21DF light intesity sensor
Arduino wiring for HTU21DF light intesity sensor
HTU21D module
HTU21D module
HTU21D module-back
HTU21D module-back
503-Lesson 42: Using an LM35 Temperature Sensor with Arduino
Language: C++
Copied!

Things you might need

Resources & references

Files📁

Other files