Home Raspberry PI Raspberry Pi and Si7021 sensor java example

Raspberry Pi and Si7021 sensor java example

by shedboy71

In this example we will connect an SI7021 digital temperature 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

Here is a recap of the sensor

The Silicon Labs Si7006/13/20/21/34 devices are our latest generation of I2C relative humidity and temperature sensors. All members of this device family combine fully factory-calibrated humidity and temperature sensor elements with an analog to digital converter, signal processing, and an I2C host interface.  The Si7013/20/21/34 devices are designed for high-accuracy applications, while the Si7006 is targeted toward lower-accuracy applications traditionally using discrete RH/T sensors.

The innovative CMOS design of these devices offers the lowest power consumption in the industry for a relative humidity and temperature sensor. Patented use of industry-standard low-K polymer dielectrics provides excellent accuracy and long-term stability, along with low drift and low hysteresis.

 

You can read more – https://www.silabs.com/products/sensors/humidity/si7006-13-20-21-34

 

Parts List

Label Part Type
Part1 Adafruit Si7021 Temp Humidity
Raspberry Pi1 Raspberry Pi 2

Layout

 

pi and si7021

pi and si7021

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.
// SI7021
// This code is designed to work with the SI7021_I2CS I2C Mini Module available from ControlEverything.com.
// https://www.controleverything.com/content/Humidity?sku=SI7021_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 SI7021
{
	public static void main(String args[]) throws Exception
	{
		// Create I2C bus
		I2CBus bus = I2CFactory.getInstance(I2CBus.BUS_1);
		// Get I2C device, SI7021 I2C address is 0x40(64)
		I2CDevice device = bus.getDevice(0x40);
		
		// Send humidity measurement command
		device.write((byte)0xF5);
		Thread.sleep(300);
		
		// Read 2 bytes of humidity data, msb first
		byte[] data = new byte[2];
		device.read(data, 0, 2);
		
		// Convert humidity data
		double humidity = (((((data[0] & 0xFF) * 256) + (data[1] & 0xFF)) * 125.0) / 65536.0) - 6;
		
		// Send temperature measurement command
		device.write((byte)0xF3);
		Thread.sleep(300);
		
		// Read 2 bytes of temperature data, msb first
		device.read(data, 0, 2);
		
		// Convert temperature data
		double cTemp = (((((data[0] & 0xFF) * 256) + (data[1] & 0xFF)) * 175.72) / 65536.0) - 46.85;
		double fTemp = (cTemp * 1.8 ) + 32;
		
		// Output data to screen
		System.out.printf("Relative Humidity : %.2f %% %n", humidity);
		System.out.printf("Temperature in Celsius : %.2f C%n", cTemp);
		System.out.printf("Temperature in Fahrenheit : %.2f F%n", fTemp);
	}
}

[/codesyntax]

 

Now you have to compile and run the program like this

$> sudo pi4j SI7021.java

Testing

You should see the following

si7021 output

si7021 output

Links

Industrial High Precision Si7021 Humidity Sensor with I2C Interface for Arduino

You may also like