In this example we will connect an SHT31 humidity sensor to a Raspberry Pi and use Java to display the temperature readings, there are other languages such as C and python which work as well but I wanted to try something a little different
The new digital SHT3x humidity sensor series takes sensor technology to a new level. As the successor of the SHT2x series it is determined to set the next industry standard in humidity sensing. The SHT3x humidity sensor series consists of a low-cost version with the SHT30 humidity sensor, a standard version with the SHT31 humidity sensor, and a high-end version with the SHT35 humidity sensor. The SHT3x humidity sensor series combines multiple functions and various interfaces (I2C, analog voltage output) with a applications-friendly, very wide operating voltage range (2.4 to 5.5 V). The SHT3x humidity sensor is available in both large and small volumes.
The SHT3x builds on a completely new and optimized CMOSens® chip, which allows for increased reliability and improved accuracy specifications. The SHT3x offers a range of new features, such as enhanced signal processing, two distinctive and user-selectable I2C addresses, an alert mode with programmable humidity and temperature limits, and communication speeds of up to 1 MHz.
You can read more – https://www.sensirion.com/en/environmental-sensors/humidity-sensors/digital-humidity-sensors-for-various-applications/
Parts List
| Label | Part Type | |
|---|---|---|
| Part2 | Adafruit SHT31-D Sensirion Temperature/Humidity Sensor | |
| Raspberry Pi1 | Raspberry Pi 2 |
Layout
Code
This time we explore the world of Java on the Raspberry Pi
First you need to install PI4j – http://pi4j.com/install.html . I’ll sum it up as its easy to install from a terminal
The simplest method to install Pi4J on your RaspberryPi is to execute the following command directly on your RaspberryPi.
curl -s get.pi4j.com | sudo bash
Now for the java code – this is courtesy of a controleverything example
[codesyntax lang=”java”]
// 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.
// SHT31
// This code is designed to work with the SHT31_I2CS I2C Mini Module available from ControlEverything.com.
// https://www.controleverything.com/content/Humidity?sku=SHT31_I2CS#tabs-0-product_tabset-2
import com.pi4j.io.i2c.I2CBus;
import com.pi4j.io.i2c.I2CDevice;
import com.pi4j.io.i2c.I2CFactory;
import java.io.IOException;
public class SHT31
{
public static void main(String args[]) throws Exception
{
// Create I2CBus
I2CBus bus = I2CFactory.getInstance(I2CBus.BUS_1);
// Get I2C device, SHT31 I2C address is 0x44(68)
I2CDevice device = bus.getDevice(0x44);
// Send high repeatability measurement command
// Command msb, command lsb
byte[] config = new byte[2];
config[0] = (byte)0x2C;
config[1] = (byte)0x06;
device.write(config, 0, 2);
Thread.sleep(500);
// Read 6 bytes of data
// temp msb, temp lsb, temp CRC, humidity msb, humidity lsb, humidity CRC
byte[] data = new byte[6];
device.read(data, 0, 6);
//Convert the data
double cTemp = ((((data[0] & 0xFF) * 256) + (data[1] & 0xFF)) * 175.0) / 65535.0 - 45.0;
double fTemp = ((((data[0] & 0xFF) * 256) + (data[1] & 0xFF)) * 315.0) / 65535.0 - 49.0;
double humidity = ((((data[3] & 0xFF) * 256) + (data[4] & 0xFF)) * 100.0) / 65535.0;
//Output data to screen
System.out.printf("Temperature in Celsius : %.2f C %n", cTemp);
System.out.printf("Temperature in Fahrenheit : %.2f F %n", fTemp);
System.out.printf("Relative Humidity is : %.2f %%RH %n", humidity);
}
}
[/codesyntax]
Now you have to compile and run the program like this
$> sudo pi4j SHT31.java
Testing
You should see the following
Links
1PCS/LOT SHT31 Temperature & SHT31-D Humidity Sensor module Breakout Weather for Arduino


