Simple library for the DHT11 temperature and humidity sensor. Forked from an existing Mbed DHT11 project.

Dependents:   UoY-DHT11-test

Simple DHT11 temperature and humidity library.

Example usage

#include "mbed.h"
#include "DHT11.h"

DHT11 dht(D8); // Change pin name here if required

main()
{
    printf("T:%d, H:%d\r\n", dht.readTemperature(), dht.readHumidity());
}

The sensor may be read as often as desired, but temperature and humidity values are cached and will only be updated if they are more than 2 seconds old. This is the underlying sensor update rate.

Please note that this project has been modified only enough to make it work for its intended purpose. Various parts of this project still need work, and the source code should not be seen as an example of best practice.

Committer:
ajp109
Date:
Wed Sep 15 14:47:06 2021 +0000
Revision:
13:11d0770eb603
Parent:
12:af1eadec17e5
Interface changes to remove the need for separate readData() calls

Who changed what in which revision?

UserRevisionLine numberNew contents of line
s_inoue_mbed 10:f0d789f49df7 1 /* Copyright (c) 2014 Shigenori Inoue, MIT License
s_inoue_mbed 10:f0d789f49df7 2 *
s_inoue_mbed 10:f0d789f49df7 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
s_inoue_mbed 10:f0d789f49df7 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
s_inoue_mbed 10:f0d789f49df7 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
s_inoue_mbed 10:f0d789f49df7 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
s_inoue_mbed 10:f0d789f49df7 7 * furnished to do so, subject to the following conditions:
s_inoue_mbed 10:f0d789f49df7 8 *
s_inoue_mbed 10:f0d789f49df7 9 * The above copyright notice and this permission notice shall be included in all copies or
s_inoue_mbed 10:f0d789f49df7 10 * substantial portions of the Software.
s_inoue_mbed 10:f0d789f49df7 11 *
s_inoue_mbed 10:f0d789f49df7 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
s_inoue_mbed 10:f0d789f49df7 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
s_inoue_mbed 10:f0d789f49df7 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
s_inoue_mbed 10:f0d789f49df7 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
s_inoue_mbed 10:f0d789f49df7 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
s_inoue_mbed 0:4d4c5ea17d86 17 */
s_inoue_mbed 10:f0d789f49df7 18
s_inoue_mbed 0:4d4c5ea17d86 19 #ifndef __DHT11__
s_inoue_mbed 0:4d4c5ea17d86 20 #define __DHT11__
s_inoue_mbed 0:4d4c5ea17d86 21 #include "mbed.h"
s_inoue_mbed 0:4d4c5ea17d86 22
s_inoue_mbed 0:4d4c5ea17d86 23 /** Example:
s_inoue_mbed 0:4d4c5ea17d86 24 * @code
s_inoue_mbed 0:4d4c5ea17d86 25 * #include "mbed.h"
s_inoue_mbed 0:4d4c5ea17d86 26 * #include "DHT11.h"
s_inoue_mbed 0:4d4c5ea17d86 27 *
JohnnyK 12:af1eadec17e5 28 * DHT11 d(D8); // Here fill your PIN mask/name
s_inoue_mbed 0:4d4c5ea17d86 29 *
s_inoue_mbed 0:4d4c5ea17d86 30 * main()
s_inoue_mbed 0:4d4c5ea17d86 31 * {
ajp109 13:11d0770eb603 32 * printf("T:%d, H:%d\r\n", d.readTemperature(), d.readHumidity());
s_inoue_mbed 0:4d4c5ea17d86 33 * }
s_inoue_mbed 0:4d4c5ea17d86 34 * @endcode
s_inoue_mbed 0:4d4c5ea17d86 35 */
s_inoue_mbed 0:4d4c5ea17d86 36
s_inoue_mbed 0:4d4c5ea17d86 37 class DHT11
s_inoue_mbed 9:056d1e9b428c 38 {
s_inoue_mbed 0:4d4c5ea17d86 39 public:
s_inoue_mbed 0:4d4c5ea17d86 40 /** Create a DHT11 interface
s_inoue_mbed 0:4d4c5ea17d86 41 * @param pin 1-wire-like serial I/O port of DHT11
s_inoue_mbed 0:4d4c5ea17d86 42 */
s_inoue_mbed 0:4d4c5ea17d86 43 DHT11(PinName pin);
s_inoue_mbed 0:4d4c5ea17d86 44 ~DHT11();
s_inoue_mbed 0:4d4c5ea17d86 45
ajp109 13:11d0770eb603 46 /** Reading the data from the DHT11. Calling this is not required, but
ajp109 13:11d0770eb603 47 * may be useful for debug.
s_inoue_mbed 0:4d4c5ea17d86 48 * @return Error code
s_inoue_mbed 0:4d4c5ea17d86 49 * 0: OK.
s_inoue_mbed 0:4d4c5ea17d86 50 * 1: Reading the data too often.
s_inoue_mbed 0:4d4c5ea17d86 51 * 2: 1-wire bus is busy.
s_inoue_mbed 0:4d4c5ea17d86 52 * 3: DHT11 does not respond.
s_inoue_mbed 0:4d4c5ea17d86 53 * 4: DHT11 is not ready.
s_inoue_mbed 0:4d4c5ea17d86 54 * 5: Checksum is incorrect.
s_inoue_mbed 5:da586c935e88 55 * 6: Timeout.
s_inoue_mbed 0:4d4c5ea17d86 56 */
s_inoue_mbed 3:8cd064147bde 57 int readData(void);
s_inoue_mbed 3:8cd064147bde 58
ajp109 13:11d0770eb603 59 /** Returns the current humidity from the DHT11. If the existing data record
ajp109 13:11d0770eb603 60 * is older than 2000ms, initiates a read from the device. Note that errors
ajp109 13:11d0770eb603 61 * may result in infinite attempts to read.
ajp109 13:11d0770eb603 62 * @return Humidity in %
s_inoue_mbed 0:4d4c5ea17d86 63 */
s_inoue_mbed 0:4d4c5ea17d86 64 int readHumidity(void);
s_inoue_mbed 3:8cd064147bde 65
ajp109 13:11d0770eb603 66 /** Returns the current temperature from the DHT11. If the existing data record
ajp109 13:11d0770eb603 67 * is older than 2000ms, initiates a read from the device. Note that errors
ajp109 13:11d0770eb603 68 * may result in infinite attempts to read.
ajp109 13:11d0770eb603 69 * @return Temperature in Celcius
s_inoue_mbed 0:4d4c5ea17d86 70 */
s_inoue_mbed 0:4d4c5ea17d86 71 int readTemperature(void);
s_inoue_mbed 3:8cd064147bde 72
s_inoue_mbed 0:4d4c5ea17d86 73 enum ErrorDHT11 {
s_inoue_mbed 0:4d4c5ea17d86 74 OK = 0,
s_inoue_mbed 9:056d1e9b428c 75 READ_TOO_OFTEN = 1,
s_inoue_mbed 0:4d4c5ea17d86 76 BUS_BUSY = 2,
s_inoue_mbed 0:4d4c5ea17d86 77 NOT_PRESENT = 3,
s_inoue_mbed 0:4d4c5ea17d86 78 NOT_READY = 4,
s_inoue_mbed 0:4d4c5ea17d86 79 CHKSUM_ERR = 5,
s_inoue_mbed 0:4d4c5ea17d86 80 WATCHDOG_ERR = 6,
s_inoue_mbed 0:4d4c5ea17d86 81 };
s_inoue_mbed 0:4d4c5ea17d86 82
s_inoue_mbed 0:4d4c5ea17d86 83 private:
s_inoue_mbed 0:4d4c5ea17d86 84 DigitalInOut io;
s_inoue_mbed 0:4d4c5ea17d86 85 InterruptIn io_irq;
s_inoue_mbed 0:4d4c5ea17d86 86 Timer t;
s_inoue_mbed 0:4d4c5ea17d86 87 uint32_t t_pulse_us;
s_inoue_mbed 9:056d1e9b428c 88 const static int t_tol_start;
s_inoue_mbed 9:056d1e9b428c 89 const static int t_tol_pulse;
s_inoue_mbed 0:4d4c5ea17d86 90 uint64_t data;
s_inoue_mbed 0:4d4c5ea17d86 91 uint32_t chksum;
s_inoue_mbed 0:4d4c5ea17d86 92 uint32_t cnt;
s_inoue_mbed 0:4d4c5ea17d86 93 uint32_t wdt;
s_inoue_mbed 0:4d4c5ea17d86 94 bool eod;
s_inoue_mbed 0:4d4c5ea17d86 95 void init(void);
s_inoue_mbed 0:4d4c5ea17d86 96 void pos_edge(void);
s_inoue_mbed 0:4d4c5ea17d86 97 void neg_edge(void);
ajp109 13:11d0770eb603 98 void readNewData(void);
s_inoue_mbed 0:4d4c5ea17d86 99 };
s_inoue_mbed 0:4d4c5ea17d86 100
s_inoue_mbed 0:4d4c5ea17d86 101 #endif