#include "DHT.h"
#define DHTPIN 12 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT 22
#define LEDCNT 10 // Set number of LEDs in the bar graph
DHT dht(DHTPIN, DHTTYPE);
// Array of pins for LEDs in the bar graph
int ledPins[] = {
2, 3, 4, 5, 6, 7, 8, 9, 10, 11
};
void setup() {
Serial.begin(9600);
for (int thisLed = 0; thisLed < LEDCNT; thisLed++) {
pinMode(ledPins[thisLed], OUTPUT);
}
dht.begin();
}
void loop() {
// Wait a few seconds between measurements.
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// Count turn on LEDs in the bar graph
int ledCntOn = (int)(t / 4);
for (int thisLed = 0; thisLed < LEDCNT; ++thisLed) {
if (ledCntOn > thisLed)
digitalWrite(ledPins[thisLed], HIGH);
else
digitalWrite(ledPins[thisLed], LOW);
}
Serial.print(F("Temperature: "));
Serial.print(t);
Serial.print(F("°C\n"));
}