Understanding a calculation

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 5.
To post in this forum you must have a registered copy of Flowcode 5 or higher.

Moderator: Benj

Post Reply
Bobw
Posts: 157
Joined: Sat Jan 22, 2011 10:39 pm
Location: Michigan
Has thanked: 6 times
Been thanked: 27 times
Contact:

Understanding a calculation

Post by Bobw »

I am trying to understand the formula to calculate HUMIDITY
I am using a Honeywell HIH-4010 humidity sensor.
*** PLEASE DO NOT GIVE ME THE FORMULA *** It will not help me understand the numbers.
Data Sheet:http://sensing.honeywell.com/honeywell- ... H-4021-002
From the data sheet I find the following information:
TABLE 2. EXAMPLE DATA PRINTOUT
Model HIH-4010-003

Calculated values at 5 V
VOUT at 0% RH 0.958 V
VOUT at 75.3% RH 3.268 V
Linear output for 3.5% RH
accuracy at 25 °C
Zero offset 0.958 V
Now I know I have to change the ADC reading to reflect that at 0%RH is should be 0.958 V and at 75%RH is should be 3.268 volts.
Normal 10bit ADC will give me 0-1023 steps,or 0.004882813 volts per step. I know I need to shift this to a scale somewhere in the middle of this.
100%RH will not =5 volts.
At 0%RH the ADC should be at or near ADC step 196, or 0.95703125 volts.(have to love Excel)
Not wanting to use floats I could multiply that value by 1000 and get 957, or just use the data sheet figure and call it 958.
I know 958 = 0%RH offset.
I know 326.8 would =70%RH offset.
My supply voltage =5 volts.
Now is where I get lost, I am not sure how to construct the formula. I am not worried about temperature correction. The sensor is pretty linear as it is, and 1-2% is not going to be a big deal. Not building a space ship here.

Bob

kersing
Valued Contributor
Valued Contributor
Posts: 2045
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 553 times
Been thanked: 1081 times
Contact:

Re: Understanding a calculation

Post by kersing »

Bob,

Calculate the 100% voltage from the values you know. Let's say it is at 3.5 volt (it is not, you will need to use excel :-)). That would give you an ADC reading of 716. So the full scale of 0% to 100% would be 196 (your post) to 716 (example value) = 520 steps.

Now to calculate the current value, read the ADC value, let assume it is 538. Next, you know 0% is at a certain ADC value (196 in your post), subtract that number from you reading. This results in an example value of 342. Now find the percentage by dividing the 342 by the number of steps (520). As this would require floating point you first multiply the value by 100 and next divide by 520, the result is the percentage. As 342*100 is too large for a signed integer you need to store it in either an unsigned integer (preferred on 8 bit devices) or a long variable. (Unsigned integers are 0-65535 so will work nicely for this example, but might not in the real world depending to the number of steps)

Does this make sense?

Regards,

Jac
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Understanding a calculation

Post by medelec35 »

Hi Bob,
I know you said said
Bobw wrote:*** PLEASE DO NOT GIVE ME THE FORMULA ***
But at this stage it's silly explaining how formula was derived if it does not work!

So here is what I came up with:
RHumidity = ADC*4/25-27

Note the above will only work (I thik it will anyway :P ) if ADC is ReadAsInt so value is from 0 to 1023
If you try it and it works then I will post how I created it.

I'm not an expert at maths, but there are a bit of maths involved like transposition & factoring.
You may find that Jac post helps a lot, so you may not even require the usage of my formula.

Martin
Martin

Bobw
Posts: 157
Joined: Sat Jan 22, 2011 10:39 pm
Location: Michigan
Has thanked: 6 times
Been thanked: 27 times
Contact:

Re: Understanding a calculation

Post by Bobw »

Jac & Martin

OK, here is what I have come up with so far.
Variables: Readhum set as int, hum set as int, Q set as int (in case I needed a temp storage)
Readhum= ADCvalue
Formula : hum = ((readhum - 196) * 100) / 616
Using excel 100% would= 3.964 volts or 812 of the 1024 steps.
812 (number of steps) -196 (0% step) = 616 steps 0-100

After programming I get an actual reading of 21%, compared to 3 other RH sensors, this should be closer to 30%

I also see this formula listed in the Data sheet.
VOUT=(VSUPPLY)(0.0062(sensor RH) + 0.16), typical at 25 ºC

