If you are interested in adding humidity sensing to an Arduino project, or perhaps interested in angryelectron’s Tweetpot project, then you’ll need to know how to use the HIH4030 (or HIH4031) humidity Sensor and angryelectron’s HIH4030 libraries for Arduino and Java.

Buying Sensors

The sensor, made by Honeywell, outputs an analog voltage that is proportional to the relative humidity.  Unfortunately they are relatively (ha!) expensive and rather small.  I buy my sensors at Digikey, but I must admit soldering them is rather fiddly work.  Thankfully, Sparkfun has created a break-out board if you don’t have the patience or a steady hand.

The sensor is available in non-condensing (HIH4030) and condensing (HIH4031) versions.  I prefer the condensing variety, especially for the Tweetpot project.  Also, if you have the chance,  purchase the part that comes with a factory-calibration sheet.  These parts have the suffix -003 (eg. HIH-4031-003).  The calibration data allows for more accurate results.

The datasheet has the full product number scheme.


Refer to the datasheet for pin definitions, or take my word for it:

  • Pin 1 = GND / -ve
  • Pin 2 = OUT / data
  • Pin 3 = VCC / +ve

The data pin can be connected to an analog input of an Arduino, XBee, RaspberryPi, or any other IO device.

The datasheet  specifies the recommended range for VCC:  4.0V – 5.8V.  In practice you can use lower voltages (such as 3.3V) but you lose some accuracy.  Ideally, you’ll want to use 5V, especially if you want the most accurate results using the calibration data.


Reading the sensor is very simple using the built-in A/D converters in Arduinos and XBees, but converting that value into a relative humidity reading can be tricky.

Many people have written libraries and posted sample code online for doing this, but most make assumptions in order to simplify the routines.   It is difficult to understand how these libraries determine their scaling and conversion values, especially for a novice or hobbyist Maker who isn’t an expert and reading datasheets and solving formulas, and they make it nearly impossible to compensate for different slopes or supply voltages.   For this reason, the angryelectron HIH4030 libraries were created.

HIH4030 Library – Arduino

This library can be used with or without temperature compensation, with or without factory calibration data, and can use a range of supply values and still give accurate results.  The code is fully commented and shows how formulas have been derived.  The code is open-source and available on github or download the latest release here:

[wpdm_file id=1]

Get the library and install it in your Sketchbook library folder.  The library contains two working examples,  and the source code (HIH4030.cpp) explains how to use each method and how formulas were derived.  Please use the Issue Tracker on GitHub to report problems.

Here is a quick summary of the library’s methods:

  • HIH4030(io_pin, supplyVoltage, referenceVoltage) – create an instance of the sensor.
  • calibrate(slope, offset) – Use factory-specified values for slope and offset.  If not called, default values are used.
  • getSensorRH() – returns relative humidity, in percent.
  • getTrueRH(temperature) – returns relative humidity compensated for the supplied temperature.
  • vout() – returns sensor output voltage.  Useful for testing, calibrating, and debugging.

Here is a very simple sketch:

#include <HIH4030.h>

/*parameters: IO pin, supply voltage, analog reference voltage */
HIH4030 hygrometer(A3, 5.0, 5.0);

void setup(void) {
  /* optional step:  calibrate if you have SLOPE and OFFSET values */
  hygrometer.calibrate(SLOPE, OFFSET);

void loop(void) {
  /* print non-temp compensated relative humidity in percent */

 HIH4030 Library – Java

This isn’t really a full library – just a single class bundled into a jar for convenience.  Pass it a voltage (and optionally the temperature) and it will return the relative humidity.  See the included Javadocs for usage.   The Tweetpot project uses this library.

You can find the source on github or download the latest release:

[wpdm_file id=2]

Please use the Issue Tracker on GitHub to report problems.  Here is a quick example:

import com.angryelectron.sensor.HIH403X


public void example() {
  final double vcc = 5.0;  /* sensor's supply voltage */
  HIH403X hih4030 = new HIH403X(vcc);

  double vout = readSensor(); /* you supply the sensor reading, in volts */
  double relativeHumidity = hih4030.getSensorRH(vout); 

For a more complete example, see HumiditySensor.java, part of the Tweetpot project.




Tagged with →  
Share →

Leave a Reply

Your email address will not be published.