Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 07:20 29 Apr 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : MM2(+): SHT11 temperature/humidity

Author Message
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 8583
Posted: 03:53am 14 Jul 2015
Copy link to clipboard 
Print this post

The SHT11 is a combined temperature and humidity sensor from Sensiron

The sensors are fairly available and sometimes show up on ebay very cheaply. They can be soldered (carefully) to an 8-pin SOIC/DIL adapter

The sensor uses a two pin interface with a clock and a data pin. This makes it easy to bit-bang from Basic. This code is converted from some Picaxe code but the floating point capability of the Micromite makes the calculations much easier.

The clock can be pulled up to VCC with a 4K7 resistor but the internal MX pullups seem to work fine.

The sensor constants in the code assume a 3.3V power supply but the datasheet referenced above gives alternate values for other supply voltages


option explicit
option default none
const SCK_PIN=1 'pin to connect SHT11 clock
const DAT_OUT_PIN=2 'pin to connect SHT11 data
const fullclock=2 'length of clock pulse in msec
const halfclock=fullclock\2
const rtemp=3 'code to start temperature conversion
const rhumidity=5 'code to start humidity conversion
'SHT11 magic numbers
const d1=-39.66
const d2=0.01
const c1=-2.0468
const c2=0.0367
const c3=-1.5955e-6
const t1=0.01
const t2=0.00008
'
DIM float temperature
pin(SCK_PIN)=0
setpin SCK_PIN,dout
setpin DAT_OUT_PIN,din
connectionreset
do
temperature=read_temp()
print temperature,read_humidity(temperature)
pause 2000
loop
end

sub connectionreset ' routine to reset the chip
local integer i
for i = 1 to 10
pin(SCK_PIN)=1
pause fullclock
pin(SCK_PIN)=0
pause fullclock
next i
return

sub transstart() ' routine to start a transaction
setpin DAT_OUT_PIN,dout' set the clock and data as outputs
pin(SCK_PIN)=0: pin(DAT_OUT_PIN)=1 ' clock low and data high
pause halfclock
pin(SCK_PIN)=1 ' clock high and data high
pause halfclock
pin(DAT_OUT_PIN)=0 ' clock stays high, data low
pause halfclock
pin(SCK_PIN)=0' clock goes low, data stays low
pause fullclock
pin(SCK_PIN)=1 ' clock high, data stays low
pause halfclock
pin(DAT_OUT_PIN)=1 ' clock stays high data high
pause halfclock
return

function read_temp() as float 'routine to return current tem,perature
local integer i,j
transstart()
writebyte(rtemp)
waitcomplete()
i=readbyteack()
j=readbyte()
read_temp=d1+d2*(j+(i<<8))
end function

function read_humidity(temp as float) as float ' routine to return current humidity
local integer i,j
local float SO,RHlin
transstart()
writebyte(rhumidity)
waitcomplete()
i=readbyteack()
j=readbyte()
SO=j+(i<<8)
RHlin=c1+c2*SO+c3*(SO^2) '
read_humidity=(temp-25)*(t1+t2*SO) * RHlin
end function

sub writebyte(b0 as integer) 'outputs the byte in b0 to the SHT11
local integer i
setpin DAT_OUT_PIN,dout' set the clock and data as outputs
pin(SCK_PIN)=0: pin(DAT_OUT_PIN)=1 ' data high and clock low
if (b0 and 128)=0 then: clocklow() :else :clockhigh(): endif
if (b0 and 64)=0 then: clocklow() :else :clockhigh(): endif
if (b0 and 32)=0 then: clocklow() :else :clockhigh(): endif
if (b0 and 16)=0 then: clocklow() :else :clockhigh(): endif
if (b0 and 8)=0 then: clocklow() :else :clockhigh(): endif
if (b0 and 4)=0 then: clocklow() :else :clockhigh(): endif
if (b0 and 2)=0 then: clocklow() :else :clockhigh(): endif
if (b0 and 1)=0 then: clocklow() :else :clockhigh(): endif
setpin DAT_OUT_PIN,DIN,pullup
i=readbit()
return

sub clocklow()
pin(SCK_PIN)=0: pin(DAT_OUT_PIN)=0 ' set data low
pause halfclock
pin(SCK_PIN)=1 ' set clock high, data stays low
pause fullclock
pin(SCK_PIN)=0 ' set clock low, data stays low
pause halfclock
end sub

sub clockhigh()
pin(SCK_PIN)=0: pin(DAT_OUT_PIN)=1 ' set data high
pause halfclock
pin(SCK_PIN)=1 ' set clock high, data stays high
pause fullclock
pin(SCK_PIN)=0 ' set clock low, data stays high
pause halfclock
end sub

function readbit() as integer' generate a clock pulse to read a bit into b1
pause halfclock
pin(SCK_PIN)=1 ' set clock high
pause halfclock
readbit=pin(DAT_OUT_PIN) 'read data
pause halfclock
pin(SCK_PIN)=0' set clock low
pause halfclock
END FUNCTION

function readbyteack() as integer 'inputs a byte , send ack
setpin DAT_OUT_PIN,DIN,pullup
if readbit() then: readbyteack=128: else :readbyteack=0: endif
if readbit() then: readbyteack=readbyteack OR 64 : endif
if readbit() then: readbyteack=readbyteack OR 32 : endif
if readbit() then: readbyteack=readbyteack OR 16 : endif
if readbit() then: readbyteack=readbyteack OR 8 : endif
if readbit() then: readbyteack=readbyteack OR 4 : endif
if readbit() then: readbyteack=readbyteack OR 2 : endif
if readbit() then: readbyteack=readbyteack OR 1 : endif
pause halfclock
setpin DAT_OUT_PIN,DOUT
clocklow
setpin DAT_OUT_PIN,DIN,pullup
END FUNCTION

function readbyte() as integer 'inputs a byte , send ack
setpin DAT_OUT_PIN,DIN,pullup
if readbit() then: readbyte=128: else: readbyte=0: endif
if readbit() then: readbyte=readbyte OR 64 : endif
if readbit() then: readbyte=readbyte OR 32 : endif
if readbit() then: readbyte=readbyte OR 16 : endif
if readbit() then: readbyte=readbyte OR 8 : endif
if readbit() then: readbyte=readbyte OR 4 : endif
if readbit() then: readbyte=readbyte OR 2 : endif
if readbit() then: readbyte=readbyte OR 1 : endif
pause halfclock
pin(SCK_PIN)=0' set data goes high with pullup
pause halfclock
pin(SCK_PIN)=1 ' set clock high, data stays high
pause fullclock
pin(SCK_PIN)=0 ' set clock low, data stays high
pause halfclock
END FUNCTION

sub waitcomplete() 'put timeout code in here if required
l: pause 20
if pin(DAT_OUT_PIN)<>0 goto l
end sub

 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024