Shop OBEX P1 Docs P2 Docs Learn Events
Simple Sensirion Sht 11 Code — Parallax Forums

Simple Sensirion Sht 11 Code

NWCCTVNWCCTV Posts: 3,629
edited 2014-07-16 12:21 in Propeller 1
For the Basic Stamp there is code that simply displays the Temperature and Humidity. I am looking for something like this in Spin. All of the demos seem to have all sorts of data displayed that I do not need. Before I start modifying the demos, I thought I would ask if anyone already has code that will display the current temperature and humidity only. Thanks.

Comments

  • Tracy AllenTracy Allen Posts: 6,664
    edited 2014-07-13 21:04
    I posted sensirion_integer <http://obex.parallax.com/object/517>.

    There are two demos. The first simply shows how to read the values and display them using Parallax Serial Terminal. A second demo shows how to change the resolution at run time, and how to turn on and off the heater (which is hardly ever useful in practice).It also shows the conversion times in milliseconds, so that you can assess the difference in speed between the modes.
  • NWCCTVNWCCTV Posts: 3,629
    edited 2014-07-15 21:18
    Where or how do I change to Fahrenheit?
  • WBA ConsultingWBA Consulting Posts: 2,934
    edited 2014-07-15 22:44
    I dug around and found this code on my laptop, but will keep looking also. It displays temp and humidity from two SHT11 devices through PST and on VGA (any board that has VGA on pins 16-23) as well as logging to SD on P3-6. Both Clock pins are tied to P0 on the prop, but each SHT11 has it's own data pin, P1 and P2 in this code (see line 96 "repeat i from 1 to 2"). It's messy, but works well. This uses the older SHT-11 object by Cam Thompson, but should be very easy to convert to Tracy's (haven't verified myself yet).
    ''=============================================================================
    ''
    '' @file     SensirionDemo
    '' @target   Propeller
    ''
    '' Display Sensirion SHT-11 values from 2 sensors every 20 seconds on a VGA/TV display
    '' and into PST through the serial port. Values are also logged to SD using the 
    '' fsrwFemto object in a csv format file called "SHT11.log".
    '' Floating point routines are used to calculate temp and RH according to the
    '' Sensirion datasheet and application notes. All low level functions for
    '' communicating with SHT-11 are contained in the Sensirion object.
    ''
    ''   &#9472;&#9472;&#9472;SHT11_Multi_VGA_SD_002
    ''        &#9500;&#9472;&#9472;Sensirion
    ''        &#9500;&#9472;&#9472;vga_text (or tv_text)
    ''        &#9500;&#9472;&#9472;FloatString
    ''        &#9500;&#9472;&#9472;Float32
    ''        &#9500;&#9472;&#9472;FullDuplexSerialPlus.spin2
    ''        &#9492;&#9472;&#9472;fsrwFemto.spin
    ''            &#9492;&#9472;&#9472;&#9472;sdspiFemto.spin    
    ''
    '' @author   Andrew Williams, WBA Consulting
    '' 
    '' Some portions:
    '' @author   Cam Thompson, Micromega Corporation 
    '' Copyright (c) 2006 Micromega Corporation
    '' See end of file for terms of use.
    ''
    '' Some Portions adapted from:
    '' ** SD INTERFACE TRAINER **
    '' ** By Jeff Ledger       **
    ''
    ''       
    '' @version  V1.2 - 9/29/09
    '' @changes
    ''  - 01  Adapted from V1.4 of Multi_TV code, added SD OBJ, CONS, VAR
    ''  - 02  Used SD writing code from SD_Trainer to create logging section of main
    ''=============================================================================
    
    CON
      _clkmode = xtal1 + pll16x
      _xinfreq = 5_000_000
    
      ' SHT_DATA      = 1                                     ' SHT-11 data pin
      SHT_CLOCK     = 0                                     ' SHT-11 clock pin
    
      CLS         = $0                                      ' clear screen
      CR          = $D                                      ' carriage return
      Deg         = $B0                                     ' degree symbol
      
      '' SD Configuration
      spiDO     = 3   
      spiClk    = 4   
      spiDI     = 5    
      spiCS     = 6   
    
      
    VAR
       byte    SHT_DATA
    
       long ioControl[2]   ''** Variable required for SD Routines **    
       byte buff[32]       '' buffer for SD
    
    OBJ
      term          : "vga_text"
      'term          : "tv_text"
      sht           : "Sensirion"
      fp            : "FloatString"
      f             : "Float32"
    
      debug         : "FullDuplexSerialPlus"   ''Used for interaction with the Propeller
      sd            : "fsrwFemto"              ''SD Software used in FemtoBASIC
      
    PUB main | rawTemp, rawHumidity, tempC, rh, dewC, i, name, r, data
    
      debug.start(31,30,0,115200)   '' Open serial communication with PST, 115200
      waitcnt(clkfreq*10 + cnt)     '' delay (so you can get PST active), 10 secs
    
      debug.tx(Debug#CLS)
      debug.str(string("Running SHT11 Multi Program",13))
    
      term.start(16)                                        ' start VGA terminal
      'term.start(12)                                        ' start TV terminal
      f.start                                               ' start floating point object
      sd.start(@ioControl) '' ** Start the SD Routines **
      
      term.out(CLS)                                         ' 
       ' read and display SHT-11 sensor readings every 2 seconds
      repeat
        setColor(3)
        displayString(0, 0, string("      Cold enough for ya?     ", CR))  ' display title 
        setColor(0)
        term.str(string("&#9474;  Sensor  &#9474;  Temp F &#9474;  RH % &#9474;", CR))
        term.str(string("&#9500;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9532;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9532;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9508;", CR))
        'start of repeating sensor sequence
        repeat i from 1 to 2                                '2 sensors on P1, P2
          sht.start(i, SHT_CLOCK)
          rawTemp := f.FFloat(sht.readTemperature) 
          rawHumidity := f.FFloat(sht.readHumidity)  
          tempC := celsius(rawTemp)
          term.str(string("&#9474; "))
          name := lookup(i: string("Internal"), string("External"))
          term.str(name)                  ' sensor name variable
          term.str(string(" &#9474; "))
          term.str(fp.FloatToFormat(fahrenheit(tempC), 5, 1))
          term.str(string(deg, "  &#9474;")) 
          rh := humidity(tempC, rawHumidity)
          term.str(fp.FloatToFormat(rh, 5, 1))
          term.str(string("  &#9474;", CR))
          debug.str(string(13,"** Sending information to SD",13))
          sd.mount(spiDO,spiClk,spiDI,spiCS) '' Mount the SD
          sd.popen(string("SHT11.log"),"a")  '' Open logfile for append
          'sd.str(name),(string(",")),(fp.FloatToFormat(fahrenheit(tempC), 5, 1),(string(",")),(fp.FloatToFormat(rh, 5, 1)),(string(", done",13,10))      sd.str(name)
          sd.str(name)
          sd.str(string(","))
          sd.str(fp.FloatToFormat(fahrenheit(tempC), 5, 1))
          sd.str(string(","))
          sd.str(fp.FloatToFormat(rh, 5, 1))
          sd.str(string(13))
          sd.pclose  '' Close the file
          sd.unmount '' Unmount the SD
          debug.str(string(13,"** Routine Finished",13))
        term.str(string("&#9492;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9524;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9524;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9496;"))
    
        debug.str(string(13,"Waiting..."))
        waitcnt(clkfreq*20 + cnt)      ''20 seconds between reading sequences
        debug.str(string(13,"Starting in 5 seconds...",13))
        waitcnt(clkfreq*5 + cnt)      ''5 seconds
        debug.str(string(16))
    
    PUB displayString(row, col, s)
      setPosition(row, col)
      term.str(s)
    
    PUB setPosition(row, col)
      if row => 0
        term.out($B)
        term.out(row)
      if col => 0
        term.out($A)
        term.out(col)
    
    PUB setColor(c)
      term.out($C)
      term.out(c)
       
    PUB celsius(t)
      ' from SHT1x/SHT7x datasheet using value for 3.5V supply
      ' celsius = -39.66 + (0.01 * t)
      return f.FAdd(-39.66, f.FMul(0.01, t)) 
    
    PUB fahrenheit(t)
      ' fahrenheit = (celsius * 1.8) + 32
      return f.FAdd(f.FMul(t, 1.8), 32.0)
      
    PUB humidity(t, rh) | rhLinear
      ' rhLinear = -4.0 + (0.0405 * rh) + (-2.8e-6 * rh * rh)
      ' simplifies to: rhLinear = ((-2.8e-6 * rh) + 0.0405) * rh -4.0
      rhLinear := f.FAdd(f.FMul(f.FAdd(0.0405, f.FMul(-2.8e-6, rh)), rh), -4.0)
      ' rhTrue = (t - 25.0) * (0.01 + 0.00008 * rawRH) + rhLinear
      return f.FAdd(f.FMul(f.FSub(t, 25.0), f.FAdd(0.01, f.FMul(0.00008, rh))), rhLinear)
      
          
    DAT
    title   byte    CR, "   Sensirion SHT-11 Readings",0
    
    
    {{
    &#9484;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9488;
    &#9474;                                                   TERMS OF USE: MIT License                                                  &#9474;                                                            
    &#9500;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9508;
    &#9474;Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation    &#9474; 
    &#9474;files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,    &#9474;
    &#9474;modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software&#9474;
    &#9474;is furnished to do so, subject to the following conditions:                                                                   &#9474;
    &#9474;                                                                                                                              &#9474;
    &#9474;The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.&#9474;
    &#9474;                                                                                                                              &#9474;
    &#9474;THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE          &#9474;
    &#9474;WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR         &#9474;
    &#9474;COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,   &#9474;
    &#9474;ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                         &#9474;
    &#9492;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9496;
    }}
    
    
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2014-07-16 09:46
    The usual formula is degF = degC *9/5 + 32. Sensirion_integer returns temperature in units of 0.01 °C. So, if you want temperature in unts of whole degF,

    degF := rht.ReadTemperature * 9 / 500 + 32.

    There is a bit of roundoff bias in that. Is it good enough? If you want it to 0.1 degF, use
    degF := rht.ReadTemperature * 9 / 50 + 320
  • NWCCTVNWCCTV Posts: 3,629
    edited 2014-07-16 12:21
    Great. I will give this a try over the weekend. Thanks all.
Sign In or Register to comment.