Skip to main content

Temperatuur en luchtvochtigheid

Temperatuur en luchtvochtigheid

Temperatuur.jpg

DHT11

image.png

(https://elektronicavoorjou.nl/product/dht11-temperatuur-en-vochtigheid-sensor/)

Output

1
Temperature: 20.5 °C
Humidity:    45 %
2
Temperature: 20.5 °C
Humidity:    45 %
...

Code

#include <DHT.h>

#define DHTPIN 2          // Pin connected to the DHT11 data pin
#define DHTTYPE DHT11     // Specify DHT11 sensor

DHT dht(DHTPIN, DHTTYPE);
int count;

void setup() {
  Serial.begin(9600);
  dht.begin();
  count = 0;
  delay(5000);
}

void loop() {
  count++;
  delay(2000); // Wait 2 seconds between readings (DHT11 needs time)
  
  float temp = dht.readTemperature(); // Read temperature as a float
  int humidity = dht.readHumidity();

  
  if (isnan(temp)) {
    Serial.println("Failed to read from DHT sensor!");
  } else {
    Serial.println(count);

    Serial.print("Temperature: ");
    Serial.print(temp, 1);
    Serial.println(" °C");

    Serial.print("Humidity:    ");
    Serial.print(humidity);
    Serial.println(" %");

    Serial.println("");
  }
}