Introduction: Detect Temperature and Humidity With Raspberry Pi and HTU21 / SHT21

Wondering what's the weather? Let's check it out with your Raspberry Pi! This tutorial provides step by step instructions how to detect temperature and humidity with the dirt cheap I2C sensor HTU21D and open source code on your Raspberry Pi.

If the embedded video does not appear on your mobile device, here is an alternative link. If you like this tutorial, please subscribe to my YouTube channel, give a star to the project in GitHub and have a look at my Instructables and videos.

Step 1: Getting Ready

Picture of Getting Ready

For this tutorial you need the following hardware components:

  • Raspberry Pi (any model or version)
  • HTU21 module
  • Breadboard
  • Male to female jumper wires (x4)
  • microSD card with Raspian GNU/Linux distribution
  • Power supply

HTU21 (or SHT21) modules are popular for hobby electronics. In general they are available for less than 5 USD per unit.

I have a couple modules here. They have different colors but the sensor is actually absolutely the same on both modules. Beware that the other physical difference is the position of the lines for data and clock. I am going to use the blue HTU21D module for the rest of the tutorial.

Step 2: Wiring

Picture of Wiring

The wiring is simple and takes less than a minute. As shown in the video, using male to female jumper wires connect VIN, GND, SCL and SDA from HTU21D to pins 1, 9, 5, 3 of Raspberry Pi.

Step 3: Installing Software

Picture of Installing Software

Boot your Raspberry Pi with Raspbian GNU/Linux distribution, open a terminal or login via SSH. Execute the following commands to download and install the required packages:

sudo apt-get update
sudo apt-get install git build-essential i2c-tools

Step 4: Enabling I2C

Picture of Enabling I2C
  1. Type the following command in a terminal:
    ​sudo raspi-config
    
  2. Select Advanced Options > I2C
  3. Follow the on screen instructions and reboot Raspberry Pi to compete this step.

Step 5: Reading Temperature and Humidity

Picture of Reading Temperature and Humidity

I have published free and open source example for reading temperature and humidity on Raspberry Pi using HTU21D. They are available in GitHub. Perform the following steps to use it:

  • Get the source code from GitHub
    git clone https://github.com/leon-anavi/rpi-examples.git
  • Build the example:
    cd rpi-examples
    cd HTU21D/c
    make
  • Run the application:
    sudo ./HTU21D_test
  • The temperature and the humidity will be printed on the screen
  • Enjoy!

Comments

rilldeverill (author)2017-03-09

Thanks for the project. I have mine working but it is returning values of around 41 Deg C and RH of 78% these values are wildly wrong almost double anyone else see this?

leon-anavi (author)rilldeverill2017-03-09

You are welcome. I am glad that the tutorial is useful. I am sorry that you have an issue with your sensor. Double check that you are using properly the examples for reading the data from the sensor. Also please consider checking with another sensor because something might be wrong with the hardware in your sensor.

joshuacarroll made it! (author)2016-07-02

Thanks for writing this! I was able to use this to test my temperature and humidity sensor.

I followed all of the steps, but when I ran the make command, I was getting the following error message:

pi@raspberrypi:~/rpi-examples/HTU21D/c $ sudo make
gcc -c -o HTU21D.o HTU21D.c -I.
HTU21D.c:3:22: fatal error: wiringPi.h: No such file or directory

#include "wiringPi.h"
^
compilation terminated.
Makefile:8: recipe for target 'HTU21D.o' failed
make: *** [HTU21D.o] Error 1

To get around this, you need to run

sudo apt-get install git wiringPi

or simply include it in the command from Step 3 like this:

sudo apt-get install git build-essential i2c-tools wiringPi
leon-anavi (author)joshuacarroll2016-07-03

Hi, sorry for the inconvenience. You are missing the library wiringPi. Please follow these instructions and install wiringPi: http://wiringpi.com/download-and-install/