NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Support Forum » Weather Station Humidity Sensor

February 07, 2011
by Ralphxyz
Ralphxyz's Avatar

Well my anemometer project has evolved into a full weather station.

I have a humidity sensor form Future Electronics

There are less expensive sensors but I liked the enclosure instead of a naked component.

This sensor is just like the Nerdkit's Temperature Sensor (LM34) a three wire setup making it easy to use the tempsensor project code.

Now how would I show Relative Humidity (RH)? I need to convert from mv.

Would I use a lookup table? How? Or a struct (BIG HOW). Or a array (again how)?

There are only 18 reference values so an array seems feasible (to my limited programming mind)but I would like to learn how to use structs since the whole purpose of my having a Nerdkit is to learn. I could always buy a weather station and save a whole lot of time.

I sure appreciate all of the help I have received.

Ralph

February 07, 2011
by mongo
mongo's Avatar

One thing you need to remember... relative humidity changes with temperature. When I was working in the bakery, I had to maintain the chart recorders for the proof boxes. That meant I had to check their accuracy periodically. I used what is called a sling psychrometer. It uses two highly accurate mercury thermometers. One is kept wet with a wick and the other is dry. When you use it, you swing it around and around for about a minute and then read the temperatures on each. Then you use a slide-rule type chart to determine the RH at any given dry bulb temperature. I still have one but one of the thermometers is broken and I haven't had time to get a replacement.

Hygrometers work a little differently and usually have a temperature sensor with them so it can make the necessary adjustments automatically.

February 07, 2011
by 6ofhalfdozen
6ofhalfdozen's Avatar

Ralph, I don't know if you found the datasheet for the Rh Probe or not, but there are some interesting info in it. here is the maker's website with the version searched out:

http://www.meas-spec.com/searchresults.aspx?search=HM1500LF

While it doesn'y specificallt mention being temperature compensated, it does show some equations for thermal offsets and such in mV/RH%/'C. i get the impression that there is some thermal compensation and conversion going on internally even if it is not mentioned. But if you don't want to be scientifically precise and use the 5VDC power like they recommend, you probably won't need to mess with too much thermal adjustment since the coeff's are pretty tiny. Ralph, you are doing some serious trailblazing and learning with these nerdkits! I definately agree that the lookup tables, structures and such would be a good thing to learn (definately something I need to learn since I plan on doing some thermocouples hooked to the NK via 24bit ADC's this year). Though in this case, I would personally just plug the given equation into the NK code. The two equations from the datasheet are below.

Assuming Vcc is 5.0 VDC Vout(in mV) = 25.68 * Rh + 1079 Rh = 0.03892 * Vout - 42.017

The datasheet does mention that above 95% or below 10% the readings will be off a decent bit, but honestly to get better than that you need to pay $$$$$$. We have a couple at work that are +-.25% RH Full Scale 0-150C and cost as much as my car. But for a weather station, 10-95% should be just fine. hmmm.. I am rambling again, so I will wrap this up. hopefully something in there was usefull.

February 07, 2011
by Ralphxyz
Ralphxyz's Avatar

Ok, I am just using the tempsensor project code.

I have ADC: 363 - 364 of 1024

Temperature: 177...

1.767 - 1.769 volts on my multimeter

How would I show that as Relative Humidity?

From the table 1767mv would be between 25%RH and 30%RH. I would like a little more precision.

Man is this sensor sensitive if I just happen to lean forward and breath out the voltage increases, in about a minute it will settle back down.

I can not believe how much fun I am having with this, I guess that is why they call them Nerdkits.

I mean I am breathing on the thing and watching my mutimeter change and the values on the LCD increase and decrease fascinating.

Well my wife is impressed (a very tolerant sort she is).

Of course I had to pinch the temperature sensor to see the temperature rise a number of times but this is different I am 12 - 15 or so inches away from the sensor lying on my desk and it is detecting the moisture in my breath.

