DHT Sensor Library

+ Reply to Thread
Results 1 to 9 of 9
  1. #1
    Join Date
    Jan 2013
    Texas USA

    Default DHT Sensor Library

    After helping out others a few times here on the forum implementing DHT series Humidity/Temperature sensors,
    I thought I might put together a DHT Sensor support library for PBP and hopefully others can benefit from it.

    I must give a huge thank you to "Scampy" on this forum, as he was key to this library getting completed.
    He provided great improvement suggestions and did the vast majority of the real world testing of the library in its various versions.

    A Note of caution when using this library.
    Retrieving data from these sensors is time sensitive since once initiated the sensor streams all of the data bits until it's finished.
    Because of this fact, if you are using Interrupts you may want to disable them during the Sensor read sequences.
    This information is highlighted in the source code at the points where an Interrupt may cause issues.

    The DHT family of sensors uses a proprietary 1-wire interface where the data sent by the sensor is a time-driven high pulse.
    The sensor sends a "0" as a high pulse of 26-28us long and a "1" as a high pulse of 70us long.
    So some means of measuring the duration of a High signal is required for accurate use.

    There are many ways to communicate with and retrieve data from these sensors and measuring the pulses, from using
    PIC HW timers to using pure PBP software commands, each with its own benefits.
    While HW timers are very accurate, it does require the Timer HW module of the PIC.
    Using the Timer module to aid in the communication with the sensor can limit the ease of implementation of a library.
    Once you settle on a Timer to use, surely someone will not be able to use that Timer module for one reason or another.
    This will lead to customization of the library by the user, so I have opted for a software approach instead.

    The consistent stumbling block with reading the data from the sensor, that I noticed, was the manual computation of
    timing/count values to determine data "0s" and "1s" coming from the sensor for various oscillator speeds.

    So this lead me to the primary goal of a library that would automatically compute these values.

    With the primary goal established, I came up with following objectives for the library that I believe to have accomplished.

    1. Software Implementation
        * (not bound to PIC hardware modules)
        * Use any available Digital Input Pin on the PIC
        * Facilitate portability across various PIC MCUs and User Apps
        % Achieved
            - Similar to a "Function" call 
    2. Standard PBP Commands
        * Stay away from ASM (for the most part)
        * Support for PBP v2.6 and higher
        % Achieved with PULSIN (and a small amount of ASM for ease of use)
    3. Consistent Interface Supporting Multiple Sensor Types
        * All parameters passed and data returned from the library consistent for all Sensor Types supported
        % Achieved 
            - Supports DHT11, DHT21, DHT22, AM2302 sensors (uniform parameters)
            - All data returned is consistently formatted 
    4. Support for Multiple Concurrent Sensors
        * Support scalability from 1 to many sensors
        % Achieved - Currently has 4 sensors but can be scaled up or down 
    5. Ease of Use
        * As few "knobs" to manually turn as possible
        * Make it an Include file
        % Achieved 
            - (3) Statements to invoke: Sensor Number=, Sensor Type=, Gosub DHT_READ
            - (1) Alias for each sensor used, (1) Constant for MAX Sensors, (1) DEFINE for PULSIN_MAX, (1) Standard DEFINE OSC x
            - Variability of internal settings computed automatically at compile time
            - Single Include File
    6. Quick Response Time
        * Return the data or errors as fast as possible
        % Achieved 
            - User parameters, Comms Initialization and Timeout errors are checked
            - Short-circuit logic returns to calling program upon errors
    7. Support a Wide Selection of MCU Speeds
        * Support as many, if not all of the supported PBP "DEFINE OSC" values
        % Achieved
            - Supports 8,10,12,16,20,24,25,32,33,40,48 & 64 MHz OSC values. 
            - (3 & 4 MHz not supported in this library) 
    8. Robust Error Checking/Reporting
        * Error-check user and sensor data
        * Return Error values for user evaluation
        % Achieved
            - Alarms and Messages during compile time
            - Returns an Error byte with (8) error conditions for user evaluation
            - Returns both received and computed checksum of sensor data
    I believe the source code is well commented, so it should be fairly easy to use and understand.
    I have included Notes and Warnings within the code to call attention to how to use the library effectively.

    I hope you find this library beneficial.


    For those who are interested, I do have a PBP v3 only library that makes the use simpler, in my opinion.
    The same as before. (3) Statements

    (1) Alias for each sensor used
    (1) #Define to enable each sensor used
    (1) #Define for the Oscillator Speed (This is additional to the standard PBP Define OSC x statement which is required)

    This structure allows the compilation process to use #ifdef statements to select and compile only code for the sensors used,
    and to select the proper PULSEIN_MAX value among other things.
    This eliminates manually commenting out code for unused sensors to ensure more compact compiled code.


  2. #2
    Join Date
    Oct 2009

    Default Re: DHT Sensor Library

    Thanks for the compliment, but I had the easy part of just compiling the code and observing / recording the results.

    The library file that "Tabsoft" has produced is a fantastic addition for anyone using these sensors. It's simple to use with just a few lines at the start of your main program, and a single gosub line to gather the data. I have an AM2302 which has been incorporated into a revision of my reptile environment controller using this library file and was really simple to implement.

    I would like to thank "Tabsoft" for asking me to take part in the development of the file. It was fun, and more to the point has allowed me to participate in giving something back to the PBP community.

  3. #3
    Join Date
    Oct 2013

    Default Re: DHT Sensor Library

    Hi to all,
    I tested this library and works excellent with AM2302 sensor.Is it possible to measure negative temperature with this library.I dont have a FREEZ spray at the moment so I can not tested it.Did someone try measure negative temperature.In code I do not see the variable to show negative sign on screen.
    Best regards

  4. #4
    Join Date
    Jul 2004

    Default Re: DHT Sensor Library

    Hi Tabsoft many thanks for your efforts.

    I have my license for pbp 3.0 yet unused ( for more than one year lol ) but hope that this summer will have some time to experiment with it. Can you suppli the library for the 3.0 versión?

    Thanks in advance
    Carpe Diem

  5. #5
    Join Date
    Jan 2013
    Texas USA

    Default Re: DHT Sensor Library

    Sorry for the delayed response.

    Here is the library for PBP3.



  6. #6
    Join Date
    Feb 2013

    Default Re: DHT Sensor Library

    How to remove unused sensors?
    I'm using only one, but when I comment out DHT_Sensor2 and others, code won't compile.
    Or I should leave these as is?

  7. #7
    Join Date
    Oct 2009

    Default Re: DHT Sensor Library

    LOL - I've not been on this forum for years, and today received notification of your post... How you doing ?

    Like I said I've not been active on here for some years, and no longer program in PBP so I'm rusty when it comes to the workings

    Comments taken form that library file file suggest

    '-- To REMOVE SENSORS perform the following:                              --
    '--   1. Change the "AM2302_MAX_SENSORS" constant to the correct number.  --
    '--   2. Comment out the appropriate "AM2302_Sensor_x" Alias lines.       --
    '--   3. In the "AM2302_Read" subroutine in the library file,             --
    '--      edit the "select case AM2302_Sensor_Num" block, comment out the  --
    '--      "Case x" portion for the sensor number you want to remove.       --
    '--   4. Comment out the entire "AM_Readx" subroutine at the end          --
    '--      of this library file for the sensor you want to remove.          --
    and this needs editing

        select case AM2302_Sensor_Num
            case 1           'Sensor #1      
                gosub Read_AM1
            case 2           'Sensor #2
                gosub Read_AM2
            case 3           'Sensor #2
                gosub Read_AM3
            case 4           'Sensor #2
                gosub Read_AM4
        end select

    Looking at the code for the version of thermostat that I used as a test platform when Tabsoft was developing the library the main program has this line in the set up area of the code (using include "AM2302_LIB_v024.bas" )

    ;----[AM2302 ]------------------------------------------------------------------
    AM2302_MAX_SENSORS con 4                        ' Number of Sensors Used

    I then used a word array for the temp and humidity values

    AM2302_H          var word[4]                   ' AM2302 sensors - Humidity values
      AM2302H1        var AM2302_H[0]
      AM2302H2        var AM2302_H[1]
      AM2302H3        var AM2302_H[2]
      AM2302H4        var AM2302_H[3]
    AM2302_T          Var Word[4]                   ' AM2302 sensors - temperatures
      AM2303T1        Var AM2302_T[0]
      AM2303T2        Var AM2302_T[1]
      AM2303T3        Var AM2302_T[2]
      AM2303T4        Var AM2302_T[3]
    I guess if using just the one sensor this could be a straight word variable

    I then have a link to a subroutine

    for cn=1 to 4  
    AM2302_Sensor_Num = cn     
    gosub read_dht
    And the subroutine it links to is

        gosub AM2302_Read   
        AM2302_H[Pid_channel]= AM2302_Hum
        AM2302_T[Pid_channel]= AM2302_Temp
        if AM2302_Temp/10=0  then return  
        serout2 portB.0, 84, [$1B,"[H"]
        serout2 portB.0, 84,[$1b, "[", dec cn+1, "B"]  
        serout2 portB.0, 84, [$1B,"[11C"]   
        serout2 portB.0, 84, [dec AM2302_Temp/10,".",dec AM2302_Temp//10]
        serout2 portB.0, 84, [$1B,"[H"]
        serout2 portB.0, 84,[$1b, "[", dec cn+1, "B"]
        serout2 portB.0, 84, [$1B,"[17C"]
        serout2 portB.0, 84, [dec AM2302_Hum/10,"%"]    
    Ignore all the serial out commands, I was using a serial to GLCD adapter at the time. The gosub called in the above subroutine is in the library file.

    Hopefully that dissection of my old program helps ? I sold my old EasyPic5 board at the beginning of September this year, and uninstalled PBP years ago, so have no means to test, but the code ran for a few years in the controller before I move to the dark side and ported the code to an Arduino Mega to take advantage of colour TFT screens.

  8. #8
    Join Date
    Apr 2014

    Default Re: DHT Sensor Library

    Scampy, thanks for the visit.Can I get you a cup of coffee? Pertaining to TFTs, I started working with the Nextion touch screens. There is an article series still running in Nuts and Volts Magazine controlling the Nextion with PBP3 that might interest you. Myself, I also moved to bigger challenges with MPLABX & XC8/XC16.

  9. #9
    Join Date
    Feb 2013

    Default Re: DHT Sensor Library

    Will this library measure negative temperature?
    I'm integrating that AM2302 into my VFD clock, as outdoors sensor...

Similar Threads

  1. Replies: 6
    Last Post: - 14th October 2013, 13:27
  2. DHT 11 Humidity & Temperature Sensor
    By jefrem in forum mel PIC BASIC
    Replies: 2
    Last Post: - 30th July 2013, 20:15
  3. Dht 11 & dht 22
    By DavidK in forum Off Topic
    Replies: 5
    Last Post: - 15th May 2012, 15:57
  4. SD card library?
    By Alexey in forum mel PIC BASIC Pro
    Replies: 22
    Last Post: - 7th February 2012, 12:28
  5. Library tools? Library Specs?
    By PickyBiker in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 21st May 2010, 03:54

Posting Permissions

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