Build Project

Home Automation

ESP32 Smart Thermostat: Temperature Control and Code

Build an ESP32 thermostat with DHT22 temperature sensing, relay fan control, hysteresis, safe low-voltage wiring, and complete Arduino code.

IntermediateAges 12+90-120 minUnder $30Parent Safe
Project Mission

Build Your Smart Thermostat

The Story

ESP32 Smart Thermostat turns the ESP32 into a real object that senses something, decides what it means, and reacts in the physical world.

The important lesson is not only the finished thermostat controller. You learn how to separate input, decision logic, and output so a project stays debuggable instead of becoming a pile of wires and guesses.

Explain Like I'm 12

Think of the ESP32 as the brain. The DHT22 temperature sensor is how it notices the room. The relay module is how it commands a separate low-voltage fan. The code is the rule book that tells the brain what to do when the numbers change.

Safety Standards

  • Unplug USB before changing jumper wires. Recheck 3.3 V, 5 V, and GND before reconnecting power.
  • Do not power motors, pumps, LED strips, or servos from the ESP32 3.3 V pin. Use a suitable external supply and common ground.
  • Breadboards are for low-current prototypes. Move high-current or unattended builds to proper terminals, enclosure, strain relief, and fusing.
  • Relay and mains-voltage projects require isolation, correct relay ratings, enclosed wiring, and qualified adult supervision.

What You Will Build

A working thermostat controller with ESP32 DHT22 temperature input, hysteresis logic, a controlled low-voltage relay output, Serial Monitor diagnostics, and a wiring map you can expand into a more advanced version.

Learning Objectives

  • Wire and test the DHT22 temperature sensor on GPIO4 before connecting the relay circuit.
  • Map each signal to a named ESP32 GPIO and keep the code constants readable.
  • Control a relay module from GPIO26 using clear threshold and relay-polarity constants instead of hidden magic numbers.
  • Use Serial Monitor as an engineering tool, not only as a success message.
  • Identify power, wiring, and timing faults using a repeatable test checklist.

Components List

Bill of Materials

PartQtyEstimated CostNotes
ESP32 DevKit1$6-$10USB board
DHT22 module1$3-$6Temperature and humidity
Relay module1$2-$5Use low-voltage load only
OLED1 optional$3-$6I2C feedback

Wiring

Wire the DHT22 temperature sensor first, verify readings on GPIO4, then connect the relay module control input on GPIO26 for a separate low-voltage fan or indicator load.

Wiring Diagram ESP32 Smart Thermostat wiring diagram
  1. 1

    Unplug USB and keep the relay load disconnected.

  2. 2

    Connect a 3-pin DHT22 module VCC to 3.3 V, GND to GND, and DATA or OUT to GPIO4.

  3. 3

    If you use a bare 4-pin DHT22 sensor, add about a 10 k ohm pull-up resistor from DATA to 3.3 V and leave the NC pin disconnected.

  4. 4

    Connect relay VCC to 5 V if your relay module requires it, GND to ESP32 GND, and IN to GPIO26.

  5. 5

    Connect only a separate low-voltage test fan or indicator load to the relay contacts during learning.

  6. 6

    Power the ESP32 and watch Serial Monitor before connecting any moving fan.

GPIO Mapping

SignalESP32 PinDirectionNotes
DHT22 DATAGPIO4InputDigital sensor data
Relay INGPIO26OutputRelay module control input
OLED SDAGPIO21I2C dataOptional
OLED SCLGPIO22I2C clockOptional

Circuit Explanation

The DHT22 side is a low-current digital sensor input on GPIO4 using 3.3 V logic. Many 3-pin DHT22 modules already include the DATA pull-up resistor; a bare 4-pin DHT22 normally needs about a 10 k ohm pull-up from DATA to 3.3 V. The ESP32 drives the relay module control input on GPIO26. The relay contacts switch the separate low-voltage fan or indicator load, and the ESP32 must not power the fan directly from a GPIO.

