RH/Temp Sensor Example Code


Closed Thread
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2003
    Posts
    2,405

    Default RH/Temp Sensor Example Code

    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_mo...ew_sensors.htm

    I thought someone here may find some use for portions of a test routine I put together, so here it is.
    Code:
      ' 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;
    Code:
        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.
    Last edited by Bruce; - 17th March 2006 at 16:17.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  2. #2
    Join Date
    Mar 2006
    Location
    Garden Grove, Ca
    Posts
    5


    Did you find this post helpful? Yes | No

    Default RH/Temp Sensor

    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

  3. #3
    Join Date
    Sep 2003
    Location
    INDIA
    Posts
    161


    Did you find this post helpful? Yes | No

    Default

    Thanks Bruce, helped me too.

    regards

  4. #4
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    425


    Did you find this post helpful? Yes | No

    Default How accurate are these?

    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.

  5. #5
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    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.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  6. #6
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    425


    Did you find this post helpful? Yes | No

    Default

    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

  7. #7
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    425


    Did you find this post helpful? Yes | No

    Default

    I forgot to mention, maybe I can buy a PIC with 2 USART ports and could solve the problem also.

  8. #8
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    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.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

Similar Threads

  1. Reading in Manchester code
    By brid0030 in forum Code Examples
    Replies: 0
    Last Post: - 10th March 2009, 21:55
  2. How much code space do PBP statements use.
    By Darrel Taylor in forum Code Examples
    Replies: 5
    Last Post: - 13th February 2009, 21:31
  3. Loop with two motor and 2 sensors
    By MrRoboto in forum mel PIC BASIC
    Replies: 4
    Last Post: - 8th December 2008, 23:40
  4. Making Program Code Space your playground...
    By Melanie in forum Code Examples
    Replies: 15
    Last Post: - 19th July 2008, 08:26
  5. Re-Writing IF-THEN-AND-ENDIF code?
    By jessey in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 18th August 2006, 17:23

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts