
Le componenti
Arduino Uno R3 (1 pezzo)
LCD 16×2 (1 pezzo)
Resistenza 10 kΩ (1 pezzo)
Sensore temperatura DHT11 (1 pezzo)
Resistenza 1 kΩ (1 pezzo)
Motore CC (1 pezzo)
Cavi jumper (19 pezzi)
Cablaggio

Codice
#include <LiquidCrystal.h>
#include <DHT.h>
#define DHTPIN 7
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
const int alertPin = 13;
unsigned long lastUpdate = 0;
const unsigned long updateInterval = 2000; // 2 seconds
void setup() {
lcd.begin(16, 2);
dht.begin();
pinMode(alertPin, OUTPUT);
digitalWrite(alertPin, LOW);
lcd.print("Starting...");
delay(1000);
lcd.clear();
}
void loop() {
unsigned long now = millis();
if (now - lastUpdate >= updateInterval) {
lastUpdate = now;
float temp = dht.readTemperature();
float hum = dht.readHumidity();
lcd.clear();
if (isnan(temp) || isnan(hum)) {
lcd.setCursor(0, 0);
lcd.print("Sensor error");
digitalWrite(alertPin, LOW);
} else {
lcd.setCursor(0, 0);
lcd.print("Temp:");
lcd.print(temp, 1);
lcd.print((char)223); // degree symbol
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Hum:");
lcd.print(hum, 1);
lcd.print("%");
if (hum > 50) {
digitalWrite(alertPin, HIGH);
} else {
digitalWrite(alertPin, LOW);
}
}
}
}