Introduction: Humidity and Temperature Measurement Using HTS221 and Raspberry Pi

About: We are a group of makers. We work in IoT, IOS app, android app, embedded design, sensor design, raspberry pi, arduino, beaglebone, particle electron, particle photon, Bluetooth.

HTS221 is an ultra compact capacitive digital sensor for relative humidity and temperature. It includes a sensing element and a mixed signal application specific integrated circuit(ASIC) to provide the measurement information through digital serial interfaces. Integrated with so many features this is one of the most appropriate sensors for critical humidity and temperature measurements.

In this tutorial the interfacing of the HTS221 sensor module with raspberry pi is demonstrated and its programming using python language has also been illustrated. To read the humidity and temperature values, we have used raspberry pi with an I2C adapter.This I2C adapter makes the connection to the sensor module easy and more reliable.

Step 1: Hardware Required:

The materials that we need for accomplishing our goal includes the following hardware components:

1. HTS221

2. Raspberry Pi

3. I2C Cable

4. I2C Shield for raspberry pi

5. Ethernet Cable

Step 2: Hardware Hookup:

The hardware hookup section basically explains the wiring connections required between the sensor and the raspberry pi. Ensuring correct connections is the basic necessity while working on any system for the desired output. So, the requisite connections are as follows:

The HTS221 will work over I2C . Here is the example wiring diagram, demonstrating how to wire up each interface of the sensor.

Out-of-the-box, the board is configured for an I2C interface, as such we recommend using this hookup if you’re otherwise agnostic.

All you need is four wires! Only four connections are required Vcc, Gnd, SCL and SDA pins and these are connected with the help of I2C cable.

These connections are demonstrated in the pictures above.

Step 3: Code for Humidity and Temperature Measurement:

The advantage of using raspberry pi is, that is provides you the flexibility of the programming language in which you want to program the board in order to interface the sensor with it. Harnessing this advantage of this board, we are demonstrating here its programming in the python. The python code for HTS221 can be downloaded from our github community that is Control Everything Community.

As well as for the ease of the users, we are explaining the code here also:

As the first step of coding you need to download the smbus library in case of python, because this library supports the functions used in the code. So, to download the library you can visit the following link:

You can copy the working python code for this sensor from here also:

import smbus

import time

# Get I2C bus

bus = smbus.SMBus(1)

# HTS221 address, 0x5F(95)

# Select average configuration register, 0x10(16)

# 0x1B(27) Temperature average samples = 256, Humidity average samples = 512

bus.write_byte_data(0x5F, 0x10, 0x1B)

# HTS221 address, 0x5F(95)

# Select control register1, 0x20(32)

# 0x85(133) Power ON, Continuous update, Data output rate = 1 Hz

bus.write_byte_data(0x5F, 0x20, 0x85)


# HTS221 address, 0x5F(95)

# Read Calibration values from non-volatile memory of the device

# Humidity Calibration values

# Read data back from 0x30(48), 1 byte

val = bus.read_byte_data(0x5F, 0x30)

H0 = val / 2 # Read data back from 0x31(49), 1 byte

val = bus.read_byte_data(0x5F, 0x31)

H1 = val /2

# Read data back from 0x36(54), 2 bytes

val0 = bus.read_byte_data(0x5F, 0x36)

val1 = bus.read_byte_data(0x5F, 0x37)

H2 = ((val1 & 0xFF) * 256) + (val0 & 0xFF)

# Read data back from 0x3A(58), 2 bytes

val0 = bus.read_byte_data(0x5F, 0x3A)

val1 = bus.read_byte_data(0x5F, 0x3B)

H3 = ((val1 & 0xFF) * 256) + (val0 & 0xFF)

# Temperature Calibration values

# Read data back from 0x32(50), 1 byte

T0 = bus.read_byte_data(0x5F, 0x32)

T0 = (T0 & 0xFF)

# Read data back from 0x32(51), 1 byte

T1 = bus.read_byte_data(0x5F, 0x33)

T1 = (T1 & 0xFF)

# Read data back from 0x35(53), 1 byte

raw = bus.read_byte_data(0x5F, 0x35)

raw = (raw & 0x0F)

# Convert the temperature Calibration values to 10-bits

T0 = ((raw & 0x03) * 256) + T0

T1 = ((raw & 0x0C) * 64) + T1

# Read data back from 0x3C(60), 2 bytes

val0 = bus.read_byte_data(0x5F, 0x3C)

val1 = bus.read_byte_data(0x5F, 0x3D)

T2 = ((val1 & 0xFF) * 256) + (val0 & 0xFF)

# Read data back from 0x3E(62), 2 bytes

val0 = bus.read_byte_data(0x5F, 0x3E)

val1 = bus.read_byte_data(0x5F, 0x3F)

T3 = ((val1 & 0xFF) * 256) + (val0 & 0xFF)

# Read data back from 0x28(40) with command register 0x80(128), 4 bytes

# humidity msb, humidity lsb, temp msb, temp lsb

data = bus.read_i2c_block_data(0x5F, 0x28 | 0x80, 4)

# Convert the data

humidity = (data[1] * 256) + data[0]

humidity = ((1.0 * H1) - (1.0 * H0)) * (1.0 * humidity - 1.0 * H2) / (1.0 * H3 - 1.0 * H2) + (1.0 * H0)

temp = (data[3] * 256) + data[2]

if temp > 32767 :

temp -= 65536

cTemp = ((T1 - T0) / 8.0) * (temp - T2) / (T3 - T2) + (T0 / 8.0)

fTemp = (cTemp * 1.8 ) + 32

# Output data to screen

print "Relative Humidity : %.2f %%" %humidity

print "Temperature in Celsius : %.2f C" %cTemp

print "Temperature in Fahrenheit : %.2f F" %fTemp

The part of code mentioned below includes the libraries required for the correct execution of the python codes.

import smbus

import time

The code can be executed by typing the below mentioned command in the command prompt.

$> python 

The output of the sensor is also shown in the picture above for the reference of the user.

Step 4: Applications:

HTS221 can be employed in various consumer products like air humidifiers and refrigerators etc. This sensor also find its application in a wider arena including Smart home automation, Industrial automation, respiratory equipments, asset and goods tracking.