DHT Master Library for Arduino framework by Diogo Cunha is licensed under CC BY 4.0
This is an improvement of Adafruit DHT Sensor Library. Many thanks for all folks whom developed the original software.
You can use this library in:
- Arduino IDE any version;
- VS Code using Platform.io choosing "Framework: Arduino";
- Include the library in your
platformio.inifile as bellow:
- Include the library in your
lib_deps =
https://github.com/diogo-dgc/dhtMaster@^1.0.0 ;DHT Master libraryThis library is optimezed for ESP-8266. However you can also use this library with ESP-32 and AVR microcontrolles.
Note: Building was tested only on ESP-8266 microcontrollers
#include <Arduino.h>
#include <DHT.h>
DHT myDht;
void setup(){
myDht.setSensorPinout(D1);
myDht.setSensorType(DHT22);
Serial.begin(9600);
myDht.begin();
}
void loop(){
delay(2000);
float humidity = myDht.getHumidity();
float temperature = myDht.getTemperatureCelsius();
if (isnan(humidity) || isnan(temperature)) {
Serial.println(F("Failed to read from DHT sensor!"));
}else{
Serial.printf("T: %4.2f ºC|\t HI: %4.2f ºC\n", myDht.getTemperatureCelsius(), myDht.getHeatIndexCelcius());
Serial.printf("T: %4.2f ºF|\t HI: %4.2f ºF\n", myDht.getTemperatureFahrenheit(), myDht.getHeatIndexFahrenheit());
Serial.printf("T: %4.2f K\t\n", myDht.getTemperatureKelvin());
Serial.printf("Umidade: %4.2f \t\n\n-----\n\n", myDht.getHumidity());
}
}The public methods that you can use are presented bellow:
DHT
Use to instance and use your DHT sensor, for example:
DHT mySensor;Note: By default, the constructor initialize the pinout as D7 for de microcontroller and the sensor type DHT11
void setSensorPinout(uint8_t pinout)
You can define a different pinout to connect your sensor, for exemple:
void setup(){
mySensor.setSensorPinout(D1);
}uint8_t getSensorPinout()
void setSensorType(DhtType sensorType)
You can define which DHT sensor you want to use. The possible values are the constants DHT11, DHT12, DHT22, DHT23 or AMS3201, for exemple:
void setup(){
mySensor.setSensorType(DHT22);
}Getters Sensor Type and Sensor Pinout
You can verify, any time, which pinout or sensor type you are difined using, respectively, getSensorPinout() or getSensorType() that will return an 8 bits integer, for example:
void setup(){
Serial.begin(9600);
mySensor.setSensorPinout(D1);
uint8_t pinoutDefined = mySensor.getSensorPinout();
mySensor.setSensorType(DHT22);
uint8_t sensorTypeDefined = mySensor.getSensorType();
mySensor.begin();
Serial.printf("Pinout: %d | Type: DHT%d\n", pinoutDefined, sensorTypeDefined);
}Note: The method
getSensorPinout()will return the PORT PIN from the microcontroller. For example: D1 in ESP-8266 will return 5 and the same D1 in Arduino Uno will return 1.
void setPullupTime(uint8_t timeBeforeStart)
By default this value is setted to 55 us due to DHT datasheet instructs to define pullup.
Although, you can change the pullup time, for exemple:
void setup(){
mySensor.setPullupTime(180);
}[!WARNING] Important: It is not recommended change this value unless you know exactly what you want according the DHT datasheet.
void turnOnForcedMode() e void turnOffForcedMode()
Force Mode is used to ignore the predetermined time between get samples, which is 2 seconds.
By default the Force Mode is setted as false. But you can change this state using the methods turnOnForcedMode() or turnOffForcedMode(), for exemple:
void setup(){
mySensor.turnOnForcedMode();
}void begin()
After define all attributes of your sensor you will need initiate it, for exemple:
void setup(){
mySensor.setSensorPinout(D1);
mySensor.setSensorType(DHT22);
mySensor.begin();
}Getting data
Your can easily get relative humidity, temperature (in Celsius degree, Fahrenheit degree or Kelvin) and heat index (in Celsius degree or Fahrenheit degree).
All methods to get data will return the value in float or a value NAN if any error occur.
void setup(){
delay(2000);
float umidity = mySensor.getHumidity();
float temperatureC = mySensor.getTemperatureCelsius();
float temperatureF = mySensor.getTemperatureFahrenheit();
float temperatureK = mySensor.getTemperatureKelvin();
float HeatIndexF = mySensor.getHeatIndexFahrenheit();
float HeatIndexC = mySensor.getHeatIndexCelcius();
}