code for the temperature and humidity sensor SHT11

Started by Yves, May 17, 2025, 11:36 AM

Previous topic - Next topic

Yves

Is anyone have the code to share for the SHT11? Thank you in advance
Cheers,

Yves
Yves

Pepe

;-------------------------------------------------------------------------------
;**** Added by Fuse Configurator ****
; Use the Fuse Configurator plug-in to change these settings

Device = 16F876

Config FOSC_HS, WDTE_OFF, PWRTE_ON, CP_OFF, BOREN_OFF, LVP_OFF, CPD_OFF, WRT_ON, DEBUG_OFF

;**** End of Fuse Configurator Settings ****
;-------------------------------------------------------------------------------

Declare Xtal = 20     ' Tell the compiler what frequency the device will be operating at
Declare Bootloader off
Declare Watchdog off
Declare Dead_Code_Remove = 1        ' Remove dead code
Declare Reminders Off
Declare Hints Off
Declare Create_Coff On

' definición display
Declare LCD_DTPin = PORTB.0
Declare LCD_ENPin PORTB.5
Declare LCD_RSPin PORTB.6


Declare LCD_Interface = 4    ' 4-bit Interface
Declare LCD_Lines = 2     
Declare LCD_Type = 0
     
Dim Dato    As Word
Dim D0      As Dato.Byte0
Dim D1      As Dato.Byte1
           
Dim J       As Word
Dim i       As Byte
 
Dim control As Byte
Dim HUM     As Float
Dim TEMP    As Float
Dim HUMA    As Float
Dim TEMPA   As Float

Symbol DTA  PORTC.4
Symbol CLK  PORTC.3   


PORTC=0
PORTB=0
TRISC=0b00010000
TRISB=0
 
Print At 1,1,"TEMP = ",Dec1 0," C  "
Print At 2,1,"HUMEDAD = ",Dec1 0 ," %  "

Resetear()

Do
TEMP = TEMPERATURA()
DelayMS 1000
HUM = HUMEDAD()
DelayMS  1000 

If TEMP <> TEMPA Then
                      TEMPA = TEMP                         
                      Print At 1,1,"TEMP = ",Dec1 TEMP," C  "
EndIf
If HUM <> HUMA Then
                      HUMA = HUM
                      Print At 2,1,"HUMEDAD = ",Dec1 HUM ," %  "
EndIf                           
Loop

Proc HUMEDAD(),Float
If command(5) = 0 Then
                       HUM = 0.0367 * Dato - 0.0000015955 * Dato * Dato - 2.0468    '12 bit
                       Result = (TEMP - 25) * (0.01 + 0.00008 * Dato )+ HUM
                  Else
                       Result = 0     
EndIf
EndProc
           
Proc TEMPERATURA (),Float
If command(3) = 0 Then
                       Result = 0.01 * Dato - 40.1 ' 14 bit   5v = 40.1
                  Else
                       Result = 0
EndIf
EndProc           

Proc Resetear() 
Output DTA
Output CLK
High DTA
For i = 0 To 9
High CLK
DelayUS 10
Low CLK
Next i
start()
DelayMS 15
EndProc

Proc command(control As Byte),Bit
Dato = 0
start()

SHOut DTA,CLK,1,[control]

Input DTA

PulseOut CLK,2
Result = 1
For J = 0 To 20000
If DTA = 0 Then
                Result = 0
                Break
EndIf
DelayUS 100
Next J 
SHIn DTA,CLK,0,[D1]
Output DTA
Low DTA
PulseOut CLK,2
Input DTA
SHIn DTA,CLK,0,[D0]
PulseOut CLK,2
EndProc

Proc start()
Output DTA
Output CLK
High DTA
Low CLK
DelayUS 10
High CLK
Low DTA
DelayUS 10
Low CLK
DelayUS 10
High CLK
DelayUS 10
High DTA
Low CLK
EndProc



I attached a demonstration in Proteus but I don't know why it shows the approximate values

Yves

