Introduction: Humidity and Temperature Measurement Using HIH6130 and Raspberry Pi

About: We are a group of makers. We work in IoT, IOS app, android app, embedded design, sensor design, raspberry pi, arduino, beaglebone, particle electron, particle photon, Bluetooth.

HIH6130 is a humidity and temperature sensor with digital output. These sensors provide an accuracy level of ±4% RH. With industry-leading long-term stability, true temperature-compensated digital I2C, Industry-leading reliability, Energy efficiency and Ultra-small package size and options.

In this tutorial the interfacing of the HIH6130 sensor module with raspberry pi is demonstrated and its programming using Java language has also been illustrated. To read the temperature and humidity 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:

The materials that we need for accomplishing our goal includes the following hardware components:

1. HIH6130

2. Raspberry Pi

3. I2C Cable

4. I2C Shield for raspberry pi

5. Ethernet Cable

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 HIH6130 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 Humidity and Temperature Measurement:

The advantage of using raspberry pi is, that 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 it's programming in the Java. The java code for HIH6130 can be downloaded from our GitHub community that is Dcube Store.

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 pi4j library in case of java because this library supports the functions used in the code. So, to download the library you can visit the following link:

http://pi4j.com/install.html

You can copy the working java code for this sensor from here also:

<p>import com.pi4j.io.i2c.I2CBus;</p><p>import com.pi4j.io.i2c.I2CDevice;</p><p>import com.pi4j.io.i2c.I2CFactory;</p><p>import java.io.IOException;</p><p>public class HIH6130</p><p>{	</p><p>public static void main(String args[]) throws Exception	</p><p>{		</p><p>// Create I2C bus		</p><p>I2CBus Bus = I2CFactory.getInstance(I2CBus.BUS_1);		</p><p>// Get I2C device, HIH6130 I2C address is 0x27(39)		</p><p>I2CDevice device = Bus.getDevice(0x27);		</p><p>Thread.sleep(500);</p><p>// Read 4 bytes of data		</p><p>// humidity msb, humidity lsb, temp msb, temp lsb		</p><p>byte[] data = new byte[4];		</p><p>device.read(0x00, data, 0, 4);</p><p>// Convert the data to 14-bits		</p><p>double humidity = (((data[0] & 0x3F) * 256) + (data[1] & 0xFF)) / 16384.0 * 100.0;		</p><p>int temp = ((((data[2] & 0xFF) * 256) + (data[3] & 0xFC)) / 4);		</p><p>double cTemp = (temp / 16384.0) * 165.0 - 40.0;		</p><p>double fTemp = cTemp * 1.8 + 32;</p><p>// Output data to screen		</p><p>System.out.printf("Relative Humidity : %.2f %% RH %n", humidity);		</p><p>System.out.printf("Temperature in Celsius : %.2f C %n", cTemp);		</p><p>System.out.printf("Temperature in Farhenheit : %.2f F %n", fTemp);	</p><p>}	</p><p>}</p>

The library which facilitates i2c communication between the sensor and the board is pi4j, its various packages I2CBus, I2CDevice and I2CFactory help to establish the connection.

<p>import com.pi4j.io.i2c.I2CBus;<br>import com.pi4j.io.i2c.I2CDevice;
import com.pi4j.io.i2c.I2CFactory;
import java.io.IOException;</p>

write() and read() functions are used to write some particular commands to the sensor to make it work in a particular mode and read the sensor output respectively.

The output of the sensor is also shown in the picture above.

Step 4: Applications:

HIH6130 can be used to provide precise relative humidity and temperature measurement in air conditioners, enthalpy sensing, thermostats, humidifiers/de-humidifiers, and humidistats to maintain occupant comfort. It can also be employed in air compressors, weather stations and telecom cabinets.