Introduction: Temperature Measurement Using STS21 and Raspberry Pi

STS21 Digital Temperature Sensor offers superior performance and a space saving footprint. It provides calibrated, linearized signals in digital, I2C format. Fabrication of this sensor is based on CMOSens technology, which attributes to the superior performance and reliability of STS21. The resolution of STS21 can be changed by command, low battery can be detected and a checksum helps to improve communication reliability.

In this tutorial the interfacing of the STS21 sensor module with raspberry pi is demonstrated and its programming using python language has also been illustrated. To read the 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. STS21

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 STS21 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 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. Python is one of the easiest programming languages with easiest syntax. The python code for STS21 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 code from here also:

import smbus

import time

# Get I2C busbus = smbus.SMBus(1)

# STS21 address, 0x4A(74)

# Select Command

# 0xF3(243) Temperature measurement in NO HOLD mode

bus.write_byte(0x4A, 0xF3)


# STS21 address, 0x4A(74)

# Read data back, 2 bytes, MSB first

data0 = bus.read_byte(0x4A)

data1 = bus.read_byte(0x4A)

# Convert the data

temp = (data0 * 256 + data1) & 0xFFFC

cTemp = -46.85 + (175.72 * temp / 65536.0)

fTemp = cTemp * 1.8 + 32

# Output data to screen

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

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

The code is executed using the following command:

$> python

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

Step 4: Applications:

STS21 Digital Temperature Sensor can be employed in systems which require high accuracy temperature monitoring. It can be incorporated in various computer equipments, medical equipments and industrial control systems with the requisite of temperature measurement with proficient accuracy.