/* ============================================================
Factory Floor Temperature Alert System
-----------------------------------------------------------
Author : Sumit
Date : 03 September 2026
Board : ESP32-S2 (Wokwi: board-esp32-s2-devkitm-1)
Description:
Reads ambient temperature from a DHT22 sensor and drives a
common-cathode RGB LED to give an at-a-glance colour status
of the factory floor temperature:
COLD ( < 20 C ) -> Deep Blue
WARM ( 20 C to 40 C ) -> Amber
HOT ( >= 40 C ) -> Red
Temperature and the active colour state are printed to the
Serial Monitor once every second.
============================================================ */
#include "DHTesp.h"
// ---------- Pin assignments ----------
// DHT22 data pin. GPIO15 is a free, non-strapping digital pin
// on the ESP32-S2 DevKitM-1, safe to use for a simple digital
// sensor bus.
const int DHT_PIN = 15;
// RGB LED pins (common cathode). GPIO4, GPIO5 and GPIO6 are
// all capable of PWM output via the LEDC peripheral, are not
// used for flashing/boot strapping, and sit next to each other
// on the header for tidy wiring.
const int LED_R_PIN = 4;
const int LED_G_PIN = 5;
const int LED_B_PIN = 6;
// ---------- Temperature thresholds (deg C) ----------
const float COLD_WARM_BOUNDARY = 20.0; // below this -> COLD
const float WARM_HOT_BOUNDARY = 40.0; // at/above this -> HOT
// ---------- Colour definitions (0-255 per channel) ----------
// Deep Blue : a dark, saturated blue (not a bright/light blue)
const int COLOR_COLD[3] = {0, 0, 139};
// Amber : warm orange-yellow, visually distinct from red
const int COLOR_WARM[3] = {255, 191, 0};
// Red : full red, no green/blue
const int COLOR_HOT[3] = {255, 0, 0};
// ---------- Timing ----------
const unsigned long PRINT_INTERVAL_MS = 1000; // 1 reading/print per second
unsigned long lastPrintTime = 0;
DHTesp dhtSensor;
// Sets the RGB LED to a given colour using PWM (analogWrite).
void setLedColor(const int color[3]) {
analogWrite(LED_R_PIN, color[0]);
analogWrite(LED_G_PIN, color[1]);
analogWrite(LED_B_PIN, color[2]);
}
// Classifies temperature into one of three states and drives
// the LED accordingly. Returns a human-readable label for
// Serial output.
const char* applyTemperatureState(float tempC) {
if (tempC < COLD_WARM_BOUNDARY) {
// Strictly below 20.0 C -> COLD
setLedColor(COLOR_COLD);
return "COLD (Deep Blue)";
} else if (tempC < WARM_HOT_BOUNDARY) {
// 20.0 C <= temp < 40.0 C -> WARM
// Boundary rule: exactly 20.0 C is treated as WARM because
// the spec defines the WARM band as "20C to 40C" (inclusive
// lower bound), while COLD is defined as strictly "below 20C".
setLedColor(COLOR_WARM);
return "WARM (Amber)";
} else {
// temp >= 40.0 C -> HOT
// Boundary rule: exactly 40.0 C is treated as HOT because the
// spec explicitly states "40C and above" (inclusive lower
// bound) for the HOT band.
setLedColor(COLOR_HOT);
return "HOT (Red)";
}
}
void setup() {
Serial.begin(115200);
delay(500);
pinMode(LED_R_PIN, OUTPUT);
pinMode(LED_G_PIN, OUTPUT);
pinMode(LED_B_PIN, OUTPUT);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
Serial.println("=== Factory Floor Temperature Alert System ===");
Serial.println("COLD < 20C | WARM 20-40C | HOT >= 40C");
Serial.println("------------------------------------------------");
}
void loop() {
unsigned long now = millis();
if (now - lastPrintTime >= PRINT_INTERVAL_MS) {
lastPrintTime = now;
TempAndHumidity data = dhtSensor.getTempAndHumidity();
float temperature = data.temperature;
// Guard against a bad/NaN reading (e.g. sensor not ready yet)
if (isnan(temperature)) {
Serial.println("Sensor read failed, skipping this cycle.");
return;
}
const char* state = applyTemperatureState(temperature);
Serial.print("Temperature: ");
Serial.print(temperature, 1);
Serial.print(" C | State: ");
Serial.println(state);
}
}
Loading
esp32-s2-devkitm-1
esp32-s2-devkitm-1