16 bit conversion - AM2302


Closed Thread
Results 1 to 13 of 13

Hybrid View

  1. #1
    Join Date
    Oct 2009
    Posts
    583

    Default 16 bit conversion - AM2302

    Hi guys,

    Following on from the thread when using a DHT11 sensor for humidity readings I've received my SHT11 / AM2302 sensor from E-bay.

    My current routine for reading the DHT11 is

    Code:
    read_dht:
    
    TRISA.1 = 0 '
    high dht_data
    low dht_data : pause 18' send 18ms low
    high dht_data : pauseus 30 ' send 30us hi
    TRISA.1 = 0
    PulsIn PORTA.1, 1, dht
    if dht < 9 then goto badresponse 
    for x = 31 to 0 step-1
    PulsIn PORTA.1, 1, dht[x] ' 1
    next x
    For x = 31 to 0 step-1 
    if dht(x)>=9 and dht(x)<=21 then dht(x)=0 'if pulsewidth between 20 and 40uS then read as '0'
    if dht(x)>=29 and dht(x)<=41 then dht(x)=1 'if pulsewidth between 60 and 80uS then read as '1'
    next x
    hum=dht[31]*128+dht[30]*64+dht[29]*32+dht[28]*16+dht[27]*8+dht[26]*4+dht[25]*2+dht[24]*1
    return
    The sensor uses the same 40 bit data stream as the DHT, but has twice the resolution. The data sheet (http://www.adafruit.com/datasheets/D...r%20AM2302.pdf) suggests that you convert the 16 bit decimal to decimal and then divide that by 10 to get the reading. As the sensor sends RH then Temp then checksum, what would be the simplest way of reading the first 16 bits rather then doing the multiply by 128, 64, 32, 16 etc

  2. #2
    Join Date
    Jan 2013
    Location
    Texas USA
    Posts
    229


    Did you find this post helpful? Yes | No

    Default Re: 16 bit conversion - AM2302

    You could possibly do something like this...

    Code:
    hum var word
    j var byte
    x var byte
    
    read_dht:
    
    TRISA.1 = 0 '
    high dht_data
    low dht_data : pause 18' send 18ms low
    high dht_data : pauseus 30 ' send 30us hi
    TRISA.1 = 0
    PulsIn PORTA.1, 1, dht
    if dht < 9 then goto badresponse 
    for x = 31 to 0 step-1
        PulsIn PORTA.1, 1, dht[x] ' 1
    next x
    'For x = 31 to 0 step-1 
    '    if dht(x)>=9 and dht(x)<=21 then dht(x)=0 'if pulsewidth between 20 and 40uS then read as '0'
    '    if dht(x)>=29 and dht(x)<=41 then dht(x)=1 'if pulsewidth between 60 and 80uS then read as '1'
    'next x
    
    'Stuff values into hum word variable based on pulsewidth value in dht byte arrary
    j = 15
    for x = 31 to 16 step - 1
        if dht(x)>=9 and dht(x)<=21 then hum.0[j] = 0
        if dht(x)>=29 and dht(x)<=41 then hum.0[j] = 1
        j = j-1    
    next x
    Regards,
    TABSoft

  3. #3
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: 16 bit conversion - AM2302

    Thanks Tabsoft,

    I'll give that a try. I did stumble across a similar thread (http://www.picbasic.co.uk/forum/showthread.php?t=17867) and amended the code to

    Code:
    read_dht:
    
    TRISA.1 = 0 '
    high dht_data
    low dht_data : pause 18' send 18ms low
    high dht_data : pauseus 30 ' send 30us hi
    TRISA.1 = 0
    PulsIn PORTA.1, 1, dht
    if dht < 9 then goto badresponse 
    for x = 0 to 31 
    PulsIn PORTA.1, 1, dht(x) ' 1
    next x
    For x = 0 to 31  
    if dht(x)>=9 and dht(x)<=21 then dht(x)=0 'if pulsewidth between 20 and 40uS then read as '0'
    if dht(x)>=29 and dht(x)<=41 then dht(x)=1 'if pulsewidth between 60 and 80uS then read as '1'
    next x
    hum=32768*dht(1)+16384*dht(2)+8192*dht(3)+4096*dht(4)+2048*dht(5)+1024*dht(6)+512*dht(7)+256*dht(8)+128*dht(9)+64*dht(10)+32*dht(11)+16*dht(12)+8*dht(13)+4*dht(14)+2*dht(15)+1*dht(16)           
    
    return
    with an amended display line, but get random digits all over the range from 6453% to 17% !!

    I'll give your example a try a and will get back

  4. #4
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: 16 bit conversion - AM2302

    tabsoft,

    Using your code and the line
    Code:
    LCDOut $FE,$80+9,"RH ",dec hum
    The LCD is displaying 6473, 6730, 6729, 6473, 6729 so I would assume that there is the odd misread now and again.

    What divider should I use of the variable hum to get a realistic figure, and is there any tweaks to the values in the for/next loop to clearly define the 1's and 0's to make the reading of the sensor more stable. I'm using an 18F4580 running at 40 Mhz (10mhz crystal with the HS_ppl option enabled)

    EDIT:

    For info using
    Code:
    LCDOut $FE,$80+9,"RH ",#hum
    the values jump around 18714, 18970,18970,18714,
    Last edited by Scampy; - 10th April 2015 at 17:12.

  5. #5
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: 16 bit conversion - AM2302

    I'm assuming that for 100% humidity the value of hum would be 65535 and 0% would be 0 ?

    If so then using DEC hum/256 gives a reading of 73, 74 on the LCD - which seems a tad to high for the living room. The cheap simple dial hydrometer in the reptile enclosures read 37% and the sensor built into the weather station which is 18" away from the development board displaying 42%
    Last edited by Scampy; - 10th April 2015 at 17:25.

  6. #6
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: 16 bit conversion - AM2302

    OK, just to check things I got my Arduino out and downloaded a sketch and library file. here's the results

    Name:  humidity.png
Views: 780
Size:  81.4 KB

    which is close enough to the readings of both hydrometers in the reptile tanks.

    Now I don't understand the C style of these arduino sketches, but the link to the website is http://www.electroschematics.com/112...orial-library/ - maybe it might help in working out why this is such a pain to do in PBP

  7. #7
    Join Date
    Jan 2013
    Location
    Texas USA
    Posts
    229


    Did you find this post helpful? Yes | No

    Default Re: 16 bit conversion - AM2302

    Give this a try.

    Code:
    hum var word
    j var byte
    x var byte
    
    
    read_dht:
    
    TRISA.1 = 0 ' Make pin an input
    high dht_data   '250ms High
    pause 250
    
    low dht_data    '20ms Low
    pause 20' send 20ms low
    
    high dht_data   '40us High
    pauseus 40
    
    PulsIn PORTA.1, 1, dht
    if dht < 9 then goto badresponse 
    for x = 31 to 0 step-1
        PulsIn PORTA.1, 1, dht[x] ' 1
    next x
    
    hum = 0 'Clear hum variable
    'Stuff values into hum word variable based on pulsewidth value in dht byte arrary
    j = 15
    for x = 31 to 16 step - 1
       for x = 31 to 16 step - 1
        if dht(x)>=35 then hum.0[j] = 1
        j = j - 1    
    next x
    return
    and then to display try this.

    Code:
    LCDOut $FE,$80+9,"RH ",dec hum/10, "." dec hum //10
    Regards,
    TABSoft

Similar Threads

  1. DHT22, AM2302 and pic18F2320
    By bitmaniac in forum mel PIC BASIC Pro
    Replies: 17
    Last Post: - 15th November 2015, 00:42
  2. Replies: 10
    Last Post: - 8th March 2015, 19:26
  3. Replies: 17
    Last Post: - 22nd May 2014, 19:58
  4. Bit conversion
    By lerameur in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 5th November 2006, 11:11
  5. Conversion of 10-bit word into ASCII
    By Barry Johnson in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 16th January 2005, 14:26

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