Calculating absolute humidity

Hello world,

I'm working on a small project where I'm measuring temperature and humidity in my garret/boxroom and then transmit the data to a KNX based home automation system.
I'm using an Arduino Pro Mini and the Adafruit BME280.

I'm sucessfully reading out data and transmitting the raw values to the bus,

But now, I want the arduino to evaluate the values and calculate the absolute humidity.
The board is up and running and I can't really use it for testing, because it's logging so I'm asking here if I'm doing things right here or if I need to tweak this code?

This project is my very first arduino project ever, so I'm a real noob actually :slight_smile: Even with the coding part.

if (bme.getTemperature_C() < -10) {
  Serial.print(2,2 * (bme.getHumidity()/100); serial.print("g/m3);
}
Else if (bme.getTemperature_C() > -10 and bme.getTemperature_C() < -8) {
  Serial.print(2,5 * (bme.getHumidity()/100); serial.print("g/m3);
}
Else if (bme.getTemperature_C() > -8 and bme.getTemperature_C() < -6) {
  Serial.print(3 * (bme.getHumidity()/100); serial.print("g/m3);
}
Else {
serial.print("Can't calculate Absolute humidity")
}

You can practice writing code without needing access to the sensor.

That fragment has many syntax errors and won't compile.

  1. Floating point numbers cannot contain a comma.
  2. Literal strings (character arrays) must be enclosed in double quotes.
  3. Observe character case. Serial.print is not the same as serial.print
  4. Balance parentheses.

I recently bought a BME280, and it's a wonderful gadget.

But mine only tells me atmospheric pressure and temperature - I don't see how you can get humidity from that...

regards

Allan.

sx3000:
But now, I want the arduino to evaluate the values and calculate the absolute humidity.

Formulas for converting relative humidity to absolute humidity are usually considerably more complex than your snippet. Where did you get that?

Did you try Google? This was the second hit on a search for absolute humidity equation.

Suggest double-checking your results with a psychometric chart.

Good morning guys! (9 AM in Sweden).

@DaveEvans, yes I tried to google and stumbled over this chart which is showing how much water in g/m3 air can contain at maximum at a certain temperature and multiply it with the measured RH.
This might not be the most scientific way to calculate, but maybe it's accurate enough to keep an eye on the levels in the garret/boxroom?
The chart I'm using can be found here:
http://www.lfs-web.se/fukt-temperatur.gif

@allanhurst, mine is Adafruit, and reading out humidity was described in the library and example code provided from Adafruit. But I'm using the Cactus library now, because it allow me set temperature offset.

@Jreminton, thanks for your input I will a look at the code and see if I understand you.
Regarding #1, it is okay to use 2.5 (while 2,5 is not okay)?
Could you please give an example of #2, I'm not I followed you there.

Here comes a small rev of the code. Found the verify/compile option :smiley:

if (bme.getTemperature_C() < -10) {
  Serial.print(2.2 * (bme.getHumidity()/100)); Serial.print("g/m3");
}
else if (bme.getTemperature_C() > -10 and bme.getTemperature_C() < -8) {
  Serial.print(2.5 * (bme.getHumidity()/100)); Serial.print("g/m3");
}
else if (bme.getTemperature_C() > -8 and bme.getTemperature_C() < -6) {
  Serial.print(3 * (bme.getHumidity()/100)); Serial.print("g/m3");
}
else {
Serial.print("Can't calculate Absolute humidity");
}

Thanks!

Okay, so I came to the conclusion that using the chart and IF strings, this will generate massive amount of code. Instead I will do the calulation on my own and I've took a look at DaveEvans link.
But I'm doing something seriously wrong with the code, and I can't figure out what I'm doing wrong, but I do get errors.

Here's the code:

int BaseLog = 2.71828;
int Temperature = bme.getTemperature_C();
int Humidity = bme.getHumidity();
int AbsoluteHumidity = (6.112 * BaseLog ^ ((17.67 * Temperature)/(Temperature + 243.5)) * Humidity * 2.1674) / (273.15 + Temperature);
Serial.print(AbsoluteHumidity);

This generates this error code while verifying;

exit status 1
invalid operands of types 'double' and 'double' to binary 'operator^'

edit
Managed to use the pow() and get it to verify/compile. :slight_smile: Wow, this is fun!!

int BaseLog = 2.71828;
int Temperature = bme.getTemperature_C();
int Humidity = bme.getHumidity();
int AbsoluteHumidity = (6.112 * pow(BaseLog,((17.67 * Temperature)/(Temperature + 243.5))) * Humidity * 2.1674) / (273.15 + Temperature);
Serial.print(AbsoluteHumidity);

edit #3
Realized that int don't give decimals, so I made a small adjustment by changing them to float.

float Temperature = bme.getTemperature_C();
float Humidity = bme.getHumidity();
float AbsoluteHumidity = (6.112 * pow(2.71828,((17.67 * Temperature)/(Temperature + 243.5))) * Humidity * 2.1674) / (273.15 + Temperature);
Serial.print(AbsoluteHumidity); Serial.print("\t"); Serial.print(Temperature); Serial.print("\t"); Serial.println(Humidity); Serial.print("\t"); Serial.println(bme.getPressure_MB());

sx3000:
stumbled over this chart which is showing how much water in g/m3 air can contain at maximum at a certain temperature and multiply it with the measured RH.
This might not be the most scientific way to calculate, but maybe it's accurate enough to keep an eye on the levels in the garret/boxroom?

Well, that sounds like a reasonable approximation, but only you can know if it is accurate enough. It would be easy enough to check.

sx3000:
Okay, so I came to the conclusion that using the chart and IF strings, this will generate massive amount of code.

A "massive amount of code" compared to the code for the rather complex formula? Does the amount of code really matter? It's just cut/paste/change a number. Are you running out of memory?

Regarding the approximate method:

Do you need to do the entire table (from -10 to +30)? It might be simpler if you use a lookup table.

Or, if you want to stay with the if/else if approach, a cleaner way than what you've been doing (IMHO) would be to use the if/else if statement only to assign the multipliers to a variable and then do the calculation and printing after that statement.

Or, perhaps even simpler yet, fit a curve through the data of the table (in the range you want to use), and dispense with the if/else ifs ('course, that would be an approximation to an approximation... :slight_smile: ) For example, a straight line (y=mx+b) might be just fine for a small temperature range. A polynomial with more terms might fit a wider range.

Just an off the wall way of measuring absolute amount of water vapor.
If you know the temperature and the barometric pressure, one can split
one of those ping sensors to measure the velocity of sound.
(transmitter and receiver at ends of a tube )
Knowing that one can calculate, from the change in density, the ratio
of water vapor to air. The velocity of sound is directly related to the
density. Density is related to the temperature and pressure. :wink:
Dwight

In fact for dry air the velocity of sound depends strongly on the temperature , and not significantly on the pressure or (as many believe) the altitude.

Humidity has a tiny effect (much less than 1% change in velocity from 0 to 100% saturation) due to the change in the average mass of the "air molecules" and would probably not be measurable using an Arduino and ultrasonic transducers.

It seems Remington is right. Not much good there.
There is an interesting web page on the subject:

It does seem that the humidity does have a significant effect
on the loss of amplitude of sound over some distance.
There is a chart showing significant loss of 10KHz over 30 meters
One wonders about higher frequencies, say 32KHz or even 100KHz.
There is a knee in the curve at lower humidities. That would
make an ambiguous reading at lower humidity.
Interesting.
Dwight

jremington:
In fact for dry air the velocity of sound depends strongly on the temperature , and not significantly on the pressure or (as many believe) the altitude.

Humidity has a tiny effect (much less than 1% change in velocity from 0 to 100% saturation) due to the change in the average mass of the "air molecules" and would probably not be measurable using an Arduino and ultrasonic transducers.

And indoors air you are breathing will have an increasing and unknown amount of CO2 replacing the O2 and
reducing the speed of sound, which might mask the signal from H2O which increases the speed.

Using relative humidity sensor and temperature is limited by the poor accuracy of polymer relative
humidity sensor, but I think the precise methods are things like IR spectroscopy which are neither
simple or cheap.

Isn't the DHT22 Temperature and Humidity a better option? I remember that the Arduino library has already a method for absolute humidity calculation and hum index also, or maybe i'm wrong ...

jremington:
In fact for dry air the velocity of sound depends strongly on the temperature , and not significantly on the pressure or (as many believe) the altitude.

Humidity has a tiny effect (much less than 1% change in velocity from 0 to 100% saturation) due to the change in the average mass of the "air molecules" and would probably not be measurable using an Arduino and ultrasonic transducers.

It would be best measured by tracking the resonant frequency of something like an organ tube,
since frequency is easy to measure - but you would then have to use an Arduino that has a quartz
crystal as system clock, not a ceramic resonator. And accurately measure the temperature of the actual
air in the tube. And you might have to account for CO2 if this was in a confined area with humans
breathing nearby.

Since the dewpoint is directly proportional to the water content, this formula might get you somewhere:

dew = 243.04 * (log(relhumidity / 100.0) + ((17.625 * temperature) / (243.04 + temperature))) / (17.625 - log(relhumidity / 100.0) - ((17.625 * temperature) / (243.04 + temperature)));

Everything in °C or hPa.

Just my two cents:

This can be a good source of theory in the subject:

Rotronic Measurement Academy

Good luck!

There's a sensible discussion of obtaining absolute humidity figures from relative humidity numbers in...

I say "sensible", because it goes some way towards explaining "the right" answer, but ALSO gives a less complex route to a "good enough" answer.

Are you sure you really WANT the absolute humidity?

I believe that if you are worried about things in the attic being damaged by dampness, relative humidty is what you want... but not sure about this. Other people's thoughts?

I believe that people who store rare books want to achieve a steady temperature AND steady RELATIVE humidity. Of course, if they achieve the former, the differences in the latter become less complex.