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:
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 DCUBE Store 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:
<p>import smbus</p><p>import time</p><p># Get I2C busbus = smbus.SMBus(1)</p><p># STS21 address, 0x4A(74)</p><p># Select Command</p><p># 0xF3(243) Temperature measurement in NO HOLD mode</p><p>bus.write_byte(0x4A, 0xF3)</p><p>time.sleep(0.5) </p><p># STS21 address, 0x4A(74)</p><p># Read data back, 2 bytes, MSB first</p><p>data0 = bus.read_byte(0x4A)</p><p>data1 = bus.read_byte(0x4A)</p><p># Convert the data</p><p>temp = (data0 * 256 + data1) & 0xFFFC</p><p>cTemp = -46.85 + (175.72 * temp / 65536.0)</p><p>fTemp = cTemp * 1.8 + 32</p><p># Output data to screen</p><p>print "Temperature in Celsius is : %.2f C" %cTemp</p><p>print "Temperature in Fahrenheit is : %.2f F" %fTemp </p>
The code is executed using the following command:
$> python STS21.pygt; python STS21.py
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.