I am getting there and the numbers in the formula are starting to make sense.

Martin, going to try your formula next.

Cheers
Bob

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Understanding a calculation

Post by medelec35 »

Hi Bob,
just putting together a different formula and seeing if it's anymore accurate.
Will post it soon (only if it works of course).

Martin
Martin

Bobw
Posts: 157
Joined: Sat Jan 22, 2011 10:39 pm
Location: Michigan
Has thanked: 6 times
Been thanked: 27 times
Contact:

Re: Understanding a calculation

Post by Bobw »

Martin,

The formula you gave resulted in 25% RH displayed.

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Understanding a calculation

Post by medelec35 »

Hi Bob the formula posted will give a 25%RH when ADC = 330 = 5/1023*330 = 1.61V:
RH1.png
(42.72 KiB) Downloaded 7377 times
If you look at Fig 3 on the datasheet then if you look at 25% RH it is about 1.6V

Can you try this one instead please:
RH = ReadADC * 4 / 25 - 31
If it does work then it was quite easy to work out and I can take you through it step by step.
No transposition in sight.

When ADC = 1023/5 * 3.268 = 668 then RH should be = 75 (datasheet shows 75.3):
RH2.png
(41.81 KiB) Downloaded 7377 times
To make sure Sensor is working, it's a good idea to measure ADC voltage.

Martin
Martin

Bobw
Posts: 157
Joined: Sat Jan 22, 2011 10:39 pm
Location: Michigan
Has thanked: 6 times
Been thanked: 27 times
Contact:

Re: Understanding a calculation

Post by Bobw »

Martin,

Sensor is working. Have it displaying ADC value along with Humidity.
Matches with my other instruments I have.
I did NOT apply any temperature correction. The unit will be indoors with a pretty constant temperature.
not sure about the high end 75+% think I am missing a number.

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Understanding a calculation

Post by medelec35 »

Hi Bob,
Thanks for the update.
For people with the same sensor,
What calculation was used in the end?

If it's one that I have posted, I will post how to work calculations out this evening when I get home from work.
Martin

Bobw
Posts: 157
Joined: Sat Jan 22, 2011 10:39 pm
Location: Michigan
Has thanked: 6 times
Been thanked: 27 times
Contact:

Re: Understanding a calculation

Post by Bobw »

here is what I ended up with.
Still not 100% sure if it is correct. I measure my voltage at 5.05 VDC with my Fluke meter.
I adjusted my scale in Excel to reflect the extra 0.05 volts.
Readhum is the ADC-sensor input.

hum = ((Readhum - 197) * 100) / (1023 - 600)

I think there will always be some error due to there are several voltage changes that must take place before you see a change up or down in RH%.
I see the ACD read out fluctuating from 329-333. At 329 I see 31%RH and at 333 I see 32%RH.

Still a work in progress.

Bob

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Understanding a calculation

Post by medelec35 »

Hi Bob,
It's good to see you have developed your own formula - well done!
medelec35 wrote:I see the ACD read out fluctuating from 329-333
Fluctuations can be reduced by decreasing the load resistance that is connected to the sensor but observing you're staying within the correct parameters of the sensor.
Also to reduce fluctuations a capacitor can be fitted, but not sure if that can be possible with this sensor or not.
I don't want to say add it, just in case it causes the sensor to fail.

Would you like me to speak to the honneywell rep about this when I see them next.
The reps don't know the answers themselves, I would assume they will speak to R & D in Italy.
Martin

Bobw
Posts: 157
Joined: Sat Jan 22, 2011 10:39 pm
Location: Michigan
Has thanked: 6 times
Been thanked: 27 times
Contact:

Re: Understanding a calculation

Post by Bobw »

Martin,
I think we have been looking at this too technical.
For Relative Humidity, It should never be more than 100%, and never less than 0%.

The question for Honeywell would be:
Does the HIH-4010 Conduct any less voltage at their stated zero point, and at 100% is that the maximum voltage it will conduct?
Maybe Honeywell can suggest a formula.

For sake of argument if at 0% is never conducts less than the stated 0.958 volts, all you would need to know is what ADC value represents this voltage.
It should at stated temperature conduct less.
So your formula should only be Humidity = ReadADC Value - zero point it should never go lower than this point. At 100% it should never conduct MORE.
May have to account for step size from one value to another.
you could in code cap it so that even if it does go above 100% it will only show 100%.
I have never heard of 101% RH.
I will try to play with my theory in the morning.
Oh Honeywell does call for an 80K resistor across ground and voltage out. I only had some 45K so I used two of them, and yes it does help to smooth out the sensor.

Same formula idea may work for temperature too
Bob

Bobw
Posts: 157
Joined: Sat Jan 22, 2011 10:39 pm
Location: Michigan
Has thanked: 6 times
Been thanked: 27 times
Contact:

Re: Understanding a calculation

Post by Bobw »

A good night sleep can really clear the mind.

To read humidity, you need to know the scale of the sensor in bits (0-1023).
In the case of the HIH-4010 this is 0%=196 and 100%=812. With out a test chamber this is hard to verify so we have to rely on the DATA SHEET figures even though it does not give me a voltage=100% only 75%, but an excel sheet was used to extrapolate the range.

So I came up with this formula:
hum = ((Readhum - 197) * 100) / (1023 - 409)

Broken down as follows.
hum= int or the humidity reading that will be displayed on the LCD
Readhum is the reading from the sensor via the ADC in bits (0-1023)
197= the zero point of the scale for 0% humidity multiplied by 100 so you do not have to use floats
this is divided by 1023 or the total range of the 8 bit ADC (number would have to be = to the range of your ADC)
this is divided by adding the 0% low end (197) plus the number of steps from 1023 back to the high end or 100% (212)
212+197=409

With this formula, and using the simulator, if you move the slider to a position where the display reads 0%, changing the 409 value does not affect the 0% reading,
it will only affect the 100% Scale reading.

Hope this makes sense

Bob

Bobw
Posts: 157
Joined: Sat Jan 22, 2011 10:39 pm
Location: Michigan
Has thanked: 6 times
Been thanked: 27 times
Contact:

Re: Understanding a calculation

Post by Bobw »

I decided to post my code that I have so far.
There are several unused variables I have to check some various things in my testing.
It will display the Humidity, temperature in F and the ADC value of the humidity sensor.

hum = ((Readhum - 197) * 100) / (1023 - 532)
My high number has changed from the original 409 to 532 this could be due to my error in trying to extrapolate the 100% point.
But when changed to this value I now get a reading consistent with the other hygrometers I am using. Anyone know of a test chamber I can use?
For what this final project will end of being I do not need real accuracy, +- 5% will work, in fact I have a switch to offset things by +-5% when I am done.
I know many may want things to be as accurate as possible.
The HIH-4010 sensor is very sensitive, just blow on it and the readings will go up from the moisture in your breath. My formula does not contain temperature correction which will affect the accuracy. Even though I am using a 9V DC wall wart into a full bridge rectifier into a 5 volt regulator that has filter caps on both sides, I could be a few millivolts over or under 5 volts which can also effect to accuracy.
I am going to let this run as I have it for a few days and see how things go.
I have seen many post about reading humidity with some very hard to understand math in the equations.
Maybe we need to start a list of proven formulas with their explanations for others to use.
Bob
Attachments
newhum22.fcf
(17.91 KiB) Downloaded 246 times

Bobw
Posts: 157
Joined: Sat Jan 22, 2011 10:39 pm
Location: Michigan
Has thanked: 6 times
Been thanked: 27 times
Contact:

Re: Understanding a calculation

Post by Bobw »

I have been playing with this off and on while time would permit.
I contacted Honeywell the make of the HIH-4010 sensor, and with their help derived the following formula. hum = (((Readhum - 197) * 10) / 49)

hum= the value or actual humidity that will be displayed
Readhum= the ADC value as read by the picmicro
197 = the zero point in ADC steps (0-1023) that the sensor will be at 0% humidity
*10 is to keep away from floats
/49 = the number of steps in ADC value for each 1% increase. it is actually close to 4.9 steps hence the *10 and the /49 in the formula. This also keeps things in whole numbers. I have never seen humidity expressed as 35.7%RH, at least in house hold use. Since I work at a Hospital lab, I also found out we have several humidity controlled incubators. one at 40% one at 65% and the third at 90% So I mounted the sensor on an extension cable and put it to the test. on 40% I read 40, on 65% I read 66, on 90% I read 92. Close enough accuracy for me. Normal hose hold humidity should usually be less than 50%.

Now for temperature:
I ended up with this formula:
temp1 = ((((tempadc + 1) * 500) / 102.4) - 500)
temp1= the displayed temperature in Celsius
tempsadc +1 = the value from the adc (0-1023) plus on step gave me the accuracy to a calibrated probe.
*500 = the voltage of my ADC ref, used 500 to again stay away from floats.
/ 102.4= the number of ADC steps
-500= the 5 volt ref voltage.

This will give an output in degrees Celsius with about .5 degree resolution.
I used string functions to give me a display that looks more like this : Temperature = 23.7
temp1=((temp1* 18) + 3200) / 10 will convert it to Fahrenheit

There are sensors, both Humidity and temperature that will give much better resolution, but you either need a >10 bit ADC or use ones that communicate with the picmicro in binary and provide an exact reading.
I have also set a do nothing loop that causes a 30 second delay. So my sensors are only polled every 30 seconds. This provides a smoother display (does not change value every second. I have also put my temperature sensor in a 10mm X 10mm epoxy block to help prevent small momentary changes in reading.

More to come as i work on it.
Bob

Bobw
Posts: 157
Joined: Sat Jan 22, 2011 10:39 pm
Location: Michigan
Has thanked: 6 times
Been thanked: 27 times
Contact:

Re: Understanding a calculation

Post by Bobw »

I am writing this in hoping it will help others.
I have been wrestling with a simple calculation to permit temperature readings and display with out the use of memory intensive float values.
It finally dawned on me I was fighting ADC resolution when using a 5 volt reference. At 5 volts, the best you can ask for is 4.88 MV per ADC step. The sensor I am using is rated at 10 MV per degree of temperature C. On a 10 BIT ADC you only have 1023 steps to play with. What I needed to do is decrease the reference voltage to the ADC to 2.048 volts, this would give me 2 MV per step and still stay in the 100MV to 1750 MV range of the sensor. Enter a voltage divider, good article can be found here :https://learn.sparkfun.com/tutorials/vo ... viders/all
instead of fixed resistors, I used a 5K multi turn trim potentiometer. This allowed me to dial in the required voltage by simply seeing that when the ADC Step = 1023 the voltage=2.048, this would now give me a resolution of 0.2 degrees Celsius for each step in the ADC. This is far better than the >0.3 I had at 5 volts.
Doing this in hardware takes into account all the variances in my wiring and other components I used.
From the data sheet, my sensor can go from -40 to 125 C. So I had to take into account the negative numbers.
So for the formula with out floats.
1) Read the ADC in Volts (Volts_ADC)
2) Multiply this by 1000 to stay away from floats and to give all the digits otherwise you will only see 0-2 (*1000)
3) Subtract 100 for the 100MV the sensor will read at -40 degrees C (-100)
4) divide this by 10 for the number of MV per whole degree C (/10)
5) Finally add negative 40 to account for the low end of the sensor (+-40)

The formula looks like this Temp_C= (((Volts_ADC*1000)-100)/10)+-40
What i see on my panel and display is this
PanelADC.jpg
PanelADC.jpg (107.45 KiB) Viewed 12274 times
I will use string manipulation to get the display to read 11.5 C and 52.7 F
I now have a display in degrees Celsius with a 0.2 degree resolution. I can also re-calibrate by adjusting the trimmer I used as a voltage divider.
I have also displayed the temperature in Fahrenheit. It now works very well. Has survived both my -19C freezer and a 37C oven and dead on with a calibrated thermometer.

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Understanding a calculation

Post by medelec35 »

Hi Bob,
Thanks for sharing your findings.
It's great when forum user share, as it help those who are struggling.
I'm just a bit puzzled this works on hardware:
Bobw wrote:Temp_C= (((Volts_ADC*1000)-100)/10)+-40
Since I thought if Volts_ADC is an integer and Volts_ADC*1000 is greater then 32767 rollover will occur and results will not be as expected.
Glad it works for you.

Good idea with the multiturn pot!

Martin
Martin

Bobw
Posts: 157
Joined: Sat Jan 22, 2011 10:39 pm
Location: Michigan
Has thanked: 6 times
Been thanked: 27 times
Contact:

Re: Understanding a calculation

Post by Bobw »

When I read the ADC voltage and print the raw number to the LCD. I only see a single digit number.
Multiplying the ADC voltage by 1000 gave me the 4 digits I need
Greatest number I should see is 2048.

Post Reply