HTU21DF TEMPERATURE AND HUMIDITY SENSOR

In this example we are going to connect from Raspberry Pi to HTU21DF temperature and humidity sensor.

(And at bottom there I tell of how to get it to work also on H3 based Orange Pi)

DISCLAIMER

We do not take any responsibility for possible errors in the guide or errors that you might do wiring it up. Incorrect wiring can result in damaged sensor or damaged Raspberry Pi.

This sensor measures temperature and humidity.

Main parameters from the sensors Data sheet:

This sensor is really small even the full brake-outboard is only size of a fingernail. The board does not come with the header pins soldered on, but with mine the header pins came with it at least so I just had to solder them on.

This sensor has I2C interface.

Pieces we use in this guide:

If you are not familiar with working with a breadboard or on how to place a cobbler on the breadboard then click here bellow

Breadboard

I2C Tools

To install the I2CTools you use the following command:

sudo apt-get install i2c-tools

You need to enable the i2c bus in the Raspberry PI settings, which you can do in the Raspberry Pi user interface.


Wiring up the board:

First we solder the header pins into the brake-out board if needed. It was possible on mine to either solder the pins on front or back.

Once the board has been soldered with header pins or wires then we are ready to wire up the sensor which we do as follows:

HTU21DF
Raspberry PI cobbler
VIN 3.3 V
GND GND
SCL SCL (Physical pin 5)
SDA SDA (Physical pin 3)
Raspberry Pi pin layout

If you need more information about the GPIO headers on Raspberry Pi or other boards then click bellow.

Raspberry Pi Pin guideOrange Pi One Pin guideOrange Pi Pin guide for most H3 modelsOrange Pi Zero 3 Pin  guideOrange Pi 5 Plus Pin guideRock 64 Pin guide

Sanity tests in console:

After you have connected everything and made sure that wiringPI, i2ctools are installed that I2C is enabled in the Raspberry PI settings, then we are ready to do sanity tests in console, or you can run the I2C example project that comes with the I2C Plugin.

If doing it in console then you would issue command like this:

sudo i2cdetect -y 1

This should show our device at 0x40. Or if you use the example project that comes with the I2C plugin then you click Get adapters and then select the adapter and then click Scan the selected adapter this should get you 64, which converted to Hex is 0x40, same as the i2cdetect returns. 

The example project that comes with the I2C Plugin, which can help you scan for the device address. I had BMP180 and BMP280  and the HTU21DF connected to the I2C bus at same time. Which is, which is why device was detected at 118 ,119 and 64. The HTU21DF is the one detected on 64 which is 0x40 hex. 

If you can see your sensor on address 0x40 (or 64 dec) then you are ready to start coding.

The Xojo code:

Coding against this sensor required small driver but I have made it really simple as I created a Xojo class called HTU21DF (see download
link at bottom) that does all the hard work. Using the class is as very easy:

Read temperature:

Sub Pressed() Handles Pressed
  #if TargetARM and TargetLinux
    try
      // We want adapter 1 and device 0x40
      var htu as new HTU21DF(1, &h40)
      
      MessageBox(Format(htu.ReadTemperature(),"#.00") + "° C")
      
    catch ex as I2CException
      MessageBox("Error: " + ex.Message)
    end try
    
  #endif
End Sub

Read humidity:

Sub Pressed() Handles Pressed
  #if TargetARM and TargetLinux
    try
      // We want adapter 1 and device 0x40
      var htu as new HTU21DF(1, &h40)
      
      MessageBox(Format(htu.ReadHumidity(),"#.00") + " %")
      
    catch ex as I2CException
      MessageBox("Error: " + ex.Message)
    end try
    
  #endif
End Sub

Getting the code

You can find this example code at Example code for GPIO guides.


I got this also to work on H3 based Orange Pi running Armbian.

To get things to work on H3 based Orange Pi I had to: