Sensirion Temperature/Humidity Sensor on off
Dmagee
Posts: 20
This is the first time that I have used this sensor so I used the sample code to test the sensor out. The sensor worked fine so I decided so try to have it turn a led on and off at a set humidity. I've tryed some IF THEN and other codes that i thought would work but nothing would control the led like I wanted. The (rhTrue) is what I was trying to monitor. I'm sure that there is an easy way of doing this, I'm just not seeing it.
any help is appreciated.
[code]
' {$STAMP BS2}
' {$PBASIC 2.5}
' ==============================================================================
'
' File...... SHT11.BS2
' Purpose... Interface to Sensirion SHT11 temperature/humidity sensor
' E-mail.... stamptech@parallaxinc.com
' Date...... 12/30/02
'
'
'
' ==============================================================================
'
' Program Description
'
'
' This program demonstrates the interface and conversion of SHT11 data to
' usable program values.
'
' For detailed information on the use and application of the ** operator,
' see Tracy Allen's web page at this link:
'
' -- http://www.emesystems.com/BS2math1.htm
'
' For Tracy's SHT1x code [very advanced]:
'
' -- http://www.emesystems.com/OL2sht1x.htm
'
' For SHT11/15 documentation and app notes, visit:
'
' -- http://www.sensirion.com
'
' I/O Definitions
'
ShtData CON 1 ' bi-directional data
Clock CON 0
'
' Constants
'
ShtTemp CON %00011 ' read temperature
ShtHumi CON %00101 ' read humidity
ShtStatW CON %00110 ' status register write
ShtStatR CON %00111 ' status register read
ShtReset CON %11110 ' soft reset (wait 11 ms after)
Ack CON 0
NoAck CON 1
No CON 0
Yes CON 1
MoveTo CON 2 ' for DEBUG control
ClrRt CON 11 ' clear DEBUG line to right
DegSym CON 186 ' degrees symbol for DEBUG
'
' Variables
'
ioByte VAR Byte ' data from/to SHT1x
ackBit VAR Bit ' ack/nak from/to SHT1x
toDelay VAR Byte ' timeout delay timer
timeOut VAR Bit ' timeout status
soT VAR Word ' temp counts from SHT1x
tC VAR Word ' temp - celcius
tF VAR Word ' temp - fahrenheit
soRH VAR Word ' humidity counts from SHT1x
rhLin VAR Word ' humidity; linearized
rhTrue VAR Word ' humidity; temp compensated
status VAR Byte ' SHT1x status byte
'
' EEPROM Data
'
'
' Initialization
'
Initialize:
GOSUB SHT_Connection_Reset ' reset device connection
PAUSE 250 ' let DEBUG window open
DEBUG CLS
DEBUG "SHT1x Demo", CR
DEBUG "
", CR
' GOTO Main ' skip heater demo
'
' Program Code
'
Sensor_Demo:
GOSUB SHT_Measure_Temp
DEBUG MoveTo, 0, 3
DEBUG "tF...... "
DEBUG DEC (tF / 10), ".", DEC1 tF, DegSym, ClrRt, CR
GOSUB SHT_Measure_Humidity
DEBUG "rhLin... "
DEBUG DEC (rhLin / 10), ".", DEC1 rhLin, "%", ClrRt, CR, CR
Heater_On:
DEBUG "SHT1x heater on", CR
status = %00000100 ' heater bit = On
GOSUB SHT_Write_Status
DEBUG "Waiting 2 seconds", CR
PAUSE 2000
Heater_Off:
DEBUG "SHT1x heater off", CR, CR
status = %00000000 ' heater bit = Off
GOSUB SHT_Write_Status
GOSUB SHT_Measure_Temp
DEBUG "tF...... "
DEBUG DEC (tF / 10), ".", DEC1 tF, DegSym, ClrRt, CR
GOSUB SHT_Measure_Humidity
DEBUG "rhLin... "
DEBUG DEC (rhLin / 10), ".", DEC1 rhLin, "%", ClrRt, CR, CR
PAUSE 5000
Main:
DEBUG CLS
DEBUG "SHT1x Demo", CR
DEBUG "
", CR
Main2:
GOSUB SHT_Measure_Temp
DEBUG MoveTo, 0, 3
DEBUG "soT...... "
DEBUG DEC soT, ClrRt, CR
DEBUG "tC....... "
DEBUG DEC (tC / 10), ".", DEC1 tC, DegSym, ClrRt, CR
DEBUG "tF....... "
DEBUG DEC (tF / 10), ".", DEC1 tF, DegSym, ClrRt
GOSUB SHT_Measure_Humidity
DEBUG MoveTo, 0, 7
DEBUG "soRH..... "
DEBUG DEC soRH, ClrRt, CR
DEBUG "rhLin.... "
DEBUG DEC (rhLin / 10), ".", DEC1 rhLin, "%", ClrRt, CR
DEBUG "rhTrue... "
DEBUG DEC (rhTrue / 10), ".", DEC1 rhTrue, "%", ClrRt
PAUSE 1000
GOSUB Humidity_Switch
GOTO Main2 ' minimum delay between readings
END
'
' Subroutines
'
' connection reset: 9 clock cyles with ShtData high, then start sequence
'
SHT_Connection_Reset:
SHIFTOUT ShtData, Clock, LSBFIRST, [$FFF\9]
' generates SHT1x "start" sequence
' _____ _____
' ShtData |_______|
' ___ ___
' Clock ___| |___| |___
'
SHT_Start:
INPUT ShtData ' let pull-up take line high
LOW Clock
HIGH Clock
LOW ShtData
LOW Clock
HIGH Clock
INPUT ShtData
LOW Clock
RETURN
' measure temperature
' -- celcius = soT * 0.01 - 40
' -- fahrenheit = soT * 0.018 - 40
'
SHT_Measure_Temp:
GOSUB SHT_Start ' alert device
ioByte = ShtTemp ' temperature command
GOSUB SHT_Write_Byte ' send command
GOSUB SHT_Wait ' wait until measurement done
ackBit = Ack ' another read follows
GOSUB SHT_Read_Byte ' get MSB
soT.HIGHBYTE = ioByte
ackBit = NoAck ' last read
GOSUB SHT_Read_Byte ' get LSB
soT.LOWBYTE = ioByte
' Note: Conversion factors are multiplied by 10 to return the
' temperature values in tenths of degrees
tC = soT / 10 - 400 ' convert to tenths C
tF = soT ** 11796 - 400 ' convert to tenths F
RETURN
' measure humidity
'
SHT_Measure_Humidity:
GOSUB SHT_Start ' alert device
ioByte = ShtHumi ' humidity command
GOSUB SHT_Write_Byte ' send command
GOSUB SHT_Wait ' wait until measurement done
ackBit = Ack ' another read follows
GOSUB SHT_Read_Byte ' get MSB
soRH.HIGHBYTE = ioByte
ackBit = NoAck ' last read
GOSUB SHT_Read_Byte ' get LSB
soRH.LOWBYTE = ioByte
' linearize humidity
' rhLin = (soRH * 0.0405) - (soRH^2 * 0.0000028) - 4
'
' for the BASIC Stamp:
' rhLin = (soRH * 0.0405) - (soRH * 0.004 * soRH * 0.0007) - 4
'
' Conversion factors are multiplied by 10 and then rounded to
' return tenths
'
rhLin = (soRH ** 26542)
rhLin = rhLin - ((soRH ** 3468) * (soRH ** 3468) + 50 / 100)
rhLin = rhLin - 40
' temperature compensated humidity
' rhTrue = (tC - 25) * (soRH * 0.00008 + 0.01) + rhLin
'
' Conversion factors are multiplied by 100 to improve accuracy and then
' rounded off.
'
rhTrue = ((tC / 10 - 25) * (soRH ** 524 + 1) + (rhLin * 10)) + 5 / 10
RETURN
' sends "status"
'
SHT_Write_Status:
GOSUB SHT_Start ' alert device
ioByte = ShtStatW ' write to status reg command
GOSUB SHT_Write_Byte ' send command
ioByte = status
GOSUB SHT_Write_Byte
RETURN
' returns "status"
'
SHT_Read_Status:
GOSUB SHT_Start ' alert device
ioByte = ShtStatW ' write to status reg command
GOSUB SHT_Read_Byte ' send command
ackBit = NoAck ' only one byte to read
GOSUB SHT_Read_Byte
RETURN
' sends "ioByte"
' returns "ackBit"
'
SHT_Write_Byte:
SHIFTOUT ShtData, Clock, MSBFIRST, [ioByte] ' send byte
SHIFTIN ShtData, Clock, LSBPRE, [ackBit\1] ' get ack bit
RETURN
' returns "ioByte"
' sends "ackBit"
'
SHT_Read_Byte:
SHIFTIN ShtData, Clock, MSBPRE, [ioByte] ' get byte
SHIFTOUT ShtData, Clock, LSBFIRST, [ackBit\1] ' send ack bit
INPUT ShtData ' release data line
RETURN
' wait for device to finish measurement (pulls data line low)
' -- timeout after ~1/4 second
'
SHT_Wait:
INPUT ShtData ' data line is input
FOR toDelay = 1 TO 250 ' give ~1/4 second to finish
timeOut = INS.LOWBIT(ShtData) ' scan data line
IF (timeOut = No) THEN SHT_Wait_Done ' if low, we're done
PAUSE 1
NEXT
SHT_Wait_Done:
RETURN
' reset SHT1x with soft reset
'
SHT_Soft_Reset:
GOSUB SHT_Connection_Reset ' reset the connection
ioByte = ShtReset ' reset command
ackBit = NoAck ' only one byte to send
GOSUB SHT_Write_Byte ' send it
PAUSE 11 ' wait at least 11 ms
RETURN
Relay_on:
IF rhTrue = < 63 THEN LOW 12
PAUSE 1000
IF rhTrue = > 65 THEN HIGH 12
RETURN
Humidity_low:
HIGH 12
LOW 14
PAUSE 11
RETURN
Humidity_High:
LOW 12
HIGH 14
PAUSE 11
RETURN
Humidity_Switch:
IF (rhTrue = >63) THEN
HIGH 12
LOW 14
PAUSE 500
ELSEIF rhTrue = <63 THEN
LOW 12
HIGH 14
PAUSE 500
ENDIF
RETURN
[/code]
any help is appreciated.
[code]
' {$STAMP BS2}
' {$PBASIC 2.5}
' ==============================================================================
'
' File...... SHT11.BS2
' Purpose... Interface to Sensirion SHT11 temperature/humidity sensor
' E-mail.... stamptech@parallaxinc.com
' Date...... 12/30/02
'
'
'
' ==============================================================================
'
' Program Description
'
'
' This program demonstrates the interface and conversion of SHT11 data to
' usable program values.
'
' For detailed information on the use and application of the ** operator,
' see Tracy Allen's web page at this link:
'
' -- http://www.emesystems.com/BS2math1.htm
'
' For Tracy's SHT1x code [very advanced]:
'
' -- http://www.emesystems.com/OL2sht1x.htm
'
' For SHT11/15 documentation and app notes, visit:
'
' -- http://www.sensirion.com
'
' I/O Definitions
'
ShtData CON 1 ' bi-directional data
Clock CON 0
'
' Constants
'
ShtTemp CON %00011 ' read temperature
ShtHumi CON %00101 ' read humidity
ShtStatW CON %00110 ' status register write
ShtStatR CON %00111 ' status register read
ShtReset CON %11110 ' soft reset (wait 11 ms after)
Ack CON 0
NoAck CON 1
No CON 0
Yes CON 1
MoveTo CON 2 ' for DEBUG control
ClrRt CON 11 ' clear DEBUG line to right
DegSym CON 186 ' degrees symbol for DEBUG
'
' Variables
'
ioByte VAR Byte ' data from/to SHT1x
ackBit VAR Bit ' ack/nak from/to SHT1x
toDelay VAR Byte ' timeout delay timer
timeOut VAR Bit ' timeout status
soT VAR Word ' temp counts from SHT1x
tC VAR Word ' temp - celcius
tF VAR Word ' temp - fahrenheit
soRH VAR Word ' humidity counts from SHT1x
rhLin VAR Word ' humidity; linearized
rhTrue VAR Word ' humidity; temp compensated
status VAR Byte ' SHT1x status byte
'
' EEPROM Data
'
'
' Initialization
'
Initialize:
GOSUB SHT_Connection_Reset ' reset device connection
PAUSE 250 ' let DEBUG window open
DEBUG CLS
DEBUG "SHT1x Demo", CR
DEBUG "
", CR
' GOTO Main ' skip heater demo
'
' Program Code
'
Sensor_Demo:
GOSUB SHT_Measure_Temp
DEBUG MoveTo, 0, 3
DEBUG "tF...... "
DEBUG DEC (tF / 10), ".", DEC1 tF, DegSym, ClrRt, CR
GOSUB SHT_Measure_Humidity
DEBUG "rhLin... "
DEBUG DEC (rhLin / 10), ".", DEC1 rhLin, "%", ClrRt, CR, CR
Heater_On:
DEBUG "SHT1x heater on", CR
status = %00000100 ' heater bit = On
GOSUB SHT_Write_Status
DEBUG "Waiting 2 seconds", CR
PAUSE 2000
Heater_Off:
DEBUG "SHT1x heater off", CR, CR
status = %00000000 ' heater bit = Off
GOSUB SHT_Write_Status
GOSUB SHT_Measure_Temp
DEBUG "tF...... "
DEBUG DEC (tF / 10), ".", DEC1 tF, DegSym, ClrRt, CR
GOSUB SHT_Measure_Humidity
DEBUG "rhLin... "
DEBUG DEC (rhLin / 10), ".", DEC1 rhLin, "%", ClrRt, CR, CR
PAUSE 5000
Main:
DEBUG CLS
DEBUG "SHT1x Demo", CR
DEBUG "
", CR
Main2:
GOSUB SHT_Measure_Temp
DEBUG MoveTo, 0, 3
DEBUG "soT...... "
DEBUG DEC soT, ClrRt, CR
DEBUG "tC....... "
DEBUG DEC (tC / 10), ".", DEC1 tC, DegSym, ClrRt, CR
DEBUG "tF....... "
DEBUG DEC (tF / 10), ".", DEC1 tF, DegSym, ClrRt
GOSUB SHT_Measure_Humidity
DEBUG MoveTo, 0, 7
DEBUG "soRH..... "
DEBUG DEC soRH, ClrRt, CR
DEBUG "rhLin.... "
DEBUG DEC (rhLin / 10), ".", DEC1 rhLin, "%", ClrRt, CR
DEBUG "rhTrue... "
DEBUG DEC (rhTrue / 10), ".", DEC1 rhTrue, "%", ClrRt
PAUSE 1000
GOSUB Humidity_Switch
GOTO Main2 ' minimum delay between readings
END
'
' Subroutines
'
' connection reset: 9 clock cyles with ShtData high, then start sequence
'
SHT_Connection_Reset:
SHIFTOUT ShtData, Clock, LSBFIRST, [$FFF\9]
' generates SHT1x "start" sequence
' _____ _____
' ShtData |_______|
' ___ ___
' Clock ___| |___| |___
'
SHT_Start:
INPUT ShtData ' let pull-up take line high
LOW Clock
HIGH Clock
LOW ShtData
LOW Clock
HIGH Clock
INPUT ShtData
LOW Clock
RETURN
' measure temperature
' -- celcius = soT * 0.01 - 40
' -- fahrenheit = soT * 0.018 - 40
'
SHT_Measure_Temp:
GOSUB SHT_Start ' alert device
ioByte = ShtTemp ' temperature command
GOSUB SHT_Write_Byte ' send command
GOSUB SHT_Wait ' wait until measurement done
ackBit = Ack ' another read follows
GOSUB SHT_Read_Byte ' get MSB
soT.HIGHBYTE = ioByte
ackBit = NoAck ' last read
GOSUB SHT_Read_Byte ' get LSB
soT.LOWBYTE = ioByte
' Note: Conversion factors are multiplied by 10 to return the
' temperature values in tenths of degrees
tC = soT / 10 - 400 ' convert to tenths C
tF = soT ** 11796 - 400 ' convert to tenths F
RETURN
' measure humidity
'
SHT_Measure_Humidity:
GOSUB SHT_Start ' alert device
ioByte = ShtHumi ' humidity command
GOSUB SHT_Write_Byte ' send command
GOSUB SHT_Wait ' wait until measurement done
ackBit = Ack ' another read follows
GOSUB SHT_Read_Byte ' get MSB
soRH.HIGHBYTE = ioByte
ackBit = NoAck ' last read
GOSUB SHT_Read_Byte ' get LSB
soRH.LOWBYTE = ioByte
' linearize humidity
' rhLin = (soRH * 0.0405) - (soRH^2 * 0.0000028) - 4
'
' for the BASIC Stamp:
' rhLin = (soRH * 0.0405) - (soRH * 0.004 * soRH * 0.0007) - 4
'
' Conversion factors are multiplied by 10 and then rounded to
' return tenths
'
rhLin = (soRH ** 26542)
rhLin = rhLin - ((soRH ** 3468) * (soRH ** 3468) + 50 / 100)
rhLin = rhLin - 40
' temperature compensated humidity
' rhTrue = (tC - 25) * (soRH * 0.00008 + 0.01) + rhLin
'
' Conversion factors are multiplied by 100 to improve accuracy and then
' rounded off.
'
rhTrue = ((tC / 10 - 25) * (soRH ** 524 + 1) + (rhLin * 10)) + 5 / 10
RETURN
' sends "status"
'
SHT_Write_Status:
GOSUB SHT_Start ' alert device
ioByte = ShtStatW ' write to status reg command
GOSUB SHT_Write_Byte ' send command
ioByte = status
GOSUB SHT_Write_Byte
RETURN
' returns "status"
'
SHT_Read_Status:
GOSUB SHT_Start ' alert device
ioByte = ShtStatW ' write to status reg command
GOSUB SHT_Read_Byte ' send command
ackBit = NoAck ' only one byte to read
GOSUB SHT_Read_Byte
RETURN
' sends "ioByte"
' returns "ackBit"
'
SHT_Write_Byte:
SHIFTOUT ShtData, Clock, MSBFIRST, [ioByte] ' send byte
SHIFTIN ShtData, Clock, LSBPRE, [ackBit\1] ' get ack bit
RETURN
' returns "ioByte"
' sends "ackBit"
'
SHT_Read_Byte:
SHIFTIN ShtData, Clock, MSBPRE, [ioByte] ' get byte
SHIFTOUT ShtData, Clock, LSBFIRST, [ackBit\1] ' send ack bit
INPUT ShtData ' release data line
RETURN
' wait for device to finish measurement (pulls data line low)
' -- timeout after ~1/4 second
'
SHT_Wait:
INPUT ShtData ' data line is input
FOR toDelay = 1 TO 250 ' give ~1/4 second to finish
timeOut = INS.LOWBIT(ShtData) ' scan data line
IF (timeOut = No) THEN SHT_Wait_Done ' if low, we're done
PAUSE 1
NEXT
SHT_Wait_Done:
RETURN
' reset SHT1x with soft reset
'
SHT_Soft_Reset:
GOSUB SHT_Connection_Reset ' reset the connection
ioByte = ShtReset ' reset command
ackBit = NoAck ' only one byte to send
GOSUB SHT_Write_Byte ' send it
PAUSE 11 ' wait at least 11 ms
RETURN
Relay_on:
IF rhTrue = < 63 THEN LOW 12
PAUSE 1000
IF rhTrue = > 65 THEN HIGH 12
RETURN
Humidity_low:
HIGH 12
LOW 14
PAUSE 11
RETURN
Humidity_High:
LOW 12
HIGH 14
PAUSE 11
RETURN
Humidity_Switch:
IF (rhTrue = >63) THEN
HIGH 12
LOW 14
PAUSE 500
ELSEIF rhTrue = <63 THEN
LOW 12
HIGH 14
PAUSE 500
ENDIF
RETURN
[/code]
Comments
Thanks.
http://youtu.be/imJc_reASCE