Friday, 21 February 2020
Main Menu
Home Home
Shop Shop
News News
Products Products
Application Notes Application Notes
Publications Publications
Links Links
Support Center Support Center
Downloads Downloads
Forum Forum
Resellers Resellers
Contact Us Contact Us
Updates Updates
MCS Wiki MCS Wiki
Online Help
BASCOM-8051 Help BASCOM-8051 Help
Contents in Cart
Show Cart
Your Cart is currently empty.
Search the Shop

Products Search

User Login


If you have problem after log in with disappeared login data, please press F5 in your browser

RSS News

AN #116 - Reading the SHT11 Humidity sensor Print
Reading the SHT11 Humidity sensor

This application note was submitted by Stuart Leslie. I am very pleased with it since I tried to get the SHT11 working too but failed. After a few days I gave up on it and was ready to test it with a 8051 chip. The SHT11 does not follow the I2C specifications. Good for us that Stuart was so clever to use the SHIFT routines.

The SHT11 is a very good dual sensor, it is a temperature and humidity sensor. It is cheap when you consider the performance and there are free samples available from Sensirion.


You can download the sample BASCOM-AVR file.
It is also shown below.

'Routine to read the SHT11 Humidity sensor chip
'By Stuart Leslie
'Contact with any questions
'Uses BascomAVR
'a .01 uf capacitor across VCC and Ground on the SHT11 really cleans up the data
'a pullup is required on "data" pin as shown in the data sheet

Dim Ctr As Byte
Dim Dataword As Word
Dim Command As Byte
Dim Dis As String * 20

Dim Calc As Single
Dim Calc2 As Single
Dim Rhlinear As Single
Dim Rhlintemp As Single
Dim Tempc As Single
Dim Tempf As Single

Const C1 = -4
Const C2 = 0.0405
Const C3 = -0.0000028
Const T1c = .01
Const T2 = .00008
Const T1f = .018

Alias Portb.0
Alias Portb.1
Alias Pinb.1
Alias Portb.2

Declare Sub Getit()

Ddrb = &B11111111  'all port b are output
Config Pinb.0 = Output 'sck
Config Pinb.1 = Output 'datain

'reset the serial communications first, it is easily confused!
Set Dataout
For Ctr = 1 To 12
Set Sck
Waitus 2
Reset Sck
Waitus 2
Next Ctr

Do 'continually read the tempfature and humidity

= &B00000011
Call Getit 'Get the temperature, puts result in "dataword" for us
= T1f * Dataword
= Tempf - 40

= T1c * Dataword 'get celcius for later calculations and for "the rest of the world"
= Tempc - 40

= Fusing(tempf , "###.##")
Print "Temperature = " ; Dis ; " (F)"

= &B00000101
Call Getit 'get the humidity
= C2 * Dataword
= Dataword * Dataword 'that "2" in the datasheet sure looked like a footnote for a couple days, nope it means "squared"!
= C3 * Calc2
= Calc + C1
= Calc + Calc2

'Dis = Fusing(rhlinear , "##.##")
'Print "Humidity adjusted for linear = " ; Dis

= T2 * Dataword
= Calc + T1c
= Tempc - 25
= Calc2 * Calc
= Calc + Rhlinear

= Fusing(rhlintemp , "##.##")
Print "Humidity adjusted for temperature = " ; Dis

Wait 1

Sub Getit()

Local Datavalue As Word
Local Databyte As Byte

'start with "transmission start"
Set Sck
Reset Dataout
Reset Sck
Set Sck
Set Dataout
Reset Sck

'now send the command
Shiftout Dataout , Sck , Command , 1

Ddrb = &B11111101 'datain is now input
Config Pinb.1 = Input 'datain
Set Sck 'click one more off
Reset Sck
Waitus 10 'no idea why, but it doesn't work without it!
Bitwait Pinb.1 , Reset 'wait for the chip to have data ready

Shiftin Datain , Sck , Databyte , 1 'get the MSB
= Databyte

Ddrb = &B11111111
Config Pinb.1 = Output

Reset Dataout 'this is the tricky part- Lot's of hair pulling- have to tick the ack!
Set Sck
Reset Sck

Ddrb = &B11111101 'datain is now input
Config Pinb.1 = Input

Shiftin Datain , Sck , Databyte , 1 'get the LSB
Shift Datavalue , Left , 8
= Datavalue Or Databyte
'don't tick the clock or ack since we don't need the CRC value, leave it hanging!
= Datavalue

Ddrb = &B11111111
Config Pinb.1 = Output

Reset Dataout
Set Sck
Reset Sck

Ddrb = &B11111101 'datain is now input
Config Pinb.1 = Input

Shiftin Datain , Sck , Databyte , 1 'not using the CRC value for now- can't figure it out! Anybody know how to impliment?
'Print "CRC value was - " ; Databyte

Ddrb = &B11111111
Config Pinb.1 = Output

Set Dataout
Set Sck
Reset Sck
End Sub