DHT Sensor Library

+ Reply to Thread
Results 1 to 5 of 5
  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.



Similar Threads

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