Read from DHT22 temperature and hum. sensor

2012-12-28
2013-05-30
  • DHT22(RHT03) as DHT11 is a digital  temperature  and relative humidity sensor based in 1-wire protocol of  MaxDetect

    Technology Co Ltd. (it's different from Maxim/Dallas 1-wire bus, so it's incompatible with Dallas 1-wire bus).
    Illustration of MaxDetect 1-wire bus:
    Data is comprised of integral and decimal part, the following is the formula for data.
    DATA=8 bit integral RH data+8 bit decimal RH data+8 bit integral T data+8 bit decimal T data+8 bit check-sum

    The all procedure in steps:
    1) Step 1: MCU send out start signal to RHT03 and RHT03 send response signal to MCU
    Data-bus's free status is high voltage level. When communication between MCU and RHT03 begins, MCU
    will pull low data-bus and this process must beyond at least 1~10ms to ensure RHT03 could detect MCU's signal,
    then MCU will pulls up and wait 20-40us for RHT03's response.
    When RHT03 detect the start signal, RHT03 will pull low the bus 80us as response signal, then RHT03 pulls
    up 80us for preparation to send data.
    2). Step 2: RHT03 send data to MCU
    When RHT03 is sending data to MCU, every bit's transmission begin with low-voltage-level that last 50us, the
    following high-voltage-level signal's length decide the bit is "1" or "0".
    For "1" after 50us low-voltage-level follows 70us high-voltage-level.
    For "0" after 50us low-voltage-level follows 26-28us high-voltage-level.

    When highest bit of temperature is 1, it means the temperature is below 0 degree Celsius.

    The following piece of code works very well for me in the 16F877 with 20MHz crystal.

    Sub ReadDHT22 (temp, hum)
    hum=0
    temp=0
    xm=0
    Dir DHT22 Out
    Set DHT22 off
    wait 1 ms
    Set DHT22 on
    wait 40 us
    Dir DHT22 In
    Wait until DHT22=off
    Wait while DHT22=off
    Wait until DHT22=on
    Wait while DHT22=on
    for i=1 to 40
    xm=0
    Wait until DHT22=off
    Wait while DHT22=off
    Do while DHT22 = on
    wait 5 us
    xm=xm+1
    loop
    if xm>=7 then
    SData(i)=1
    else
    SData(i)=0
    end if
    next
    xm=1
    for i=16 to 1 step -1
    hum=hum+SData(i)*xm
    temp=temp+SData(16+i)*xm
    xm=xm*2
    next
    hum=hum/10
    temp=temp/10
    Dir DHT22 Out
    End Sub

    The above code does not check negative temperature's and checksum
    a more complete code could be:

    Sub ReadDHT22 (temp, hum, tempsign, succesread)
    hum=0
    temp=0
    tempsign=0
    succesread=0
    timedelay=0
    ;Start MCU transmission
    Dir DHT22 Out
    Set DHT22 off
    wait 1 ms
    Set DHT22 on
    wait 40 us
    Dir DHT22 In
            ; Wait for sensor response
    Wait until DHT22=off
    Wait while DHT22=off
    Wait until DHT22=on
    Wait while DHT22=on
    ; Read 40 bits of data
    for bitread=1 to 40
    timedelay=0
    Wait until DHT22=off
    Wait while DHT22=off
    Do while DHT22 = on
    wait 5 us
    timedelay=timedelay+1
    loop
    if timedelay>=7 then
    SData(bitread)=1 ; SData is 40 bytes array
    else
    SData(bitread)=0
    end if
    next
    poweroftwo=1
    for bitread=8 to 1 step -1
    humint=humint+SData(bitread)*poweroftwo
    humdec=humdec+SData(8+bitread)*poweroftwo
    tempint=tempint+SData(16+bitread)*poweroftwo
    tempdec=tempdec+SData(24+bitread)*poweroftwo
    sum=sum+SData(32+bitread)*poweroftwo
    poweroftwo=poweroftwo*2
    next
    if sum=humint+humdec+tempint+tempdec then
    succesread=1  ; The read is successful
    hum=(humint*255+humdec)/10
    if SData(17)=1 then
    tempint=tempint-127
    tempsign=1   ;Means negative temperature
    end if
    temp=(tempint*255+tempdec)/10
    else
    succesread=0 ; The read is not successful
    end if
    Dir DHT22 Out
    End Sub

    Sorry for my poor English i hope  the above topic seam helpful to you.

     
  • Thomas Henry
    Thomas Henry
    2013-02-22

    Hi Gang,

    For another take on this part, I just had an article published on using the DHT22 with Great Cow Basic in Nuts & Volts Magazine. Anyone can view a preview of the first couple of pages online; just click the mouse icon:

    http://www.nutsvolts.com/index.php?/magazine/article/march2013_Henry

    You can also download the complete Great Cow Basic source code there for free. If you're a subscriber, then you also get access to the entire article.

    In case you're interested, the last several issues of the magazine have run other articles of mine exploiting Great Cow Basic, all the way back to November. It's still my favorite language!

    Cheers,

    Thomas Henry

     
  • floriano
    floriano
    2013-03-13

    hello
    it's possible receive the .hex file for the project article march 2013?

    thank you

    Floriano