DHT22, AM2302 and pic18F2320


Closed Thread
Results 1 to 18 of 18

Hybrid View

  1. #1
    Join Date
    Jan 2004
    Location
    Thessaloniki , GREECE
    Posts
    61


    Did you find this post helpful? Yes | No

    Default Re: DHT22, AM2302 and pic18F2320

    Quote Originally Posted by Demon View Post
    P is defined as byte, but referred as bit?
    It should be byte as it stores the width of the pulse (in uS) PULSIN receives.
    Then it simply stores on itself the value of '1' or '0' according the width of this pulse.

    Edit: what if you display the data you receive, pause a few seconds, then continue. Confirm you're getting data as you expect it.
    Hmm! I will give it a try and let you knoiw ....Good thoughts my friend!

    Edit 2: Does your array start at 0 or 1? (Haven't coded in a while).
    I don't think is matter... Arrays start at 0.

    Only 17 entries for temp, missing #17.
    I just try to not capture the higher bit of temp (1 means temp is below 0 degrees Celsius) for the moment just testing....

    Robert Thank you very much for replying

    As I can see no example ANYWHERE in the net for DHT22 and picbasic , when completed it would be a good starting point for experiments with this low cost sensor!

  2. #2
    Join Date
    Jan 2004
    Location
    Thessaloniki , GREECE
    Posts
    61


    Did you find this post helpful? Yes | No

    Default Re: DHT22, AM2302 and pic18F2320

    In order to find out what pulsin really grabs I used serout data to debug...
    Here are the results so far -after reading the sensor 3 times.... :
    NOTE I changed the code a little bit so I can capture all data (included the start transmit 1bit before each 'bit')
    Code:
    * * * START * * *
    01=> 50uS  bit_val= 1
    02=> 20uS  bit_val= 0
    03=> 50uS  bit_val= 1
    04=> 30uS  bit_val= 1
    05=> 50uS  bit_val= 1
    06=> 70uS  bit_val= 1
    07=> 50uS  bit_val= 1
    08=> 70uS  bit_val= 1
    09=> 50uS  bit_val= 1
    10=> 20uS  bit_val= 0
    11=> 50uS  bit_val= 1
    12=> 30uS  bit_val= 1
    13=> 50uS  bit_val= 1
    14=> 20uS  bit_val= 0
    15=> 50uS  bit_val= 1
    16=> 30uS  bit_val= 1
    17=> 70uS  bit_val= 1
    18=> 20uS  bit_val= 0
    19=> 50uS  bit_val= 1
    20=> 60uS  bit_val= 0
    21=> 50uS  bit_val= 1
    22=> 70uS  bit_val= 1
    23=> 50uS  bit_val= 1
    24=> 30uS  bit_val= 1
    25=> 50uS  bit_val= 1
    26=> 20uS  bit_val= 0
    27=> 50uS  bit_val= 1
    28=> 0uS  bit_val= 0
    29=> 0uS  bit_val= 0
    30=> 0uS  bit_val= 0
    31=> 0uS  bit_val= 0
    32=> 0uS  bit_val= 0
    33=> 0uS  bit_val= 0
    34=> 0uS  bit_val= 0
    35=> 0uS  bit_val= 0
    36=> 0uS  bit_val= 0
    37=> 0uS  bit_val= 0
    38=> 0uS  bit_val= 0
    39=> 0uS  bit_val= 0
    40=> 0uS  bit_val= 0
    41=> 0uS  bit_val= 0
    42=> 0uS  bit_val= 0
    43=> 0uS  bit_val= 0
    44=> 0uS  bit_val= 0
    45=> 0uS  bit_val= 0
    46=> 0uS  bit_val= 0
    47=> 0uS  bit_val= 0
    48=> 0uS  bit_val= 0
    49=> 0uS  bit_val= 0
    50=> 0uS  bit_val= 0
    51=> 0uS  bit_val= 0
    52=> 0uS  bit_val= 0
    53=> 0uS  bit_val= 0
    54=> 0uS  bit_val= 0
    55=> 0uS  bit_val= 0
    56=> 0uS  bit_val= 0
    57=> 0uS  bit_val= 0
    58=> 0uS  bit_val= 0
    59=> 0uS  bit_val= 0
    60=> 0uS  bit_val= 0
    61=> 0uS  bit_val= 0
    62=> 0uS  bit_val= 0
    63=> 0uS  bit_val= 0
    64=> 0uS  bit_val= 0
    65=> 0uS  bit_val= 0
    66=> 0uS  bit_val= 0
    67=> 0uS  bit_val= 0
    68=> 0uS  bit_val= 0
    69=> 0uS  bit_val= 0
    70=> 0uS  bit_val= 0
    71=> 0uS  bit_val= 0
    72=> 0uS  bit_val= 0
    73=> 0uS  bit_val= 0
    74=> 0uS  bit_val= 0
    75=> 0uS  bit_val= 0
    76=> 0uS  bit_val= 0
    77=> 0uS  bit_val= 0
    78=> 0uS  bit_val= 0
    79=> 0uS  bit_val= 0
    80=> 0uS  bit_val= 0
    * * * END * * *
    
    * * * START * * *
    01=> 50uS  bit_val= 1
    02=> 20uS  bit_val= 0
    03=> 50uS  bit_val= 1
    04=> 30uS  bit_val= 1
    05=> 50uS  bit_val= 1
    06=> 70uS  bit_val= 1
    07=> 50uS  bit_val= 1
    08=> 70uS  bit_val= 1
    09=> 50uS  bit_val= 1
    10=> 20uS  bit_val= 0
    11=> 50uS  bit_val= 1
    12=> 30uS  bit_val= 1
    13=> 50uS  bit_val= 1
    14=> 20uS  bit_val= 0
    15=> 50uS  bit_val= 1
    16=> 20uS  bit_val= 0
    17=> 70uS  bit_val= 1
    18=> 20uS  bit_val= 0
    19=> 50uS  bit_val= 1
    20=> 70uS  bit_val= 1
    21=> 50uS  bit_val= 1
    22=> 60uS  bit_val= 0
    23=> 50uS  bit_val= 1
    24=> 30uS  bit_val= 1
    25=> 50uS  bit_val= 1
    26=> 20uS  bit_val= 0
    27=> 50uS  bit_val= 1
    28=> 0uS  bit_val= 0
    29=> 0uS  bit_val= 0
    30=> 0uS  bit_val= 0
    31=> 0uS  bit_val= 0
    32=> 0uS  bit_val= 0
    33=> 0uS  bit_val= 0
    34=> 0uS  bit_val= 0
    35=> 0uS  bit_val= 0
    36=> 0uS  bit_val= 0
    37=> 0uS  bit_val= 0
    38=> 0uS  bit_val= 0
    39=> 0uS  bit_val= 0
    40=> 0uS  bit_val= 0
    41=> 0uS  bit_val= 0
    42=> 0uS  bit_val= 0
    43=> 0uS  bit_val= 0
    44=> 0uS  bit_val= 0
    45=> 0uS  bit_val= 0
    46=> 0uS  bit_val= 0
    47=> 0uS  bit_val= 0
    48=> 0uS  bit_val= 0
    49=> 0uS  bit_val= 0
    50=> 0uS  bit_val= 0
    51=> 0uS  bit_val= 0
    52=> 0uS  bit_val= 0
    53=> 0uS  bit_val= 0
    54=> 0uS  bit_val= 0
    55=> 0uS  bit_val= 0
    56=> 0uS  bit_val= 0
    57=> 0uS  bit_val= 0
    58=> 0uS  bit_val= 0
    59=> 0uS  bit_val= 0
    60=> 0uS  bit_val= 0
    61=> 0uS  bit_val= 0
    62=> 0uS  bit_val= 0
    63=> 0uS  bit_val= 0
    64=> 0uS  bit_val= 0
    65=> 0uS  bit_val= 0
    66=> 0uS  bit_val= 0
    67=> 0uS  bit_val= 0
    68=> 0uS  bit_val= 0
    69=> 0uS  bit_val= 0
    70=> 0uS  bit_val= 0
    71=> 0uS  bit_val= 0
    72=> 0uS  bit_val= 0
    73=> 0uS  bit_val= 0
    74=> 0uS  bit_val= 0
    75=> 0uS  bit_val= 0
    76=> 0uS  bit_val= 0
    77=> 0uS  bit_val= 0
    78=> 0uS  bit_val= 0
    79=> 0uS  bit_val= 0
    80=> 0uS  bit_val= 0
    * * * END * * *
    ...remember that I use a 4Mhz crystal so the pulse width is returned in 10us increments.

    Acoording to the above dubugging infos we can see a 50uS pulse wich according to datasheet is the pulse before each "0" or "1" pulse!

    One weird thing is also tha after pulse number 27 I read no pulses!!
    Last edited by bitmaniac; - 4th April 2013 at 12:53.

  3. #3
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,172


    Did you find this post helpful? Yes | No

    Default Re: DHT22, AM2302 and pic18F2320

    Did you use 100nF capacitor as mentionned in DHT22 datasheet?

    Edit: there's a guy here that gets only 21 elements, sounds like a similar issue:
    http://www.picaxeforum.co.uk/showthr...22-Please-Help

    Edit 2: this is in C, but read the blog at top. Has several interesting comments.
    http://blog.ringerc.id.au/2012/01/us...ht-22.html?m=1

    Can you run faster than 4mhz? Can you use internal osc instead? 18F generally can go quite fast.

    Robert
    Last edited by Demon; - 4th April 2013 at 13:48.

  4. #4
    Join Date
    Jan 2004
    Location
    Thessaloniki , GREECE
    Posts
    61


    Did you find this post helpful? Yes | No

    Default Re: DHT22, AM2302 and pic18F2320

    Thanks for trying to help my problem out my friend!

    Yes, I tried with 100nf on power.

    I am searching over the net for a week now and came across 2 different datasheet of the same sensor...DHT22 and sites with code in C e.t.c I have read most of them and trying to use picbasic instead!

    I will examine carefully your links (VERY good infos!!!) and come back....
    Last edited by bitmaniac; - 4th April 2013 at 13:54.

  5. #5
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,172


    Did you find this post helpful? Yes | No

    Default Re: DHT22, AM2302 and pic18F2320

    Well this guy got it working in C after playing with delays so it's most likely a timing issue.

    18F2320 can run at 40MHz internally, I'd try that first. Others mention waiting at least 2 seconds between samples.

    http://forum.43oh.com/topic/2239-sol...or-on-energia/

    Robert

  6. #6
    Join Date
    Jan 2004
    Location
    Thessaloniki , GREECE
    Posts
    61


    Did you find this post helpful? Yes | No

    Thumbs up SOLVED! DHT22, AM2302 and pic18F2320

    SUCCESS!!!

    After trying to increase crystal to 25Mhz and also 40Mhz I had the same problem of grabing only the first 21 pulses.... (problems with PULSIN speed...hmm....)

    Finally the solution was just to use PULSIN 40 times! in my code and not use the FOR NEXT LOOP !

    So there was timming speed problems and had to improve my code for speed - a little...

    Now I am getting correct results

    Here is the final code (the only thing left is to check checksum for errors!) ENJOY.

    Code:
    '***************************************
    '*  Name    : 18f2320_DHT22_AM2302.BAS *
    '*  Author  : bitmaniac   Alexandros Z.*
    '*  Notice  : Copyright (c) 2013       *
    '*  Date    : 5/4/2013                 *
    '***************************************
    
    ;include "ALLDIGITAL.pbp"
    ;@ __CONFIG _CONFIG1H, _INTIO2_OSC_1H & _IESO_OFF_1H ' use internal osc
    @ __CONFIG _CONFIG1H, _HS_OSC_1H & _IESO_OFF_1H ' use internal osc
    @ __CONFIG _CONFIG2L, _PWRT_OFF_2L & _BOR_OFF_2L
    @ __CONFIG _CONFIG2H, _WDT_OFF_2H & _WDTPS_512_2H
    @ __CONFIG _CONFIG3H, _MCLRE_OFF_3H & _PBAD_DIG_3H ;& _CCP2MX_OFF_3H
    @ __CONFIG _CONFIG4L, _LVP_OFF_4L
    
    DEFINE  LCD_DREG        PORTC	' LCD Data Port
    DEFINE  LCD_DBIT        4		' Starting Data Bit
    DEFINE  LCD_RSREG       PORTC	' Register Select Port
    DEFINE  LCD_RSBIT       2		' Register Select Bit
    DEFINE  LCD_RWREG       PORTC   ' LCD read/write port 
    DEFINE  LCD_RWBIT       1       ' LCD read/write bit 
    DEFINE  LCD_EREG        PORTC	' Enable Port
    DEFINE  LCD_EBIT        0		' Enable Bit
    DEFINE  LCD_BITS		4		' Data Bus Size
    DEFINE  LCD_LINES		2		' Number of Lines on LCD
    DEFINE  LCD_COMMANDUS 2000
    DEFINE  LCD_DATAUS 50
    pause 300
    
    define osc 4
    
    ADCON0=0 
    ADCON1=00001111                  'All digital
    CMCON=7                          'Comparators OFF
    INTCON2.7=0                      'RBPU =0 , TURN ON PORTB PULL-UPS
    
    TRISA=%00000000
    TRISB=%00001111
    
    PORTA=0
    PORTB=0
    
    humread     var word             '16-bit var to store humidity
    tmpread     var word             '16-bit var to store temperature
    checksum    var BYTE             '8-bit var to store checksum
    x           var byte             'general var
    pulse       var byte [81]        'store pulse var
    p           var bit [81]         'store pulse var
    
    led         var LATA.0
    dht_port    var PORTB.4
    
    clear
    high dht_port
    high led
    
    ;---------- main program -------------
    high led
    LCDout $FE,$01,"DHT-22"
    LCDout $FE,$C0,"testing..."
    pause 2000
    
    start:
    ;---- check_DHT22 ----
    ;-== send StartSignal ==-
    TRISB.4 = 0                      'Data port is output
    high dht_port
    pauseus 10                       'send 10uS high pulse
    low dht_port                     
    pause 1                          'send 1mS high pulse
    high dht_port
    ;-== wait response from Sensor ==-
    TRISB.4 = 1                      'Data port is input
    while dht_port=1:wend            'wait for low from DHT
    while dht_port=0:wend            'wait for high FROM DHT
    high led
    ;-== Grab 40bits data from DHT22 ==-
    pulsin PORTB.4,1,pulse(1)        'Humidity HighBit data
    pulsin PORTB.4,1,pulse(2)
    pulsin PORTB.4,1,pulse(3)
    pulsin PORTB.4,1,pulse(4)
    pulsin DHT_port,1,pulse(5)
    pulsin DHT_port,1,pulse(6)
    pulsin DHT_port,1,pulse(7)
    pulsin DHT_port,1,pulse(8)
    pulsin DHT_port,1,pulse(9)
    pulsin DHT_port,1,pulse(10)
    pulsin DHT_port,1,pulse(11)
    pulsin DHT_port,1,pulse(12)
    pulsin DHT_port,1,pulse(13)
    pulsin DHT_port,1,pulse(14)
    pulsin DHT_port,1,pulse(15)
    pulsin DHT_port,1,pulse(16)      'Humidity LowBit data
    pulsin DHT_port,1,pulse(17)      'Temperature HighBit data (1 means below 0 degree!)
    pulsin DHT_port,1,pulse(18)
    pulsin DHT_port,1,pulse(19)
    pulsin DHT_port,1,pulse(20)
    pulsin DHT_port,1,pulse(21)
    pulsin DHT_port,1,pulse(22)
    pulsin DHT_port,1,pulse(23)
    pulsin DHT_port,1,pulse(24)
    pulsin DHT_port,1,pulse(25)
    pulsin DHT_port,1,pulse(26)
    pulsin DHT_port,1,pulse(27)
    pulsin DHT_port,1,pulse(28)
    pulsin DHT_port,1,pulse(29)
    pulsin DHT_port,1,pulse(30)
    pulsin DHT_port,1,pulse(31)
    pulsin DHT_port,1,pulse(32)      'Temperature LowBit data
    ;pulsin DHT_port,1,pulse(33)     'Checksum HighBit
    ;pulsin DHT_port,1,pulse(34)
    ;pulsin DHT_port,1,pulse(35)
    ;pulsin DHT_port,1,pulse(36)
    ;pulsin DHT_port,1,pulse(37)
    ;pulsin DHT_port,1,pulse(38)
    ;pulsin DHT_port,1,pulse(39)
    ;pulsin DHT_port,1,pulse(40)     'Checksum LowBit
    ;-== convert to '0' & '1' ==-
    for x=1 to 40
    if pulse(x)>=2 and pulse(x)<=4 then p(x)=0 'if pulsewidth between 20 and 40uS then read as '0' 
    if pulse(x)>=6 and pulse(x)<=8 then p(x)=1 'if pulsewidth between 60 and 80uS then read as '1'
    next x
    ;-== convert bin to dec ==-
    humread=32768*p(1)+16384*p(2)+8192*p(3)+4096*p(4)+2048*p(5)+1024*p(6)+512*p(7)+256*p(8)+128*p(9)+64*p(10)+32*p(11)+16*p(12)+8*p(13)+4*p(14)+2*p(15)+1*p(16)
    tmpread=16384*p(18)+8192*p(19)+4096*p(20)+2048*p(21)+1024*p(22)+512*p(23)+256*p(24)+128*p(25)+64*p(26)+32*p(27)+16*p(28)+8*p(29)+4*p(30)+2*p(31)+1*p(32)
    ;checksum=128*p(33)+64*p(34)+32*p(35)+16*p(36)+8*p(37)+4*p(38)+2*p(39)+1*p(40)
    ;-== show real values ==-
    LCDout $FE,$01,"RHdata  Tdata"
    LCDout $FE,$C0,dec humread/10,".",dec humread//10,"%   ",dec tmpread/10,".",dec tmpread//10,"oC" 'print integral part only
    low led
    pause 2000                       'give some time to DHT to stabilize!....
    goto start
    END
    Name:  2013-04-05 12.16.45.jpg
Views: 8199
Size:  63.0 KB

    Finnally I would like to say A BIG THANK YOU to demon for sending me the correct infos to search about that helped me to pinpoint my problem!
    Last edited by bitmaniac; - 5th April 2013 at 11:29.

  7. #7
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,172


    Did you find this post helpful? Yes | No

    Default Re: SOLVED! DHT22, AM2302 and pic18F2320

    Congrats! Did you comfirm the readings with another device? Looks a little hot and dry where you live.

    Robert

Similar Threads

  1. PIC18F2320 RA4 output problem
    By Kristjan in forum General
    Replies: 2
    Last Post: - 31st May 2007, 00:56

Members who have read this thread : 2

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