Now I will need to find a sling psychrometer at a yard sale in order to calibrate my sensor. Thanks mongo for reminding me of how that works. I remember having a sling psychrometer in freshman earth science class.

Ralph

February 07, 2011
by mongo
mongo's Avatar

Ralph,

Check out Cole Parmer Instruments for the device. I thing that was where I got mine, umpteen years ago.

February 07, 2011
by mongo
mongo's Avatar

I haven't used mine in a long time. Not since I got the digital... I think they run about $100 to $125 each.

February 08, 2011
by Ralphxyz
Ralphxyz's Avatar
Well this is going well beyond my math skills to say nothing about my programming abilities.

Could someone explain the math to me? That is, how would I use the formulas?

![](http://gettheworkdone.net/images/NerdKits/Projects/WeatherStation/Math.png)

Then can someone explain how they get the nice straight linear progressive line out of the table data:
![](http://gettheworkdone.net/images/NerdKits/Projects/WeatherStation/Modeled%20Signal%20Output.png)

1600 - 1325 = 275   
1860 - 1600 = 260   
2110 - 1860 = 250    
2360 - 2110 = 250
2605 - 2360 = 245
2860 - 2605 = 255
3125 - 2860 = 265
3405 - 3125 = 280

Does not look like a straight line to me. That is why I need the math explained.

Of course I dropped out of Algebra2 my junior year in high school which was my last formal instruction in math so maybe I am just missing it.

Just a explanation of using the linear equation would probable be enough though I'd love to understand the polynomial equation.

On another note, in case any one is interested this is a classic case of "AVOIDANCE". I am totally engrossed in working this math out for the humidity sensor which is not what I "need" to be working on. So by working on this I can avoid working on that which is intimidating me (getting the RTC chip working).

Ralph

Sorry the links to my images appear to be broken at least in the Preview screen. The links are good it's the forum software at work again.

February 08, 2011
by 6ofhalfdozen
6ofhalfdozen's Avatar

Ralph,

Because the RH meter outputs from ~1.33v-3.55V you will have some compression of data. the good news is that the 10bit ADC steps are about 5mV per step. I did some calculations and if you use the equations provided you can get pretty nice accuracy. Using the ADC values you mentioned, 363-362, I calculated an RH of 26.9-27.1%. I reworked the equations so you can input the ADC counts and get an RH% output. Also, I put in a pretty close approximate for saving code space and calculation time.

RH% = .19004 * ADCcounts - 42.017

 approximation     RH% = (ADCcounts * 19 / 100) - 42

For testing points, it might be usefull for you to build a table in excel or something to have a reference of ADC counts vs RH% readings. anyhow, that should point you in the right direction and clean up some of that +-5% you were getting from the table. hope that helps.

February 08, 2011
by n3ueaEMTP
n3ueaEMTP's Avatar

Found this on ebay if anybody is interested:

eBay--Honeywell Relative Humidity Sensor HIH4000

I might have to pick one up, just for fun...

February 08, 2011
by 6ofhalfdozen
6ofhalfdozen's Avatar

Ralph,

Sorry I missed your last post. I got stuck on the phone mid-post. Algebra was never my strong suit, so I went at it two ways and got the same result. Anyhow, here is what I did to deal with the equations. btw, the table they provided is nice for quick reference, but is really too vague to use for calculations like you want.

The first way, I took the given formula for converting mV to RH% RH% = .03892 * Vout - 42.017 The NK ADC provides 1024 steps across 5V, so 5V/1024steps = 4.833mV per step. Using this to try and scale the equation you get RH% = 0.03892 * (Vout = ADCsteps /4.833) - 42.017 simplified into RH% = .19004 * ADCsteps - 42.017 using your above result of 363 steps your plug this in and get 26.97% RH

The second way I went was to build a table of mV's from 1000-5000 and calculated RH using their equation in excel. I added a column of ADC steps for the 1000-5000mV using mV/(5v/1024steps), which goes from 205 to 1024. the datasheet mentions that the sensor at 0% RH outputs ~1.33V and at 100% Rh outputs ~3.55V, so I removed all the rows outside of this range. I created a chart and graphed RH% over ADC counts. Adding a trendline with equation or using the RSQ,Slope,Intercept functions in excel I calculated a line for the data. my equation comes out to y = 0.19004 X - 42.01700 R2 = 1.0000 which tells me that the data is a REALLY good fit and the equation is the same I calculated in way 1. the caveat to this is that the manufacturer doesn't trust the data above 95% or below 5%, so I would be a little skeptical of those data points. Otherwise the line fine looks good, and short of some actual known humidity chamber testing, you should be all set. hopefully that helps clear some stuff up.

February 08, 2011
by Ralphxyz
Ralphxyz's Avatar

6ofhalfadozen once again I am indebted to you, thank you so much.

n3ueaEMTP, that Honeywell sensor looks like it works the same way, there are a lot of different humidity sensors. Like I said the reason I choose the one I did was because of the enclosure, it is all set to be exposed to outside weather. I figured that was worth the extra money.

It really is fascinating to have it pick up my breathing so readily.

Ralph

February 08, 2011
by Ralphxyz
Ralphxyz's Avatar

6ofhalfadozen, would you send me the spreadsheets you made up? I am really curious to understand the math.

Thanks,

Ralph

rhulslander gmail.com

February 08, 2011
by 6ofhalfdozen
6ofhalfdozen's Avatar

Ralph,

It should be on its way. Let me know if you need any clarifications. I do these kinds of calculations all the time for work, so sometimes my steps aren't the clearest to others.

February 08, 2011
by Ralphxyz
Ralphxyz's Avatar

Doug, thank you so much. I got the spreadsheets. I added your formula to the tempsensor code. The humidity reading seems low but then againI do not have a cross reference so who knows. I definitely have a sling psychrometer on my list of things to look out for.

Thanks again every one.

Here is the modified code in case anyone wants it:

// HumiditySensor.c
// for NerdKits with ATmega168 modified tempsensor project
// mrobbins@mit.edu

#define F_CPU 14745600

#include <stdio.h>
#include <math.h>

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <inttypes.h>

#include "../libnerdkits/delay.h"
#include "../libnerdkits/lcd.h"
#include "../libnerdkits/uart.h"

// PIN DEFINITIONS:
//
// PC0 -- temperature sensor analog input

void adc_init() {
  // set analog to digital converter
  // for external reference (5v), single ended input ADC0
  ADMUX = 0;

  // set analog to digital converter
  // to be enabled, with a clock prescale of 1/128
  // so that the ADC clock runs at 115.2kHz.
  ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0);

  // fire a conversion just to get the ADC warmed up
  ADCSRA |= (1<<ADSC);
}

uint16_t adc_read() {
  // read from ADC, waiting for conversion to finish
  // (assumes someone else asked for a conversion.)
  // wait for it to be cleared
  while(ADCSRA & (1<<ADSC)) {
    // do nothing... just hold your breath.
  }
  // bit is cleared, so we have a result.

  // read from the ADCL/ADCH registers, and combine the result
  // Note: ADCL must be read first (datasheet pp. 259)
  uint16_t result = ADCL;
  uint16_t temp = ADCH;
  result = result + (temp<<8);

  // set ADSC bit to get the *next* conversion started
  ADCSRA |= (1<<ADSC);

  return result;
}

double sampleToFahrenheit(uint16_t sample) {
  // conversion ratio in DEGREES/STEP:
  // (5000 mV / 1024 steps) * (1 degree / 10mV)
  //    ^^^^^^^^^^^      ^^^^^^^^^^
  //     from ADC         from LM34
  return sample * (5000.0 / 1024.0 / 10.0);  
}

int main() {
  // start up the LCD
  lcd_init();
  FILE lcd_stream = FDEV_SETUP_STREAM(lcd_putchar, 0, _FDEV_SETUP_WRITE);
  lcd_home();

  // start up the Analog to Digital Converter
  adc_init();

  // start up the serial port
  uart_init();
  FILE uart_stream = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);
  stdin = stdout = &uart_stream;

  // holder variables for temperature data
  uint16_t last_sample = 0;
  uint8_t i;
  uint8_t RH =0;

  while(1) {
    // take 100 samples and average them!
    for(i=0; i<100; i++) {
      last_sample = adc_read();
    }    
    //RH% = .19004 * ADCcounts - 42.017
    RH = .19004 * last_sample - 42.017;

            // write message to LCD
    lcd_home();
    lcd_write_string(PSTR("ADC: "));
    lcd_write_int16(last_sample);
    lcd_write_string(PSTR(" of 1024   "));
    lcd_line_three();
    fprintf_P(&lcd_stream, PSTR("Humidity is: %.0d %% "), RH);    
    // write message to serial port
    //printf_P(PSTR("%.2f degrees F\r\n"), temp_avg);
  } 
  return 0;
}

Ralph

February 08, 2011
by Rick_S
Rick_S's Avatar

Ralph,

I noticed you took 100 samples but didn't average them. So your code is looping 100 times but only using the last read value. If a single sample is all you need to use, you could eliminate the loop. I think this will be a neat project once you get it finished. I'm looking forward to see it all together.

Rick

February 09, 2011
by Ralphxyz
Ralphxyz's Avatar

Yup, once again you are correct Rick. I was not using the averaging so I didn't need the loop here is the corrected code snippet:

  while(1) {
    RH = .19004 * adc_read() - 42.017;

            // write message to LCD
    lcd_home();

When I have all of the weather station devices consolidated I am going to need every bit of space in the code.

6ofhalfdozen, thank you again for that great spreadsheet it was a lot of work you did.

Here is a shot of my setup and a clip from the spreadsheet so that you can see the accuracy, the tube across the multi-meter is the humidity sensor:

Ralph

February 09, 2011
by 6ofhalfdozen
6ofhalfdozen's Avatar

Looks good Ralph!! I don't know if you have one, but one of the little $10 temp/RH displays might help you double check your RH probe. In the lab, we have calibration salts and fixed test chambers to test our RH probe's calibration. Most of those chemicals are pretty nasty. However, regular table salt can be used. So if you wanted to do a quick test, here is a rough way to do it. A super/fully saturated solution of NaCL , table salt, in water at 70F will have a headspace with ~75% RH. so if you were to mix a lot of salt into water and put in into a ziplock bag then seal it. It you let it sit overnight to equilibrate, then carefully and quickly open the bag a tiny bit to stick the RH probe's tip into the bag and try to seal it as best you can. It should after a few minutes read close to 75%. This is really rough, but if you get anywhere from 60-80% I would call it good. It looks like you are making some progress on the total weather station. I have been watching the progress with the RTC in the other thread, and hopefully you guys are really close to having it all sorted out. This should be one sweet project once you have it all together..Can't wait to see how it comes out.

February 09, 2011
by Ralphxyz
Ralphxyz's Avatar

Hi 6ofhalfdozen, thanks I have searched the web for "sling psychrometer" and have found instructions one making one. Essentially a wet and dry bulb thermometer, mount it on a board add a rope and some wet gauze and you have a sling psychrometer. That should be a accurate reference. I'll also try your NaCl zip lock bag method. I like to have multiple test methods, especially as I am just learning all of this stuff it really makes it interesting to have multiple methods.

I have extended this weather station project to the extent of adding a small generator so that I do not need to worry about changing batteries or plugging it in. I started on this wanting to gather wind load data to see if a wind turbine would be feasible at my house. I would so much love to go off the grid or at least have the benefit of "Net Metering" .

I am having so my fun, course I have pulled most of the hair from my head and been very frustrated at times (due largely to my ignorance about math in particular) but hey I am and will get there.

Ralph

Post a Reply

Please log in to post a reply.

Did you know that a microcontroller can measure an RC time constant? Learn more...