News:

;) This forum is the property of Proton software developers

Main Menu

18F46K22 and DHT11 Sensor

Started by Jamie, Apr 07, 2023, 02:59 AM

Previous topic - Next topic

Jamie

Has anyone had any success with the DHT11 humidity and temp sensor?

I found the following code on the forum and gave it a try and it just keeps saying Sensor not Ready.
I do have the pullup resistor on the data line.

In the original code it used pulsin but compiled ok then i changed it to pulsein as the manual shows. Also tried adjusting the timing with no luck.




Device 18F46K22                 ' 32k but 16k code space with 16bit words
Config_Start
  FOSC = HSMP       ;HS oscillator (medium power 4-16 MHz)
  PLLCFG = OFF      ;Oscillator used directly
  PRICLKEN = On     ;Primary clock is always enabled
  FCMEN = OFF       ;Fail-Safe Clock Monitor disabled
  IESO = OFF        ;Oscillator Switchover mode disabled
  PWRTEN = OFF      ;Power up timer disabled
  BOREN = SBORDIS   ;Brown-out Reset enabled in hardware only (SBOREN is disabled)
  BORV = 190        ;VBOR set to 1.90 V nominal
  WDTEN = OFF       ;Watch dog timer is always disabled. SWDTEN has no effect.
  WDTPS = 32768     ;1:32768
  CCP2MX = PORTC1   ;CCP2 input/output is multiplexed with RC1
  PBADEN = OFF      ;PORTB<5:0> pins are configured as digital I/O on Reset
  CCP3MX = PORTB5   ;P3A/CCP3 input/output is multiplexed with RB5
  HFOFST = On       ;HFINTOSC output and ready status are not delayed by the oscillator stable status
  T3CMX = PORTC0   ;T3CKI is on RC0
  P2BMX = PORTD2   ;P2B is on RD2
  MCLRE = EXTMCLR   ;MCLR pin enabled, RE3 input pin disabled
  STVREN = OFF      ;Stack full/underflow will cause Reset
  LVP = OFF         ;Single-Supply ICSP disabled
  XINST = OFF       ;Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
  Debug = OFF       ;Disabled
  Cp0 = OFF           ;Block 0 (000800-003FFFh) not code-protected
  CP1 = OFF           ;Block 1 (004000-007FFFh) not code-protected
  CP2 = OFF           ;Block 2 (008000-00BFFFh) not code-protected
  CP3 = OFF           ;Block 3 (00C000-00FFFFh) not code-protected
  CPB = OFF           ;Boot block (000000-0007FFh) not code-protected
  CPD = OFF           ;Data EEPROM not code-protected
  WRT0 = OFF       ;Block 0 (000800-003FFFh) not write-protected
  WRT1 = OFF       ;Block 1 (004000-007FFFh) not write-protected
  WRT2 = OFF       ;Block 2 (008000-00BFFFh) not write-protected
  WRT3 = OFF       ;Block 3 (00C000-00FFFFh) not write-protected
  WRTC = OFF       ;Configuration registers (300000-3000FFh) not write-protected
  WRTB = OFF       ;Boot Block (000000-0007FFh) not write-protected
  WRTD = OFF       ;Data EEPROM not write-protected
  EBTR0 = OFF       ;Block 0 (000800-003FFFh) not protected from table reads executed in other blocks
  EBTR1 = OFF       ;Block 1 (004000-007FFFh) not protected from table reads executed in other blocks
  EBTR2 = OFF       ;Block 2 (008000-00BFFFh) not protected from table reads executed in other blocks
  EBTR3 = OFF       ;Block 3 (00C000-00FFFFh) not protected from table reads executed in other blocks
  EBTRB = OFF       ;Boot Block (000000-0007FFh) not protected from table reads executed in other blocks
Config_End


Declare Xtal = 10
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' 4x20 LCD Interface setup

Declare LCD_Type 0                      ' This is an alphanumeric LCD Display
Declare LCD_Interface 4                 ' USE a 4 bit interface
Declare LCD_Lines 2                     ' This is a 4 line LCD Display
Declare LCD_DTPin = PORTB.4             ' use upper 4 bits of portB for the data lines
Declare LCD_ENPin = PORTB.2             ' LCD Enable pin
Declare LCD_RSPin = PORTB.3             ' LCD Reset pin

All_Digital = True

Symbol HumSenLn = PORTB.0

 Dim DataIn As Byte
 Dim ChkSum As Byte
 Dim Index As Byte
 Dim SenData As Dword
 Dim Loop1 As Byte

 GoTo Main
 '******************************************************************************
 
 Get_Hum:
   
    SenData=0:ChkSum=0
 
    High HumSenLn
    DelayMS 50
    Low HumSenLn:DelayMS 18
    High HumSenLn:DelayUS 30
    DataIn = PulseIn HumSenLn,1

    If DataIn < 33 Then
        Print At 1,1, "Sensor not ready"
        DelayMS 2000
        Cls
    Else
        For Index = 31 To 0 Step -1
            DataIn = PulseIn HumSenLn,High 'recieve pulses from sensor
            If DataIn > 18 Then  LoadBit SenData,Index,2
        Next Index
       
        For Index = 7 To 0 Step -1
            DataIn = PulseIn HumSenLn,High
            If DataIn > 18 Then LoadBit ChkSum,Index,2
        Next Index
       
        Print At 1,1,"H:",Dec SenData.Byte3,"%"
        Print At 1,13,"    "
        Print At 1,11,"T:",Dec SenData.Byte1,0,"C"
       
        If ChkSum <> (SenData.Byte1 + SenData.Byte3) Then
            Cls
            Print At 2,1,"Check Sum Error!"
            DelayMS 2000
        EndIf
   EndIf
Return
'******************************************************************************

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

 While
    GoSub Get_Hum
    DelayMS 5000
 Wend


wessie

Hi - I think there are code on this forum for the DHT22, if that would help. Perhaps try run your clock at 16mhz. Below code that works for me with the DHT22 using a 16F1827.

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

Device = 16F1827

Config1 FOSC_INTOSC, WDTE_OFF, PWRTE_OFF, MCLRE_ON, CP_OFF, CPD_OFF, BOREN_OFF, CLKOUTEN_OFF, IESO_OFF, FCMEN_OFF
Config2 WRT_OFF, PLLEN_OFF, STVREN_OFF,LVP_ON

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

     Declare Xtal = 16
'     
    Declare LCD_Type 0
    Declare LCD_DTPort PORTB
    Declare LCD_Interface 4
    Declare LCD_Lines 2
    Declare LCD_RSPin PORTA.7
    Declare LCD_ENPin PORTA.6
    Declare LCD_DTPin PORTB.4        
    Declare LCD_CommandUs 2000    ' Command Delay (uS)
    Declare LCD_DataUs 50         ' Data Delay (uS)
 
    Dim HumData As Word
    Dim HumFract As Word
    Dim TempData As Word
    Dim TempFract As Word
    Dim Hold As Word
'   
    Dim HDhld As Byte       'Humidity hold
    Dim TDhld As Byte       'Temprature hold
    Dim LHhld As Byte       'Humidity hold Low value
    Dim LThld As Byte       'Temprature hold Low value
    Dim Dhld As Byte        'Temp / hum diff before triger   
    Dim DataIn As Byte
    Dim ChkSum As Byte
    Dim Index As Byte
    Dim MinusFlag As Byte
    Dim CalcChkSum As Byte
    Dim Lop As Byte
    Dim RelF As Byte    'relay flag - no sensor detected
    Dim BLT As Byte     'back light timer
    Dim BLD As Byte     'backlite trigger
    Dim CC As Byte      'hearbeat symbol
'
    Symbol HumSenLn = PORTB.3 
    Symbol Unit = 18        'Logic 1 value.
    Symbol But3 = PORTA.0   'button3 - enter   
    Symbol But1 = PORTA.1   'button1
    Symbol But2 = PORTA.2   'button2
    Symbol BL = PORTA.4     'back light
    Symbol RelT = PORTB.1   'Temprature Relay
    Symbol RelH = PORTB.0   'Humidity Relay
   
'
    All_Digital = True
    TRISA=%00000111
    TRISB=%00001000
'
'    FVRCON = %11000001       'ref=1.024v
'    FVRCON = %00000000       'ref=5v
   
    OSCCON = %01111000         '16Mhz
    OSCTUNE = %00000000        'tune osc
'     
 '***** Special LCD Character Definition - oC. *****
 'Use LCD Chr Gen to create and store in location 0

'Print $FE,$40,$1C,$14,$1C,$06,$09,$08,$09,$06   'deg C 0
Print $FE,$40,$18,$18,$06,$09,$08,$09,$06,$00
Print $FE,$48,$1C,$08,$08,$08,$06,$05,$06,$05   '1 TA
Print $FE,$50,$1C,$14,$14,$1C,$07,$05,$05,$05   '2 TO
                                                '3 TN
'Print $FE,$58,$00,$0A,$15,$11,$0A,$04,$00,$00   '3 Heart
Print $FE,$78,$08,$14,$1C,$14,$07,$02,$02,$02   '7 RT
Print $FE,$60,$00,$1F,$11,$11,$11,$11,$1F,$00   '4 []
Print $FE,$68,$00,$00,$0E,$0A,$0A,$0E,$00,$00   '5
Print $FE,$70,$00,$00,$00,$04,$04,$00,$00,$00   '6





 '
 GoTo Main
 
 '*****************************************************************************
  GetHum:
    High HumSenLn
    DelayMS 300
 
    HumData=0:TempData=0:ChkSum=0:MinusFlag = 0
    Low HumSenLn:DelayMS 18
    High HumSenLn:DelayUS 30
    DataIn = PulsIn HumSenLn,High
   
    If DataIn < 20 Then      '80uS start pulse from DHT22
        Print At 2,1, "Sensor not ready"
        RelF = 1 : Low RelH : Low RelT
        DelayMS 2000
        Cls
        GoTo GetHum
    EndIf       
'        Print At 2,6, "DHT22 Test"       
        RelF = 0 
        For Index = 15 To 0 Step -1
            DataIn = PulsIn HumSenLn,High
            If DataIn > Unit Then  LoadBit HumData,Index,2   '70uS = Logic 1
        Next Index
       
        For Index = 15 To 0 Step -1
            DataIn = PulsIn HumSenLn,High                                                                             
            If DataIn > Unit Then  LoadBit TempData,Index,2
        Next Index
       
        For Index = 7 To 0 Step -1
            DataIn = PulsIn HumSenLn,High
            If DataIn > Unit Then LoadBit ChkSum,Index,2
        Next Index
       
        CalcChkSum = HumData.HighByte + HumData.LowByte + TempData.HighByte + TempData.LowByte
       
        If ChkSum <> CalcChkSum Then   
            Print At 2,1,"Check Sum Error!"
            RelF = 1 : Low RelH : Low RelT           
            DelayMS 2000
            Cls
'            Print At 2,6, "DHT22 Test"
        Else     
            RelF = 0
            Hold = HumData
            HumData = HumData/10
            HumFract = Hold - (HumData *10)
 
            If  TempData.15 = 1 Then MinusFlag = 1
            TempData = TempData & $7FFF   'Strip off bit 15 incase it is 1
           
            Hold = TempData
            TempData = TempData/10
            TempFract = Hold - (TempData*10)
             
'
            Print At 1,1,"T:",Dec TempData,".",Dec TempFract,0
            Print At 2,1,"H:",Dec HumData,".",Dec HumFract,"%"
            Print At 1,9,1,7,Dec TDhld,0
            Print At 2,9,1,7,Dec HDhld,"%"
            Print At 1,15,"d",Dec Dhld

'           
'            Print At 4,15,"    "
'            If MinusFlag = 1 Then
'                Print At 3,12,"T:-"
'            Else
'                Print At 4,12, " T:"
'            EndIf       
'            Print Dec TempData,".",Dec TempFract,0
        EndIf
    Return

Jamie

Thank you Wessie

I'll order a DHT22.
the DHT11 just doesn't want to work for me.

I've tried changing the clock to 16mhz, didn't work
tried changing the timings also no luck.
If I get the DHT11 going I'll post the code.

thanks again for the help.

Jamie