Automatic dusk-to-dawn light
Lights switch on by themselves at dusk and off again in the morning.
Shop by category
Shop all componentsOn a humid day 32 degrees feels far worse than on a dry day. The heat index combines temperature and humidity into one feels-like figure. The DHT22 gives both, the DHT library calculates the heat index, and the ESP32 turns a fan relay on above 32 degrees and off again below 30, so it does not chatter. The OLED shows the actual and feels-like values.
Anything connected to 230 V mains must be installed by a qualified electrician in a closed, earthed enclosure. Test with a low-voltage lamp first.
Tick anything you already have and the total updates.
Follow the colours: red is power, dark grey is ground, blue is a signal.
USB phone charger. Anything connected to 230 V mains must be installed by a qualified electrician in a closed, earthed enclosure. Test with a low-voltage lamp first.
heat_index_fan.inoVerify & upload in Electrolark#include <DHT.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>
DHT dht(4, DHT22);
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
const int RELAY_PIN = 26;
const bool ACTIVE_LOW = true;
const float ON_C = 32.0, OFF_C = 30.0; // heat index limits
bool fan = false;
void setFan(bool on) { fan = on; digitalWrite(RELAY_PIN, (on == ACTIVE_LOW) ? LOW : HIGH); }
void setup() {
pinMode(RELAY_PIN, OUTPUT);
setFan(false);
dht.begin();
Wire.begin(21, 22);
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
}
void loop() {
float t = dht.readTemperature(), h = dht.readHumidity();
if (!isnan(t) && !isnan(h)) {
float hi = dht.computeHeatIndex(t, h, false); // false = Celsius
if (!fan && hi > ON_C) setFan(true);
else if (fan && hi < OFF_C) setFan(false);
oled.clearDisplay(); oled.setTextColor(WHITE); oled.setTextSize(1);
oled.setCursor(0, 0); oled.print("Temp "); oled.print(t, 1); oled.print(" C "); oled.print(h, 0); oled.print("%");
oled.setCursor(0, 20); oled.print("Feels like");
oled.setTextSize(3); oled.setCursor(0, 32); oled.print(hi, 1);
oled.setTextSize(1); oled.setCursor(80, 54); oled.print(fan ? "FAN ON" : "fan off");
oled.display();
}
delay(2000);
}
Check the DHT22 wiring and that the data pin is GPIO 4.
Widen the gap between ON_C and OFF_C.
Flip ACTIVE_LOW.
A feels-like temperature that adds the effect of humidity.
Not directly. Mains and compressors need proper hardware; switch the thermostat instead.
About half a degree and a few percent humidity.
6 parts: ESP32-WROOM-32 38-Pin Development Board, DHT22 AM2302 Digital Temperature & Humidity Sensor, 5V Relay Module with Optocoupler, 0.96" OLED Display 128x64, Solderless Breadboard and Dupont Jumper Wires (40 pcs). On this page you can tick the parts you already have and add only the rest to your cart.
It is a intermediate project and takes about 1.5 hours. The build on this page has 6 steps.
No. This page has the complete, tested code (heat_index_fan.ino).
About ₹798 for the parts at the prices on this page. Quantity price breaks lower the cost when you build several.
Calculating heat index with the DHT library; Hysteresis on a fan relay; Showing two values on an OLED.
Anything connected to 230 V mains must be installed by a qualified electrician in a closed, earthed enclosure. Test with a low-voltage lamp first.
Ideas that reuse this build: ESP32 MQTT temperature dashboard. They are listed under Take it further on this page.
Lights switch on by themselves at dusk and off again in the morning.
A precise thermostat that keeps tropical fish at a steady 25–26 °C.
Starts the pump when the tank runs low and stops it when it is full.
Tap a hidden pad to switch a lamp on or off, even through a wooden desk.
Learning project. Check every circuit and the datasheet of each part before you power it up.