Engineering Explanation

Why a thermostat needs hysteresis: a fan should not rapidly switch on and off when the room temperature hovers near one setpoint. This sketch uses FAN_ON_C = 27.0 and FAN_OFF_C = 25.5 so the relay has a quiet middle band.

Below 25.5 C the fan state becomes OFF. From 25.5 C to below 27.0 C the code keeps the previous fan state. At or above 27.0 C the fan state becomes ON. Those separate ON and OFF thresholds prevent relay chatter near the setpoint.

Place the DHT22 where it can measure room air, not local heat. Keep it away from the ESP32 regulator, relay coil/module heat, fan exhaust, heating elements, and direct sunlight. The DHT22 is a good learning thermostat sensor, but it is not a precision HVAC-grade sensor. Its response is relatively slow, so the current 2-second read interval is appropriate and you should not poll it excessively fast.

Code

Copy into Arduino IDE. Install any libraries noted in the component guides first.

smart-thermostat.ino
#include "DHT.h"

#define DHTPIN 4
#define DHTTYPE DHT22
#define RELAY_PIN 26

const int RELAY_ON = HIGH;   // Change to LOW for an active-LOW relay module.
const int RELAY_OFF = LOW;   // Change to HIGH for an active-LOW relay module.

const float FAN_ON_C = 27.0;
const float FAN_OFF_C = 25.5;
bool fanOn = false;
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(115200);
  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, RELAY_OFF);
  dht.begin();
  Serial.println("Smart thermostat ready");
}

void loop() {
  float tempC = dht.readTemperature();
  if (isnan(tempC)) {
    Serial.println("Temperature read failed");
    digitalWrite(RELAY_PIN, RELAY_OFF);
    delay(2000);
    return;
  }

  if (!fanOn && tempC >= FAN_ON_C) fanOn = true;
  if (fanOn && tempC <= FAN_OFF_C) fanOn = false;

  digitalWrite(RELAY_PIN, fanOn ? RELAY_ON : RELAY_OFF);
  Serial.print("Temp C: "); Serial.print(tempC, 1);
  Serial.print(" | Fan: "); Serial.println(fanOn ? "ON" : "OFF");
  delay(2000);
}

Code Explanation

The code keeps the fan state in a Boolean variable. Two thresholds create hysteresis: FAN_ON_C turns the output on at or above 27.0 C, and FAN_OFF_C turns it off at or below 25.5 C.

RELAY_ON and RELAY_OFF let you support active-HIGH or active-LOW relay modules by changing two constants instead of rewriting the control logic. Setup initializes the relay to RELAY_OFF, and a failed DHT22 read also leaves the relay in the safe OFF command state.

Expected Output

Below the lower threshold, Serial Monitor prints Fan: OFF. Warm the sensor above the ON threshold and the relay LED should switch on. Let the sensor cool below the OFF threshold and the relay turns off.

Build Photos

  • Breadboard overviewShow the ESP32, module placement, and power rails clearly.
  • Close-up wiringCapture each GPIO wire so beginners can compare their build.
  • Working outputShow the Serial Monitor, display, robot, pump, or lock state after the code runs.

Troubleshooting

  • DHT22 returns NaN or "Temperature read failed" Confirm 3.3 V, GND, DATA on GPIO4, the DHT library type is DHT22, and add about a 10 k ohm DATA-to-3.3 V pull-up if you are using a bare 4-pin sensor.
  • Temperature seems too high Move the DHT22 away from the ESP32 regulator, relay module heat, fan exhaust, heating element, and direct sunlight so it measures room air instead of local hot spots.
  • Relay LED works but the fan does not The ESP32 may be driving the relay input correctly while the separate low-voltage load circuit is not powered or not connected through the relay contacts.
  • Relay logic is reversed Some relay modules are active HIGH and some are active LOW. Swap RELAY_ON and RELAY_OFF in the constants instead of rewriting the thermostat logic.
  • Relay chatters near the threshold Keep the separate 27.0 C ON and 25.5 C OFF thresholds. If chatter remains, move the sensor away from the fan airflow and use a steadier low-voltage supply.
  • ESP32 resets when relay switches Use a suitable separate supply for the fan or load, keep relay/load wiring short, and make sure only signal ground is shared where the module requires it.
  • Fan stays ON or OFF unexpectedly Check the current temperature against FAN_ON_C and FAN_OFF_C, confirm GPIO26 reaches the relay IN pin, and verify the relay polarity constants match your module.

