Raspberry Pi STS21 Temperature Sensor Java Tutorial
by Dcube Tech Ventures in Circuits > Electronics
13 Views, 0 Favorites, 0 Comments
Raspberry Pi STS21 Temperature Sensor Java Tutorial
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. Here is its demonstration of interfacing it with Raspberry Pi using Java code.
What You Need..!!
Connections:
Take an I2C shield for raspberry pi and gently push it over the gpio pins of raspberry pi.
Then connect the one end of I2C cable to STS21 sensor and the other end to the I2C shield.
Also connect the Ethernet cable to the pi or you can use a WiFi module.
Connections are shown in the picture above.
Code:
The java code for STS21 can be downloaded from our GitHub repository- Dcube Store.
Here is the link for the same :
https://github.com/DcubeTechVentures/STS21/blob/master/Java/STS21.java
We have used pi4j library for java code
http://pi4j.com/install.html
You can also copy the code from here, it is given as follows:
// Distributed with a free-will license.
// Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
// STS21
// This code is designed to work with the STS21_I2CS I2C Mini Module available in Dcube Store.
import com.pi4j.io.i2c.I2CBus;
import com.pi4j.io.i2c.I2CDevice;
import com.pi4j.io.i2c.I2CFactory;
import java.io.IOException;
public class STS21
{
public static void main(String args[]) throws Exception
{
// Create I2C bus
I2CBus bus = I2CFactory.getInstance(I2CBus.BUS_1);
// Get I2C device, STS21 I2C address is 0x4A(74)
I2CDevice device = bus.getDevice(0x4A);
// Write Temperature measurement trigger with NO HOLD mode
device.write((byte)0xF3);
Thread.sleep(300);
// Read 2 bytes of data, msb first
byte[] data = new byte[2];
device.read(data, 0, 2);
// Convert data
int temp = ((data[0] & 0xFF) * 256) + (data[1] & 0xFF);
double cTemp = -46.85 + (175.72 * temp) / 65536.0;
double fTemp = (cTemp * 1.8) + 32;
// Output to screen
System.out.printf("Temperature in Celsius is : %.2f C %n", cTemp);
System.out.printf("Temperature in Fahrenheit is : %.2f F %n", fTemp);
}
}
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.