Detect Temperature and Humidity With Raspberry Pi and HTU21 / SHT21

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

About: Software engineer and open source enthusiast

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

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

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

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

  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

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!

2 People Made This Project!

Recommendations

  • Make it Glow Contest

    Make it Glow Contest
  • First Time Author Contest

    First Time Author Contest
  • PCB Challenge

    PCB Challenge

18 Discussions

0
videomatic2015
videomatic2015

Question 6 months ago on Step 5

Hi, thanks.
can I use two or more sensors in a raspi? How to change the i2c device address?
Another question:
what about the maximun i2c distance?
thank you very much

0
leon-anavi
leon-anavi

Answer 6 months ago

Hi, thanks for the feedback. As far as I know from the HTU21D datasheet the I2C
address is 0x40 and you can't change it but you can use I2C multiplexer like TCA9548A to attach several of these sensors. The length of the wires depends on the hardware setup. In general short distances are recommended although I have read posts by people claiming they have managed to have I2C working with several meters of cables.

0
a_derain
a_derain

1 year ago

Hi, thanks a lot for this great project. Everything is fine for me, and the returning values are also correct. I only have one question about the datasheet, all the datasheets I can find on the Internet only contain 2 or 3 pages, so could you please share with the datasheet you referred to? Thank you very much.

0
a_derain
a_derain

Reply 1 year ago

Thanks a lot!

0
RobertB492
RobertB492

Question 1 year ago

I'm setting this up for measuring temperatur/humidity in a greenhouse. However, the test application seems to output values even though no sensor is attached?

0
leon-anavi
leon-anavi

Reply 1 year ago

Hm... check if any I2C device is present with i2cdetect.

0
RobertB492
RobertB492

Reply 1 year ago

No, as there is nothing connected, i2cdetect turns up zero devices. However, the line:
int fd = wiringPiI2CSetup(HTU21D_I2C_ADDR);
returns a file descriptor > 0. Not sure what it's supposed to do if there is no device connected at all...

0
RobertB492
RobertB492

Reply 1 year ago

Anyway, I've now connected the device and it is detected with i2cdetect and seems to work.

0
RobertB492
RobertB492

Reply 1 year ago

Alrite, it seems that the read(...) call does not return 3 when the device is disconnected, this can be used to detect connection.

0
dave02420
dave02420

2 years ago

I was getting weird (overly high) values, too, but it turned out to be a wiring mistake. I had mistakenly connected to the pi’s SCLK pin instead of the SCL pin. Once I fixed that, it worked like a charm. Thank you for posting this.

0
rilldeverill
rilldeverill

3 years ago

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?

0
leon-anavi
leon-anavi

Reply 3 years ago

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.

0
PeterV202
PeterV202

Reply 2 years ago

Hello

Something is wrong, I have the same problem

first sensor read T 44,36 Hum. 84,20
another sensor T 44,06 Hum. 86,23

in the room is T 22 Hum. 64

0
TarasF1
TarasF1

2 years ago

Hello.I have one question.What does this line do?

unsigned int temp = (buf [0] << 8 | buf [1]) & 0xFFFC;

0
leon-anavi
leon-anavi

Reply 2 years ago

Retrieves the temperatures according to the datasheet of the I2C sensor.

0
TarasF1
TarasF1

Reply 2 years ago

Thank you!