Raspberry Pi - SI7021 Humidiy & Temperature Sensor Java Tutorial
by Dcube Tech Ventures in Circuits > Electronics
26 Views, 0 Favorites, 0 Comments
Raspberry Pi - SI7021 Humidiy & Temperature Sensor Java Tutorial
SI7021 is a humidity and temperature sensor which can operate on I2C communication protocol. It has a monolithic CMOS IC integrating humidity and temperature sensor elements, an analog-to-digital converter etc. Incorporated with highly advanced signal processing elements and callibration data feature this sensor forms one of the most efficient device to measure humidity and temperature. Here is its demonstration 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 SI7021 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 SI7021 can be downloaded from our github repository-Dcube Store
Here is the link for the same :
https://github.com/DcubeTechVentures/SI7021....
We have used pi4j library for java code, the steps to install pi4j on raspberry pi is described here:
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.
// SI7021
// This code is designed to work with the SI7021_I2CS I2C Mini Module available from 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 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);
}
}
Applications:
SI7021 is a highly efficient and reliable sensor that can be incorporated in indoor weather stations that can maintain the optimum indoor environment in terms of humidity and temperature suited for human body.It forms an integral part in automotive climate control and defogging systems. It can also be employed in mobile phones and tablets.