Common Mistakes

  • Driving a fan motor directly from an ESP32 GPIO.
  • Using mains voltage on a breadboard.
  • Forgetting hysteresis and making the relay chatter.
  • Placing the DHT22 beside the ESP32 regulator, relay coil, fan exhaust, heating element, or direct sunlight.
  • Using a relay module that needs more current than the USB port can provide.

Testing Checklist

  • ESP32 appears on the correct port and accepts a basic blink upload.
  • Ground is shared between every module that exchanges signals with the ESP32.
  • Each GPIO in the code matches the wire connected on the breadboard.
  • Serial Monitor prints startup text at 115200 baud.
  • The DHT22 temperature value changes slowly when you create a real test condition, with about 2 seconds between reads.
  • The relay module input on GPIO26 changes only when the expected hysteresis threshold is reached.
  • The fan turns ON at or above 27.0 C, stays in its previous state in the middle band, and turns OFF at or below 25.5 C.
  • The DHT22 is positioned away from ESP32, relay, fan, sunlight, and heater heat sources before judging room temperature accuracy.
  • The circuit still behaves correctly after power is removed and restored.

Upgrade Ideas

  • Add buttons to adjust setpoint.
  • Show temperature and fan state on OLED.
  • Add Wi-Fi dashboard control.
  • Log heating and cooling cycles.

Real-World Applications

  • Room fan controller
  • Greenhouse vent trigger
  • Small incubator demo
  • HVAC classroom model
  • Thermal control trainer

Downloads

  • ESP32 Smart Thermostat Arduino sketchUse the code section as the downloadable source until file downloads are published.
  • Wiring checklistMatch the GPIO table and wiring steps before powering the circuit.
  • Troubleshooting worksheetRecord symptoms, Serial output, voltage checks, and fixes.

FAQs

Why does my DHT22 thermostat reading look wrong?

Check 3.3 V power, ground, DATA on GPIO4, the pull-up resistor if using a bare 4-pin DHT22, and whether the sensor is near heat from the ESP32 regulator, relay module, fan exhaust, heater, or direct sunlight.

Why does the relay fan output not respond?

Check the ground connection first. Then confirm GPIO26, relay power, the separate low-voltage load circuit, and whether RELAY_ON and RELAY_OFF match your active-HIGH or active-LOW relay module.

Why does the thermostat use two temperatures instead of one?

The 27.0 C ON threshold and 25.5 C OFF threshold create hysteresis. The middle band keeps the previous fan state so the relay does not chatter near the setpoint.

Can I connect this to household HVAC wiring?

No. This tutorial is for a low-voltage fan or indicator prototype only. Do not connect mains or HVAC wiring to the breadboard circuit.

Review, Testing, and References

Author: Abdul Mubeen and the ESP32 Engine editorial team. Last updated: 2026-06-29. Reviewed: wiring logic, Arduino code structure, beginner safety, and learning sequence.

Educational level: Intermediate. Estimated completion time: 90-120 min. This project is for learning and prototyping; production or unattended hardware needs additional engineering review.

Project Complete!

You built a real thermostat controller and learned how to connect sensing, decision logic, and output control in one ESP32 project.

  • Wire and test a DHT22 temperature sensor
  • Control a relay module and separate low-voltage fan from ESP32
  • Debug hardware with Serial Monitor
  • Improve the project safely