DHT sensor library
DHT.h
Go to the documentation of this file.
1 
18 #ifndef DHT_H
19 #define DHT_H
20 
21 #include "Arduino.h"
22 
23 /* Uncomment to enable printing out nice debug messages. */
24 //#define DHT_DEBUG
25 
26 #define DEBUG_PRINTER \
27  Serial
30 /* Setup debug printing macros. */
31 #ifdef DHT_DEBUG
32 #define DEBUG_PRINT(...) \
33  { DEBUG_PRINTER.print(__VA_ARGS__); }
34 #define DEBUG_PRINTLN(...) \
35  { DEBUG_PRINTER.println(__VA_ARGS__); }
36 #else
37 #define DEBUG_PRINT(...) \
38  {}
39 #define DEBUG_PRINTLN(...) \
40  {}
41 #endif
42 
43 /* Define types of sensors. */
44 #define DHT11 11
45 #define DHT12 12
46 #define DHT22 22
47 #define DHT21 21
48 #define AM2301 21
50 #if defined(TARGET_NAME) && (TARGET_NAME == ARDUINO_NANO33BLE)
51 #ifndef microsecondsToClockCycles
52 
56 #define microsecondsToClockCycles(a) ((a) * (SystemCoreClock / 1000000L))
57 #endif
58 #endif
59 
63 class DHT {
64 public:
65  DHT(uint8_t pin, uint8_t type, uint8_t count = 6);
66  void begin(uint8_t usec = 55);
67  float readTemperature(bool S = false, bool force = false);
68  float convertCtoF(float);
69  float convertFtoC(float);
70  float computeHeatIndex(bool isFahrenheit = true);
71  float computeHeatIndex(float temperature, float percentHumidity,
72  bool isFahrenheit = true);
73  float readHumidity(bool force = false);
74  bool read(bool force = false);
75 
76 private:
77  uint8_t data[5];
78  uint8_t _pin, _type;
79 #ifdef __AVR
80  // Use direct GPIO access on an 8-bit AVR so keep track of the port and
81  // bitmask for the digital pin connected to the DHT. Other platforms will use
82  // digitalRead.
83  uint8_t _bit, _port;
84 #endif
85  uint32_t _lastreadtime, _maxcycles;
86  bool _lastresult;
87  uint8_t pullTime; // Time (in usec) to pull up data line before reading
88 
89  uint32_t expectPulse(bool level);
90 };
91 
95 class InterruptLock {
96 public:
97  InterruptLock() {
98 #if !defined(ARDUINO_ARCH_NRF52)
99  noInterrupts();
100 #endif
101  }
102  ~InterruptLock() {
103 #if !defined(ARDUINO_ARCH_NRF52)
104  interrupts();
105 #endif
106  }
107 };
108 
109 #endif
float convertCtoF(float)
Converts Celcius to Fahrenheit.
Definition: DHT.cpp:133
DHT(uint8_t pin, uint8_t type, uint8_t count=6)
Instantiates a new DHT class.
Definition: DHT.cpp:43
float convertFtoC(float)
Converts Fahrenheit to Celcius.
Definition: DHT.cpp:141
bool read(bool force=false)
Read value from sensor or return last one from less than two seconds.
Definition: DHT.cpp:232
float computeHeatIndex(bool isFahrenheit=true)
Compute Heat Index Simplified version that reads temp and humidity from sensor.
Definition: DHT.cpp:175
Class that stores state and functions for DHT.
Definition: DHT.h:66
void begin(uint8_t usec=55)
Setup sensor pins and set pull timings.
Definition: DHT.cpp:64
float readTemperature(bool S=false, bool force=false)
Read temperature.
Definition: DHT.cpp:86
Class that defines Interrupt Lock Avaiability.
Definition: DHT.h:98
float readHumidity(bool force=false)
Read Humidity.
Definition: DHT.cpp:149