Thank you Pepe,
The module I have has 3 pin (KY-015) +5V, GND and S. your code shows:
Symbol DTA  PORTC.4
Symbol CLK  PORTC.3

How do I wire it?
I believe the KY-015 module is based on the SHT11?
I'm a bit confused.

Cheers
Yves 
Yves


Yves

In fact I'm using DHT11 on a module called KY-015 which is a single wire unit. That where i would like the driver code.
 
Yves

RGV250

If you look in the datasheet pages 4, 5 & 6 it shows the data. It is 40 bits, I think you take the data line low for a short time and then clock in the data but this is the tricky bit. Page 6 shows the 0 / 1 are not just high or low.

Bob

Yves

It looks like I also need a pullup resistor?
Yves

RGV250

QuoteIt looks like I also need a pullup resistor?

It might already be on the breakout board you have.

Bob

Yves

Thank you the only thing that was missing is the pullup resistor (5.6k). Now it is working fine.

Cheers

Yves
Yves

Pepe



;-------------------------------------------------------------------------------
;**** Added by Fuse Configurator ****
; Use the Fuse Configurator plug-in to change these settings

Device = 16F628A

Config FOSC_HS, WDTE_OFF, PWRTE_ON, MCLRE_ON, BOREN_OFF, LVP_OFF, CPD_OFF, CP_OFF

;**** End of Fuse Configurator Settings ****
;-------------------------------------------------------------------------------

Declare Xtal = 16
Declare Optimiser_Level = 3
Declare Bootloader off
Declare Watchdog off
Declare Float_Display_Type = Fast
Declare Dead_Code_Remove = 1        ' Remove dead code
Declare Reminders Off
Declare Hints Off
Declare auto_variable_bank_cross On
Declare Create_Coff On

Declare LCD_Interface = 4
Declare LCD_DTPin PORTB.4
Declare LCD_ENPin PORTB.3
Declare LCD_RSPin PORTB.0
Declare LCD_Lines = 2
Declare LCD_Type = 0

Dim DataIn As Byte
Dim ChkSum As Byte
Dim Index As Byte
Dim SenData As Dword
Dim Loop1 As Byte
Dim hum As Byte
Dim temp As Byte
Dim err As Byte
Symbol HumSenLn = PORTA.2

' Special LCD Character Definitions
Print $FE,$40,$07,$05,$05,$07,$00,$00,$00,$00 '0  - Degree

Cls
Print At 1,4, "DHT11 Test"
DelayMS 2000
Cls

Do
    err = Get_Hum()
   
    Select Case err
       Case 1
         Cls
         Print At 1,1, "Sensor not ready"
       Case 2
         Cls
         Print At 2,1,"Check Sum Error!"
       Case Else
         Print At 1,1,"H: ",Dec hum,"%  "
         Print At 1,10,"T: ",Dec temp,0,"C  "
         DelayMS 3000
    EndSelect
        DelayMS 2000
Loop

Proc start()
  Output HumSenLn              ' Configure connection pin as output
  Low HumSenLn                ' Connection pin output low
  DelayMS 20
  High HumSenLn                'Connection pin output high
  DelayUS 30
  Input HumSenLn     'Configure connection pin as input
EndProc

Proc Get_Hum(),Byte

    SenData = 0
    ChkSum = 0
    Result = 0
   
    start()

    DataIn = PulseIn HumSenLn,High
 

    If DataIn < 32 Then
                        Result = 1
                   Else
                        For Index = 31 To 0 Step -1
                         DataIn = PulseIn HumSenLn,High 'recieve pulses from sensor
                         If DataIn > 18 Then
                                             LoadBit SenData,Index,2
                         EndIf
                        Next Index

                        For Index = 7 To 0 Step -1
                         DataIn = PulseIn HumSenLn,High
                         If DataIn > 18 Then
                                             LoadBit ChkSum,Index,2
                         EndIf                   
                        Next Index

                        hum = SenData.Byte3
                        temp = SenData.Byte1

                        If ChkSum <> hum + temp Then
                                                     Result = 2
                        EndIf
    EndIf
EndProc




demo proteus

Pepe

Another example proteus