Skip to content

Instantly share code, notes, and snippets.

@kakopappa
Created September 21, 2023 08:00
Show Gist options
  • Save kakopappa/08c5cd5bbee8da13e6ca081afd017974 to your computer and use it in GitHub Desktop.
Save kakopappa/08c5cd5bbee8da13e6ca081afd017974 to your computer and use it in GitHub Desktop.
DHT11 and DHT22, AM2302, RHT03
#include "DHT.h" // https://github.com/markruys/arduino-DHT Support for DHT11 and DHT22, AM2302, RHT03
#if defined(ESP8266)
#define DHT_PIN D1
#elif defined(ESP32)
#define DHT_PIN 16
#endif
float temperature;
float humidity;
DHT dht;
void setup() {
Serial.begin(115200);
delay(10);
dht.setup(DHT_PIN);
}
void loop() {
delay(dht.getMinimumSamplingPeriod());
temperature = dht.getTemperature();
humidity = dht.getHumidity();
if (isnan(temperature) || isnan(humidity)) {
Serial.printf("DHT reading failed!\r\n");
return;
} else {
Serial.printf("Temperature: %2.1f Celsius\tHumidity: %2.1f%%\r\n", temperature, humidity);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment