/**
ESP32 + DHT22 + LDR Example with LED indicators
https://wokwi.com/arduino/projects/322410731508073042
*/
#include "DHTesp.h"
/* For DHT22 (white sensor) */
const int DHT_PIN = 15;
DHTesp dhtSensor;
/* For LDR (blue sensor) */
#define LIGHT_SENSOR_PIN 33 // ESP32 pin GPIO33 (ADC)
/* LED Output Pins */
const int DHT_LED_PIN = 22; // LED for DHT22 temperature indicator
const int LDR_LED_PIN = 21; // LED for LDR light indicator
/* For the thermistor */
const float BETA = 3950; // should match the Beta Coefficient of the thermistor
const int THERMISTOR_PIN = 4;
const int THERMISTOR_LED_PIN = 32;
/* Thresholds */
const float TEMP_THRESHOLD = 40; // Temperature threshold for DHT LED (in °C)
const int LIGHT_THRESHOLD = 800; // Light threshold for LDR LED
void setup() {
Serial.begin(115200);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
// Set up LED pins as outputs
pinMode(DHT_LED_PIN, OUTPUT); // pin 34
pinMode(LDR_LED_PIN, OUTPUT); // pin 21
// For the thermistor
analogReadResolution(10);
pinMode(THERMISTOR_PIN, INPUT);
pinMode(THERMISTOR_LED_PIN, OUTPUT);
}
void loop() {
// --- Read temperature and humidity from DHT22 ---
TempAndHumidity data = dhtSensor.getTempAndHumidity();
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
// --- Read the light sensor (LDR) --- prende sensor derecha arriba
int analogValue = analogRead(LIGHT_SENSOR_PIN);
Serial.print("Analog Value = ");
Serial.print(analogValue); // Raw sensor value
Serial.println("LDR LED: " + String(digitalRead(LDR_LED_PIN) ? "ON" : "OFF"));
// --- Read thermistor --- prende sensor
int analogValue2 = analogRead(THERMISTOR_PIN);
float celsius = 1 / (log(1 / (1023. / analogValue2 - 1)) / BETA + 1.0 / 298.15) - 273.15;
Serial.print("Thermistor Temperature: ");
Serial.print(celsius);
Serial.println(" ℃");
// Condition for thermistor LED
Serial.println("---"); // Reading separator
// Para led 1
// Control DHT LED based on temperature - prende sensor del centro
if (data.temperature >= TEMP_THRESHOLD && data.humidity >= 50) { // Si mayor a 40 prende temperatura1 prende led
digitalWrite(DHT_LED_PIN, HIGH);
Serial.println("DHT LED: ON (High Temperature)");
} else {
digitalWrite(DHT_LED_PIN, LOW);
Serial.println("DHT LED: OFF (Normal Temperature)");
}
// para led 2 -- derecha arriba
// Light classification and LED control
if (analogValue < 40) {
Serial.println(" => Dark");
digitalWrite(LDR_LED_PIN, LOW);
} else if (analogValue < LIGHT_THRESHOLD) {
Serial.println(" => Dim");
digitalWrite(LDR_LED_PIN, LOW);
} else if (analogValue < 2000) {
Serial.println(" => Light");
if (celsius < 5){
digitalWrite(LDR_LED_PIN, HIGH); // Si menor a 2000 prende
}
} else if (analogValue < 3200) {
Serial.println(" => Bright");
digitalWrite(LDR_LED_PIN, HIGH); // Si menor a 3200 prende
} else {
Serial.println(" => Very bright"); // Si menor a 3200 prende
digitalWrite(LDR_LED_PIN, HIGH);
}
// para sensor 3 -- izquierda arriba
if (celsius >= 35 && data.humidity >= 50) { // Si mayor a 35 temperatura2 prende
digitalWrite(THERMISTOR_LED_PIN, HIGH);
Serial.println("Thermistor LED: ON");
} else {
digitalWrite(THERMISTOR_LED_PIN, LOW);
Serial.println("Thermistor LED: OFF");
}
delay(2000); // Wait 2 seconds before the next measurement
}