Go Down

Topic: HIH-4030 Humidity Sensor - need help assigning a fixed temp value (Read 1 time) previous topic - next topic


I'm new to programming hence my confusion.

I'm using the HIH-4030 relative humidity sensor with an Arduino Uno an Ethernet shield and feeding the data to Cosm.com. The values that the sensor is returning don't make sense. It's been ranging from 600-800. I found some sample code on a website that Sparkfun linked to where they used a fix temperature value to calculate the relative humidity. Below I've included that code in the Code 1 section.

In the section Code 2 below that's the code I'm currently using to stream data to Cosm. How can I combine the fixed temperature value from Code 1 to the code in Code 2? If someone would be willing to explain I'd greatly appreciate it.

Thanks for the help


Code 1

//From the bildr article http://bildr.org/2012/11/hih4030-arduino/

int HIH4030_Pin = A0; //analog pin 0

void setup(){

void loop(){

//To properly caculate relative humidity, we need the temperature.
float temperature = 25; //replace with a thermometer reading if you have it
float relativeHumidity = getHumidity(temperature);


delay(100); //just here to slow it down so you can read it

float getHumidity(float degreesCelsius){
//caculate relative humidity
float supplyVolt = 5.0;

// read the value from the sensor:
int HIH4030_Value = analogRead(HIH4030_Pin);
float voltage = HIH4030_Value/1023. * supplyVolt; // convert to voltage value

// convert the voltage to a relative humidity
// - the equation is derived from the HIH-4030/31 datasheet
// - it is not calibrated to your individual sensor
// Table 2 of the sheet shows the may deviate from this line
float sensorRH = 161.0 * voltage / supplyVolt - 25.8;
float trueRH = sensorRH / (1.0546 - 0.0026 * degreesCelsius); //temperature adjustment

return trueRH;


Code 2

* Cosm Arduino sensor client example.
* This sketch demonstrates connecting an Arduino to Cosm (https://cosm.com),
* using the new Arduino library to send and receive data.
* Requirements
* * Arduino with Ethernet shield or Arduino Ethernet (board must use the
* Wiznet Ethernet chipset)
* * Arduino software with version >= 1.0
* * An account at Cosm (https://cosm.com)
* Optional
* * An analog sensor connected to pin 2 (note we can still read a value from
* the pin without this)
* Created 8th January, 2013 using code written by Adrian McEwen with
* modifications by Sam Mulube
* Full tutorial available here: https://cosm.com/docs/quickstart/arduino.html
* This code is in the public domain.

#include <SPI.h>
#include <Ethernet.h>
#include <HttpClient.h>
#include <Cosm.h>

#define API_KEY "key goes here" // your Cosm API key
#define FEED_ID ##### // your Cosm feed ID

// MAC address for your Ethernet shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// Analog pin which we're monitoring (0 and 1 are used by the Ethernet shield)
int sensorPin = 2;

unsigned long lastConnectionTime = 0; // last time we connected to Cosm
const unsigned long connectionInterval = 15000; // delay between connecting to Cosm in milliseconds

// Initialize the Cosm library

// Define the string for our datastream ID
char sensorId[] = "sensor_reading";

CosmDatastream datastreams[] = {
CosmDatastream(sensorId, strlen(sensorId), DATASTREAM_FLOAT),

// Wrap the datastream into a feed
CosmFeed feed(FEED_ID, datastreams, 1 /* number of datastreams */);

EthernetClient client;
CosmClient cosmclient(client);

void setup() {
// put your setup code here, to run once:

Serial.println("Cosm Sensor Client Example");

Serial.println("Initializing network");
while (Ethernet.begin(mac) != 1) {
Serial.println("Error getting IP address via DHCP, trying again...");

Serial.println("Network initialized");

void loop() {
// main program loop
if (millis() - lastConnectionTime > connectionInterval) {
// read a value from the pin
int sensorValue = analogRead(sensorPin);
// send it to Cosm
// read the datastream back from Cosm
// update connection time so we wait before connecting again
lastConnectionTime = millis();

// send the supplied value to Cosm, printing some debug information as we go
void sendData(int sensorValue) {

Serial.print("Read sensor value ");

Serial.println("Uploading to Cosm");
int ret = cosmclient.put(feed, API_KEY);
Serial.print("PUT return code: ");


// get the value of the datastream from Cosm, printing out the value we received
void getData() {
Serial.println("Reading data from Cosm");

int ret = cosmclient.get(feed, API_KEY);
Serial.print("GET return code: ");

if (ret > 0) {
Serial.print("Datastream is: ");

Serial.print("Sensor value is: ");



I didn't check your code, sorry.
But I think there is some confusion.
The Honewell sensor returns the RH (Relative Humidity), regardless of the temperature. If you look at the datasheet, at figure 4, you see that the RH is a little different for 0 degrees and 70 degrees, but not that much.

I think that the bildr article is confusing.
All you have to do is convert the raw analog value to a voltage, and the voltage to a RH.

When you want to know the dew point or something else, that is when some calculations are needed.

Sparkfun page : https://www.sparkfun.com/products/9569
Wikipedia Relative Humidity : http://en.wikipedia.org/wiki/Relative_humidity
Wikipedia Dew Point : http://en.wikipedia.org/wiki/Dew_point

Found a library for the HIH4030 sensor.

http://angryelectron.com/hih4030-libs/ <--details are here.

https://github.com/angryelectron/tweetpot/tree/master/arduino/HIH4030 <--library and documentation located here.


Jan 18, 2013, 07:51 pm Last Edit: Jan 18, 2013, 07:52 pm by Krodal Reason: 1
Good, that code is what I was talking about.
analogRead() -> voltage -> RH value.
The code also calculates the 'true RH' value, but I haven't read yet what that is.

Any idea if you use the Pachube client code and make some tweaks would it work with Cosm.com, which is the newer version of Pachube?
Has anyone tried this?




The library for the sensor utilizes the 1str order  fit curve for the sensor. I have written code that uses the 2nd order fit curve to get a little more accuracy.

some notes:
the library allows you to change the supply voltage and allows you to calibrate the sensor based on factory supplied cal certs. it also allows you to adjust for temperature.

the code i have written does not allow this. you must use 5.0 volt supply voltage.

Code: [Select]
float GET_HUMIDITY(byte analogPin){
/*[Voltage output (2nd order curve fit)] Vout=0.00003(sensor RH)^2+0.0281(sensor RH)+0.820, typical @ 25 ÂșC and 5VDC supply
the equation was found on http://www.phanderson.com/hih-4000.pdf
and a better / more ledigible typical output curve can be found at http://sensing.honeywell.com/index.php/ci_id/51625/la_id/1/document/1/re_id/0

Solving the above for (sensor RH) = 182.57418583506*(sqrt(Vout + 5.76008333333335) - 2.5651673109825)

Vout = (float)(analogRead(pin)) * vRef / 1023 where vRef = 5 volts
Vout = (float)(analogRead(pin)) * 5 / 1023
Vout = (float)(analogRead(pin)) * 0.004888

(sensor RH) = 182.57418583506*(sqrt(((float)(analogRead(pin)) * 0.004888) + 5.76008333333335) - 2.5651673109825)

as an example, assume Vout = 2.0 volts, (sensor RH) would then equal 40.9002 %
looking at the ledigible typical output curve, it can be seen that at 2.0 volts the best linear fit curve is just a little more than 40% indicating the calculation is correct

  float Percent_RH = 182.57418583506 * (sqrt(((float)(analogRead(analogPin)) * 0.004888) + 5.76008333333335) - 2.5651673109825);
  return Percent_RH;

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131