Advertisement
mikroavr

baca_dht22

Jul 3rd, 2023
885
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "DHT.h"
  2.  
  3. #define DHTPIN 16     // Digital pin connected to the DHT sensor
  4. #define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
  5. DHT dht(DHTPIN, DHTTYPE);
  6.  
  7. void setup() {
  8.   Serial.begin(115200);
  9.   Serial.println(F("DHTxx test!"));
  10.  
  11.   dht.begin();
  12. }
  13.  
  14. void loop() {
  15.   // Wait a few seconds between measurements.
  16.   delay(2000);
  17.  
  18.   // Reading temperature or humidity takes about 250 milliseconds!
  19.   // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  20.   float h = dht.readHumidity();
  21.   // Read temperature as Celsius (the default)
  22.   float t = dht.readTemperature();
  23.   // Read temperature as Fahrenheit (isFahrenheit = true)
  24.   float f = dht.readTemperature(true);
  25.  
  26.   // Check if any reads failed and exit early (to try again).
  27.   if (isnan(h) || isnan(t) || isnan(f)) {
  28.     Serial.println(F("Failed to read from DHT sensor!"));
  29.     return;
  30.   }
  31.  
  32.   // Compute heat index in Fahrenheit (the default)
  33.   float hif = dht.computeHeatIndex(f, h);
  34.   // Compute heat index in Celsius (isFahreheit = false)
  35.   float hic = dht.computeHeatIndex(t, h, false);
  36.  
  37.   Serial.print(F("Humidity: "));
  38.   Serial.print(h);
  39.   Serial.print(F("%  Temperature: "));
  40.   Serial.print(t);
  41.   Serial.print(F("°C "));
  42.   Serial.print(f);
  43.   Serial.print(F("°F  Heat index: "));
  44.   Serial.print(hic);
  45.   Serial.print(F("°C "));
  46.   Serial.print(hif);
  47.   Serial.println(F("°F"));
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement