Help w/ programming large equation
AGCB
Posts: 327
in Propeller 1
In my weather recording program (see "Read 8 switches w/ least pins" in General Discussion), to convert humidity to dew point there is a rather large equation listed in the data sheet for the HTU21D Humidity sensor .
I was hoping someone would help me with coding this in Propeller SPIN. I assume this is possible to do.
I can already get the binary output from the sensor but lack the ability to use the dew point formula with Propeller.
I could not figure out a way to attach the page directly to this post so please search " HTU21D " Page 16!
This is code I have so far
Thanks much for your time.
Aaron
I was hoping someone would help me with coding this in Propeller SPIN. I assume this is possible to do.
I can already get the binary output from the sensor but lack the ability to use the dew point formula with Propeller.
I could not figure out a way to attach the page directly to this post so please search " HTU21D " Page 16!
This is code I have so far
CON {{ Play w/ and learn humidity sensor 2nd try w/ I2C spin driver 1.4 }} _clkmode = xtal1 + pll16x _xinfreq = 5_000_000 'I2C pins sclpin = 20 sdapin = 21 Bitrate = 100_000 'I2C @ 100k Hum = $40 ' 7-bit I2C address '%100_0000 OBJ tv : "tv_text" i2c : "I2C SPIN driver v1.4od" VAR long address, addr0,temp0, humid, RH long addrs0[2], Humi[2], temp[2] byte timbuf[7] PUB main tv.start(0) I2C.init(sclpin, sdapin) tv.str(string("user register bits = ")) I2C.command(Hum,$E7) tv.bin(I2C.readNext(Hum),8) repeat tv.cr tv.cr tv.str(string("msb + lsb = ")) tv.bin(I2C.readWordB(Hum,$E5),16) tv.cr tv.cr humid:=(I2C.readWordB(Hum,$E5)) RH:=((humid*125/65536)-6) 'I would rather have Dew Point than relative humidity tv.str(string("RH = ")) tv.dec(RH) waitcnt(clkfreq+cnt) tv.home
Thanks much for your time.
Aaron
Comments
I haven't looked at the datasheet myself yet but I will soon. I just wanted to post a link to the datasheet (pdf) to make it easier for others to find. Here's the part at DigiKey.
Here's the equation on page 16.
I generally avoid using floating point math but this might be one of those those times using a floating point library like F32 would be a good idea.
I'll likely post a floating point example a bit later.
(To insert a link use the little chain icon between the YouTube icon and the quote icon.)
As one can likely guess, the variables with a "f" prefix are encoded as floating point numbers.
Here's some code I wrote to help display these values.
I find the method "ReadableBin" very useful. IMO, it's a lot easier to read.
Than to read:
The "DecPoint" method makes it easy to display scaled integers.
I used a different I2C object. I used the one I was using in a different project since I already had it open in the Propeller Tool.
I also used PST to display the data. Hopefully it won't take long to change it back to using a TV output.
I figured some type of float object would have to be used but what threw me the most was the log10. I didn't think that would be in the float object as I'm not too familiar with that. Bad me for failing to look! Also, I took shop math in HS, 45 years ago.
I'll spend some time w/ your code as soon as I can.
I don't know who the author is but the object "I2C devices found" has helped me debug and check hardware more than a few times. Thanks to the author whoever you are! In this case I had 2 devices on the board with the same address specified ($40) which instead should have been %100_0000 and %10_0000
Aaron
Marty
Aaron
fTemperature:= F32.FAdd(F32.FMul(F32.FDiv(9.0,5.0),fTemperature),32.0) ' degrees F[/code]
You seem to have a typo
A. House painting - cross the dew point and the house you are painting may turn into a nightmare.
B. Packaging of iron products for shipping -- Passing the dew point after packaging and everything might arrive rusty.
C. Shipment of orchids over long-distance -- Crossing the dew point after placed in a cargo container many ruin the whole shipment in transist. Orchids love high humidity and warmth, but get brown spots from water droplets on their surface.
D. Food packaging -- Too much humidity while packaging, and the food quality is stale when opened.
E. Spray paint your car in a controlled environment and moving it outside may cross the dew point and ruin the paint work.
+++++++++
I've actually found that 26 to 27 degrees centigrade is optimal air conditioning for me as the humidity content of the air remains low enough not to cause slim molds to grow on my walls, while high enough to save a bit of money. I grew up with the idea that 20 degrees centigrade was optimal.
What sort of units does the temperature need to be in in order to use it in the partial pressure calculation?
All calculations in the datasheet are done with Celcius, so it it is likely that the A, B & C constants are adjusted for Celcius. I would to the conversion for dew point to Fahrenheit afterwards and use ftemperature (small t) for the calculations and fTemperature (capital T) for display only
Isn't SPIN case insensitive?
Yes, it is case insensitive. Even if it weren't, it would still be a bad idea to have two variables whose names differ only in case because it would confuse people.
I keep forgotting that , have not used Spin in awhile and too many other languages in between
I had missed Mike Green's earlier post. (I'm still trying to figure out how I didn't see it sooner.)
I hope you compare my equation against what Mike wrote.
I've attached both the current code and a screenshot of the PST
THIS is the PST screen
The partial pressure never changes and I don't know what it should be.
The dew point is what's way off. Everything else is close. I have the unit sitting in a window sill on a 40 degree F rainy day
The method "GetPartialPressure" expects the temperature to be an integer.
When the method uses the "FFloat" method on a value which is already encoded as a float the resulting value is not a useful value.
The first thing to try is to change the "GetPartialPressure" method to:
Seems to me I had commented out that line several times but with the unit in the house it was probably enough to give some strange unexpected readings. Now with it sitting on the window sill, the temperature is about 3 degrees above my other outdoor thermometer. I would expect the dew point to be 4 - 7 degrees F below the temperature because there is no fog but it's not too far from it. It's reading at about -2 degrees C.
The local airport (3 miles away) shows 42 degrees F(5.6C), 4.4 degrees C dew point, and 96% RH but no fog.
So it's in the ball park but probably just not accurate enough.
And here's the airport METAR
I kind of wonder if you're getting rounding errors with all those floating point calculations. I don't see any obvious way around the problem.
You ought to try printing out the raw data needed to make the calculation and punch the numbers in on a good calculator. This should let you know if the problem is in rounding errors or not.