German English
Sensors

SHT21 - Read values from humidity sensor SHT21

The microcontroller on the radio module Preon32 has several IO interfaces which allows accessing nearly each kind of sensor. The product VariSen which is designed for use with Preon32 contains some sensors, the SHT21 included.

This examples shows how to access a SHT21 sensor. The program reads the humidity values each second and prints it out. Accessing this sensor is very easy because Virtenio provides a driver for it.
/*
 * Copyright (c) 2011., Virtenio GmbH
 * All rights reserved.
 */
package com.virtenio.webdemo;

import com.virtenio.drivers.i2c.NativeI2C;
import com.virtenio.drivers.sensors.i2c.SHT21;

import java.io.IOException;

public class HumidityI2C {

    public static void main(String[] args) throws IOException, InterruptedException {

        NativeI2C i2c = NativeI2C.getInstance(1);
        i2c.open();

        SHT21 sht21 = new SHT21(i2c);
        sht21.setResolution(SHT21.RESOLUTION_RH12_T14);
        sht21.reset();

        Thread.sleep(1000);
        //
        while (true) {
            // relative humidity
            sht21.startRelativeHumidityConversion();
            Thread.sleep(100);
            int rawRH = sht21.getRelativeHumidityRaw();
            float rh = SHT21.convertRawRHToRHw(rawRH);
            System.out.println("SHT21: rawRH=" + rawRH + "; RH=" + rh);
            Thread.sleep(900);
        }
    }

}