PDA

View Full Version : RH/Temp Sensor Example Code



Bruce
- 17th March 2006, 06:54
Someone asked me a few questions recently on using one of these new RH/Temp sensors, so,
of course, I had to have one to mess with & grabbed one for testing.

These are really nice little units for anyone looking to measure relative humidity & temperature.

http://www.preconusa.com/humidity_moisture_dew_sensors.htm

I thought someone here may find some use for portions of a test routine I put together, so here it is.


' MPASMWIN V5.02 assembler. PBP V2.46a 674 words.
DEFINE LOADER_USED 1
DEFINE OSC 4
DEFINE HSER_TXSTA 24H ' TX enabled, 8-bit, BRGH=1 for high-speed
DEFINE HSER_SPBRG 25 ' Set baud rate generator for 9600bps @4MHz
DEFINE HSER_RCSTA 90H ' USART enabled, 8-bit, continuous receive
TEMP VAR BYTE[12] ' Array for sensor data
SIGN VAR TEMP(7) ' Alias to TEMP(7). Sensors ± temp sign bit location
RESULTF VAR WORD ' For °C to °F conversion
DEG CON 176 ' ASCII ° symbol for degrees
NegC CON "-" ' -°C is represented by the ASCII "-" symbol
' Note: Positive temp is represented by an ASCII space
' Sensor output = 9600bps ASCII format as: H xx.x T±xx.x CR

' Program serial output 9600bps ASCII format: RH=11.5% : TEMP=21.8°C : 71.24°F
MAIN:
PAUSE 1000 ' Let everything initialize & delay between display updates

' Synch with HS-200DD RH/Temp sensor ASCII H character output
SERIN2 PORTB.0,84,[WAIT("H"),STR TEMP\12] ' HS-2000DD input on RB0

' Parse & display relative humidity reading from HS-200DD sensor
HSEROUT ["RH=",TEMP(1),TEMP(2),TEMP(3),TEMP(4),"% : "]

' Parse & display temperature reading from HS-2000DD sensor
HSEROUT ["TEMP=",TEMP(8),TEMP(9),TEMP(10),TEMP(11),DEG,"C : "]

' Parse & remove ASCII component & build whole integer number
RESULTF = ((TEMP(8)-48)*100)+((TEMP(9)-48)*10)+(TEMP(11)-48)

' I.E. ASCII temp "24.9" now = 249d for a useable whole integer number.

' Now take ((resultf * 18)/100)+32 for whole number, and resultf // 100
' for the remainder.

' Example: 41.0°C = 105.8°F. ((410*18)/100)+32 = whole the number result.
' 7,380/100 = 73. We're dropping the .8 and adding 73 + 32 for 105.
' Then with RESULTF//100 we end up with 8 as the remainder for 105.8°F

RESULTF = RESULTF * 18 ' °C * 9/5
IF Sign = NegC THEN Negative ' If -°C jump to -result handler

