DHT11 Humidity & temperature sensor with SLEEP for 16F690


Closed Thread
Results 1 to 2 of 2

Hybrid View

  1. #1
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    891

    Default DHT11 Humidity & temperature sensor with SLEEP for 16F690

    Hello,

    Here is my code to read temperature and humidity from a DHT11 sensor. It has also the SLEEP functionality.

    I tried to make it as simple and clear as possible

    Code:
    ' ====== PIC 16F690 FUSES ==========================================================================
    ' External oscillator 8MHz
    @ __config _FCMEN_OFF &_IESO_OFF &_CPD_OFF &_WDT_OFF &_HS_OSC &_BOR_OFF &_CP_OFF &_PWRTE_OFF &_MCLRE_OFF
      
    ' ====== REGISTERS =================================================================================
    ' Registers   76543210
    OPTION_REG = %10000000 ' PORT A&B Pull-Ups and Prescaler        (def.: %11111111)
    OSCCON     = %00000000 ' External                               (def.: %01101000)
    WDTCON     = %00001001 ' Enable WatchDog (the REGISTER, not the fuse) for SLEEP
    
    ' ====== DEFINES ===================================================================================
    DEFINE OSC 8 ' don't forget the external oscillator
    
    ' ====== VARIABLES =================================================================================
    DHT_Data    VAR BYTE [32] ' hold bitwise humidity and temperature data
    HUMIDITY    VAR BYTE      ' holds humidity value
    TEMPERATURE VAR BYTE      ' holds temperature value
    x           VAR BYTE      ' a counter
    SerialData  VAR PORTA.0   ' To display data through serial comm
    DHT_Port    VAR PORTB.7   ' PIC's port to DHT11's "OUT" pin
    DHT_PWR     VAR PORTC.7   ' power supply for DHT11's module - "+" pin (2mA)
    
    ' ====== INTIALIZE =================================================================================                                                                                                    
    HIGH SerialData           'set port High to avoid sending serial "garbage"
    
    ' ====== PROGRAM START =============================================================================
    MAIN:
    
        ' Power-up DHT11
        HIGH DHT_PWR          ' switch ON DHT11 sensor
        HIGH DHT_Port         ' make the PIC's port an Output
        PAUSE 1000            ' let DHT11 settle
        
        ' Send Start Signal to DHT11
        DHT_Port = 0 : PAUSE 20   ' set LOW at least 18ms to let DHT11 detect the signal
        DHT_Port = 1 : PAUSEUS 30 ' set 20-40µs HIGH and wait for DHT11's response
    
        ' DHT11 responds to MCU now in going 80µs LOW and 80µs HIGH before sending data
        WHILE DHT_Port = 0 : WEND  ' DHT11 goes LOW - just wait until (~80µs) it goes HIGH
        ' Once gone HIGH, DHT11 waits 80µs and then starts to send data
    
        ' Receive 32 data bits (out of 40 - last 8bits are checksum - not used)
        FOR x = 31 TO 0 STEP -1
            PULSIN DHT_Port, 1, DHT_Data[x]
        NEXT x
        
        ' Switch OFF DHT11 sensor (not mandatory but gives better RH results)
        LOW DHT_PWR
        
        ' Convert pulses length into binary values (26-28µs = "0", 70µs = "1")
        FOR x = 31 TO 0 STEP -1
            IF DHT_Data[x] > 10 THEN  ' 50µs (somewhere between 28 and 70µs)
                DHT_Data[x] = 1       ' is it greater than 50µs so it's a "1"
            ELSE
                DHT_Data[x] = 0       ' is it smaller than 50µs so it's a "0"
            ENDIF
        NEXT x
        
        ' Convert bits to variables
        HUMIDITY.7 = DHT_Data[31]
        HUMIDITY.6 = DHT_Data[30]
        HUMIDITY.5 = DHT_Data[29]
        HUMIDITY.4 = DHT_Data[28]
        HUMIDITY.3 = DHT_Data[27]
        HUMIDITY.2 = DHT_Data[26]
        HUMIDITY.1 = DHT_Data[25]
        HUMIDITY.0 = DHT_Data[24]
        
        TEMPERATURE.7 = DHT_Data[15]
        TEMPERATURE.6 = DHT_Data[14]
        TEMPERATURE.5 = DHT_Data[13]
        TEMPERATURE.4 = DHT_Data[12]
        TEMPERATURE.3 = DHT_Data[11]
        TEMPERATURE.2 = DHT_Data[10]
        TEMPERATURE.1 = DHT_Data[9]
        TEMPERATURE.0 = DHT_Data[8]    
        
        ' Display temperature and humidity
        SEROUT2 SerialData,84,["T: ", DEC TEMPERATURE, "C   RH: ", DEC HUMIDITY, "%", 13,10] '9600,8,N,1
    
        ' Now, goto low power for 1 minute    
        SLEEP 60 ' in seconds - wake-up every minute
    
        GOTO MAIN
    
    '======= CIRCUITRY =================================================================================
    '       [PIC]
    'PORTA.0[19] ICSPDAT / Serial data out (PICkit2's UART tool RX pin)
    'PORTA.1[18] ICSPCLK
    'PORTA.2[17]
    'PORTA.3[04] MCLR
    'PORTA.4[03] OSC2 - 8Mhz Xtal
    'PORTA.5[02] OSC1 - 8Mhz Xtal
    '
    'PORTB.4[13]
    'PORTB.5[12]
    'PORTB.6[11]
    'PORTB.7[10] DHT11's "OUT" pin with 10k pull-up resistor
    '
    'PORTC.0[16]
    'PORTC.1[15]
    'PORTC.2[14]
    'PORTC.3[07]
    'PORTC.4[06]
    'PORTC.5[05]
    'PORTC.6[08]
    'PORTC.7[09] DHT11's module power supply pin "+"
    Roger

  2. #2
    Join Date
    Nov 2007
    Location
    West Covina, CA
    Posts
    219


    Did you find this post helpful? Yes | No

    Default Re: DHT11 Humidity & temperature sensor with SLEEP for 16F690

    Thanks for posting your work Roger.

    I've been wanting to experiment with a temp/RH device lately and this is enough to make it happen.
    Copied to a file before it gets lost.
    Louie

Similar Threads

  1. DHT11 humidity sensor
    By tbarania in forum Code Examples
    Replies: 0
    Last Post: - 28th March 2015, 21:29
  2. Replies: 6
    Last Post: - 14th October 2013, 13:27
  3. DHT 11 Humidity & Temperature Sensor
    By jefrem in forum mel PIC BASIC
    Replies: 2
    Last Post: - 30th July 2013, 20:15
  4. DHT11 Temperature/Humidity sensor
    By mircogomiero in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 26th January 2013, 13:37
  5. A Temperature & Humidity Data Recorder Project
    By Oldspring in forum Off Topic
    Replies: 0
    Last Post: - 9th July 2008, 19:47

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts