HYT939 is a digital humidity sensor which works on I2C communication protocol. Humidity is a pivotal parameter when it comes to medical systems and laboratories. So, in order to accomplish these goals, we tried to interface HYT939 with Raspberry Pi. In this tutorial, the interfacing of the HYT939 sensor module with Raspberry Pi is demonstrated and its programming using Java language has also been illustrated.
To read the 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 RequiredHardware that is required to accomplish the task is as follows:
1. HYT939
2. Raspberry Pi
3. I2C Cable
4. I2C Shield For Raspberry Pi
5. Ethernet Cable
Step 2: Hardware HookupThe 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 HYT939 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: Java Code for humidity measurementThe advantage of using Raspberry Pi is that it 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 its programming in the Java. The java code for HYT939 can be downloaded from our Github community that is Control Everything Community.
As well as for the ease of the users, we are explaining the code here.
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. 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:
import com.pi4j.io.i2c.I2CBus;
import com.pi4j.io.i2c.I2CDevice;
import com.pi4j.io.i2c.I2CFactory;
import java.io.IOException;
public class HYT939
{
public static void main(String args[]) throws Exception
{
// Create I2CBus
I2CBus bus = I2CFactory.getInstance(I2CBus.BUS_1);
// Get I2C device, HYT939 I2C address is 0x28(40)
I2CDevice device = bus.getDevice(0x28);
// Send normal mode command
device.write((byte)0x80);
Thread.sleep(500);
// Read 4 bytes of data
// humidity msb, humidity lsb, temp msb, temp lsb
byte[] data = new byte[4];
device.read(data,0,4);
// Convert the data to 14-bits
double humidity = (((data[0] & 0x3F) * 256) + (data[1] & 0xFF)) * (100.0 / 16383.0);
double cTemp = ((((data[2] & 0xFF) * 256) + (data[3] & 0xFC)) / 4) * (165.0 / 16383.0) - 40;
double fTemp = (cTemp * 1.8 ) + 32;
// Output data to screen
System.out.printf("Relative Humidity is : %.2f %%RH %n", humidity);
System.out.printf("Temperature in Celsius is : %.2f C %n", cTemp);
System.out.printf("Temperature in Fahrenheit is : %.2f F %n", fTemp);
}
}
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.
import com.pi4j.io.i2c.I2CBus;
import com.pi4j.io.i2c.I2CDevice;
import com.pi4j.io.i2c.I2CFactory;
import java.io.IOException;
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. Following part of the code illustrates the usage of these functions.
// Send normal mode command
device.write((byte)0x80);
Thread.sleep(500);
// Read 4 bytes of data
// humidity msb, humidity lsb, temp msb, temp lsb
byte[] data = new byte[4];
device.read(data,0,4);
The output of the sensor is also shown in the picture above.
Step 4: ApplicationsHYT939, being an efficient digital humidity sensor, are employed in medical systems and autoclaves. Pressure dew point measurement and drying systems also find the usage of this sensor module. In various laboratories where appropriate humidity level is a pivotal parameter for conducting experiments, this sensor can be deployed there for humidity measurements.
Comments