Why Build Your Own Smart Thermostat?
Commercial smart thermostats like Nest and Ecobee cost $200–$300 and phone home to company servers. They profile your habits, harvest usage data, and lock you into cloud fees. A DIY unit built on open-source software costs under sixty dollars, learns your schedule with on-device AI, and gives you absolute control of your data. The bill of materials:
- Raspberry Pi Zero 2 W – $15
- 1-Wire DS18B20 waterproof temperature probe – $4
- CJMCU-811 CCS811 CO₂ + TVOC sensor – $8
- KY-019 4-channel relay module – $6
- 24 V AC HVAC transformer – $10
- Breadboard, jumpers, enclosure – $10
- Micro-SD card (≥16 GB) – $8
Total: ~$61. Hardly any soldering is required, and the entire stack fits inside a standard thermostat wall-box.
Before You Touch the Wires
Turn off power at the breaker panel. HVAC control lines carry 24 V AC, low but still dangerous in a confined metal box. Label the existing wires (R, G, Y, W, C) with masking tape before removing the old thermostat.
Wire Up the Hardware
- Power: Connect the 24 V transformer to R (24 V hot) and C (return). This powers the relay board and charges the Pi through its GPIO 5 V pin, eliminating wall-warts.
- Relay Outputs:
- Relay 1 – Y (cooling)
- Relay 2 – W (heating)
- Relay 3 – G (fan)
- Sensors: Plug DS18B20 into 1-Wire via GPIO 4 (data), 3.3 V (power), and GND. Connect CCS811 (I²C SDA/SCL) for air-quality–driven ventilation scheduling.
- Indicator LEDs: Optional status RGB LED on GPIO 18, 19, 20 shows heat/cold demand wirelessly across the room.
Using a screw-down terminal block keeps everything secure and serviceable. Take photos as you work; they save hours of troubleshooting later.
Flashing the SD Card
Download Raspberry Pi OS Lite (64-bit). Enable SSH by writing an empty file named ssh
to the boot partition before first boot. Create a wpa_supplicant.conf
with your Wi-Fi credentials; the Pi will connect automatically.
From GPIO to Thermostat Logic
We’ll run Home Assistant Core on the Pi. It takes < 2 GB of RAM yet packs full HASS hardware automation. Install with the official script (curl -sSL https://get.home-assistant.io
).
Add sensors to configuration.yaml
:
sensor: - platform: 1wire names: 28-xxxxxxxxxxxx: living_room_temp - platform: template sensors: ccs811_co2: value_template: "{{ states('sensor.ccs811_co2') }}" unit_of_measurement: "ppm"
The DS18B20 driver loads automatically; CCS811 needs the adafruit-circuitpython-ccs811
library via pip.
Teaching the AI Your Routine
We attach a lightweight neural net (tinyML PID model) that runs entirely on the Pi Zero. It treats indoor temperature, humidity, and occupancy as inputs and outputs heating/cooling set-points every 30 seconds. Training is fully local; it begins with a conservative schedule and refines itself daily.
- Capture 7 days of raw data (
/config/history
). - Use
hass-exporter
to dump adataset.json
with minute-granularity readings. - Train the model with TensorFlow Lite’s microcontroller workflow on your laptop (takes 60 seconds).
- Copy the
.tflite
file back to the Pi at/config/models/
.
Total turnaround: 15 min per week, entirely optional. Skipping the training keeps the thermostat in classic programmable mode but still benefitting from web-based control.
Creating a Hass-Free Construction Model
Under Home Assistant, add a Generic Thermostat card:
climate: - platform: generic_thermostat name: DIY Smart Thermostat heater: switch.relay_heat cooler: switch.relay_cool fan: switch.relay_fan target_sensor: sensor.living_room_temp min_temp: 10 max_temp: 30
You now have a conventional web interface and Google Home / Alexa integration via the default Home Assistant cloud (optional), all without nanoseconds of external telemetry.
Ethernet Backup for Wi-Fi Fails
Add a PoE Splitter ($12) to supply both network and power in one cable. If Wi-Fi glitches during a firmware update, you can still SSH in from the unmanaged switch in your wiring closet.
Air-Quality Aware Ventilation
Pump fresh air only when CO₂ > 1000 ppm. Create an automation:
automation: - id: co2_fan_boost trigger: platform: numeric_state entity_id: sensor.ccs811_co2 above: 1000 action: - service: switch.turn_on entity_id: switch.relay_fan
Average energy saved: 8 % according to a 2023 Lawrence Berkeley National Laboratory field study—without breaking privacy.
Voice & Away Mode with In-House AI
Install Rhasspy 3, a fully off-line voice assistant. A cheap USB microphone captures commands. Custom sentences file sentences.ini
:
[SetTemperature] temperature = 20..26 set (living room|) temperature to ( [the] temperature)
Rhasspy returns JSON to Home Assistant; the thermostat adjusts instantly. When your phone’s MAC address leaves Wi-Fi (no cloud ping required), presence detection kicks the system into Eco mode (heat to 63 °F / 17 °C, cooling to 80 °F / 27 °C).
Failsafe & Freezing Protection
Raspberry Pi boot failures should not paralyze heating. A hardware watchdog relay (TC358748) cuts in after 10 min without 3 V3 logic and restores basic faux-stat operation: ALWAYS on heat below 50 °F (10 °C). This part costs $5 and ties to the same enclosure.
Controlling From Anywhere While Staying Private
We avoid opening ports. Home Assistant’s WireGuard add-on gives a single encrypted UDP tunnel that every phone and laptop can use. Configure a split-DNS so you never have to remember IP addresses. Total setup time, five minutes per client.
Hardware Calibration Tips
- Place DS18B20 in the return-air path, not on an exterior wall.
- Offset CCS811 CO₂ readings by −50 ppm based on fresh-air streaming test.
- Wrap all relay terminals with heat-shrink to quiet emi clicks from nearby Wi-Fi access point.
- Add a 10 k resistor between DS18B20 data line and 3.3 V (required for accurate communication).
Power Budget Analysis
The Pi Zero 2 W idles at 110 mA @ 5 V (0.55 W), relay coils add 250 mA per active relay, and the average total draw across a 24-hour cycle is 2.1 W. Over a year that’s about 18 kWh, or $2.70 at U.S. average rates—one-tenth the idle load of a Nest.
Extra Features for the Curious
- Window Open Alert: Sharp temperature drop + CO₂ drop triggers a push notice.
- Vacation Mode Scheduler: one YAML file enables email report of runtimes; integrate with your email filter to shut HVAC off automatically when IFTTT vacation calendar event ends.
- DS18B20 Spike Detector: ignores sensor glitches (>3 °C in a minute) with a simple median filter in Node-RED.
- In-home Extremes Diary: log all heat-pump starts for you to forward to HVAC service as evidence of efficiency loss after a decade.
Budget vs. Payback
With conservative 10 % heating/cooling savings on a typical 2,000 ft² U.S. house, the unit pays for itself in one heating season. The experience level required: the same as wiring up a ceiling fan.
When Things Go Wrong
- “No DS18B20 device found” — Check pull-up resistor and connect Pin 4 to 3.3 V (not 5 V!).
- Relay stuck on — Swap a spare 4-channel module; they are ubiquitous.
- Overshoot/undershoot 1 °F events — Change
kp: 2.0, ki: 0.05, kd: 1.0 in the tinyML model JSON.
- Corrupt SD after 6 months — Over-provision 1 GB, set
fsck.mode=force
in/boot/cmdline.txt
.
Safety Checklist
Before ac power-up:
- Continuity test every relay line with a multimeter.
- Verify correct thermostat wire colors at both ends (sensor and HVAC board).
- Secure the enclosure to the wall box; USB-C power plug strain can pull wires loose over months.
Parting Advice
A DIY thermostat is as much about philosophy as temperature control. The circuit teaches you HVAC wiring, the tinyML model demystifies machine learning, and the local networking setup gives you lifelong internet privacy. Bridge those three disciplines and you’ll never look at commercial thermostats again—except for spare parts.