#include <DHT.h>
#define DHTPIN 4 // Pin donde está conectado el DHT22
#define DHTTYPE DHT22 // Definimos el tipo de sensor
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
dht.begin();
}
void loop() {
// Lectura de temperatura y humedad
float h = dht.readHumidity();
float t = dht.readTemperature();
// Comprobamos si hay errores en la lectura
if (isnan(h) || isnan(t)) {
Serial.println("Error al leer el DHT22");
return;
}
// Mostramos los valores en el monitor serie
Serial.print("Humedad: ");
Serial.print(h);
Serial.print("% Temperatura: ");
Serial.print(t);
Serial.println("°C");
delay(2000);
}