' This handles everything from 99.9°C (211.82°F) to 00.0°C (32.0°F)
HSEROUT [DEC (RESULTF/100)+32,".",DEC2 RESULTF//100,DEG,"F",13,10]
GOTO MAIN

Negative:
' The first section handles everything from -00.1°C (31.82°F) to -17.7°C (0.14°F)
' which is still +°F.
IF RESULTF < 3200 THEN
HSEROUT [DEC (3200-RESULTF)/100,".",DEC2 (3200-RESULTF)//100,DEG,"F",13,10]
ELSE
' The 2nd section handles everything from -17.8°C (-0.04°F) to -99.9°C (-147.82°F)
' Note how we reverse the subtraction here on -°F.
HSEROUT ["-",DEC (RESULTF-3200)/100,".",DEC2 (RESULTF-3200)//100,DEG,"F",13,10]
ENDIF
GOTO MAIN

END
And if you don't have one of these nifty gadgets, but would like to run the conversion routines with a
serial terminal emulator, here's a chopped version;


DEFINE LOADER_USED 1
DEFINE OSC 4
DEFINE HSER_TXSTA 24H ' TX enabled, 8-bit, BRGH=1 for high-speed
DEFINE HSER_SPBRG 25 ' Set baud rate generator for 9600bps @4MHz
DEFINE HSER_RCSTA 90H ' USART enabled, 8-bit, continuous receive
TEMP VAR BYTE(5) ' Array for simulated sensor temp input
RESULTF VAR WORD ' For °C to °F conversion
DEG CON 176 ' ASCII value for ° symbol
SIGN VAR TEMP(0) ' TEMP(0) holds the sign indicator
NEG CON "-" ' Negative °C indicator
' Positive °C indicator = " " ASCII space

HSEROUT ["Input H then +xx.x or -xx.x temp to convert",13,10]
' Enter H-19.7 for the conversion result of -19.7°C to °F

Main:
HSERIN [WAIT("H"),STR TEMP\5]
HSEROUT [TEMP(0),TEMP(1),TEMP(2),TEMP(3),TEMP(4),DEG,"C = "]
' Temp input to convert starts at TEMP(1). TEMP(0) = sign indicator
' TEMP(3) is the decimal point
RESULTF = ((TEMP(1)-48)*100)+((TEMP(2)-48)*10)+(TEMP(4)-48)
GOSUB Test
GOTO Main

Test:
' This handles everything from 00.0°C (32.0°F) to 99.9°C (211.82°F)
RESULTF = RESULTF * 18 ' °F = (1.8*°C)+32
IF Sign = NEG THEN Negative
HSEROUT [DEC (RESULTF/100)+32,".",DEC2 RESULTF//100,DEG,"F",13,10]
RETURN

Negative:
' The first section handles everything from -00.1°C (31.82°F) to -17.7°C (0.14°F)
IF RESULTF < 3200 THEN
HSEROUT [DEC (3200-RESULTF)/100,".",DEC2 (3200-RESULTF)//100,DEG,"F",13,10]
ELSE
' The 2nd section handles everything from -17.8°C (-0.04°F) to -99.9°C (-147.82°F)
HSEROUT ["-",DEC (RESULTF-3200)/100,".",DEC2 (RESULTF-3200)//100,DEG,"F",13,10]
ENDIF
RETURN

END
Enter H+69.9 to view the °C to °F conversion for 69.9°C to °F.
Enter H-69.9 to see the result of -69.9°C to °F.

It should work for °C to °F conversion of values from 99.9°C to -99.9°C.

Joval
- 26th March 2006, 06:41
Bruce,
I just can't believe it!!!
Just two days ago I was asked if I would be interested in playing with some of these units, both the 2000V and D.
New to the forum, I came across the code example you posted and I just couldn't believe it.
I am not very good at coding, but have learned a lot thru the forum.
Reading the code comments and tying that with the info in the documentation has help greatly.
Now all I have to do is figure which PIC/s to use.

Thanks a million!!!

Joe

charudatt
- 23rd April 2006, 08:22
Thanks Bruce, helped me too.

regards

Christopher4187
- 14th June 2006, 15:18
Bruce,

I may use these for my project. From your experience, how accurate are these sensors? It looks like you don't need a vref voltage which is really good, unless I am mistaken.

Bruce
- 14th June 2006, 15:45
Hi Christopher,

I haven't tested with this one for accuracy, but it's one of the easiest to use RH/temp sensors I've seen in a while.

This one has an onboard processor that sends RH & temp out in standard ASCII format. No need for anything analog. It's real simple to use.

Christopher4187
- 15th June 2006, 15:35
Bruce,

I've ordered three temp sensors, two digital and one analog; they should be here tomorrow. I have some additional questions about your code.

1. I am using the USART port on the PIC for something else so I am pretty sure I can use the temp sensor on another pin for serout/serin but does the code have to be manipulated a lot? Instead of writing:

HSEROUT ["RH=",TEMP(1),TEMP(2),TEMP(3),TEMP(4),"% : "]

Can I write

Serout2 portb.1, 396, ["RH=",TEMP(1),TEMP(2),TEMP(3),TEMP(4),"% : "]

2. The result will be something like 98.3 or 104.6 or 35.4 rather than just 35 or 45 or 198? Is this correct?

Thanks,

Chris

Christopher4187
- 15th June 2006, 15:43
I forgot to mention, maybe I can buy a PIC with 2 USART ports and could solve the problem also.

Bruce
- 15th June 2006, 16:55
Hi Chris,

You won't need a PIC with a hardware USART, and you can use any I/O-pin or
serial command you prefer. I just used the USART since I had a board with a
hardware serial connection for the test.