Log & Graph 24‑V Thermostat Events with an Optocoupler and Raspberry Pi
Many home‑automation projects involve building a thermostat from scratch, but what if you already own a programmable thermostat and simply want to record when it turns on or off? This guide explains how to use an optocoupler to safely isolate the 24‑V AC HVAC system from a Raspberry Pi, capture logic‑level signals, log events, and plot temperature data in real time.
⚠️ Working with mains‑level voltage can be hazardous. Only proceed if you understand electrical safety and have the necessary skills. The author cannot be held liable for damage to equipment, pets, or property.
Step 1: Gather Parts and Tools
Required components:
- HCPL3700 optocoupler (includes rectifier)
- 2 × 3.3 kΩ resistors, ½ W or higher
- 10 µF, ≥10 V electrolytic capacitor
- 0.1 µF ceramic capacitor
- 8 kΩ pull‑up resistor (or 3.3 kΩ + 4.7 kΩ in series)
- Raspberry Pi (any model with 3.3 V GPIO)
- Two‑conductor insulated wire, preferably with a quick‑disconnect connector
- Basic electronics supplies: breadboard, jumper wires, etc.
Optional temperature sensor add‑on:
- ADT7410 digital temperature sensor
- SOIC breakout board
- Additional 0.1 µF capacitor
- 4 × 10 kΩ resistors
Optional PCB design tools:
- Two‑terminal screw block
- Female header pins for the SOIC board
- Male header pins or female header pins for Pi/Arduino connectivity
- SMD versions of the resistors to reduce board size
Tools:
- Multimeter (AC voltage mode)
Requirements:
- 24 VAC HVAC system
- Python and related libraries (e.g.,
hipi-i2c) - Optional: Plotly account for online graphing
- Optional: Eagle (free version) for PCB design
Step 2: Identify the Thermostat’s Relay Wires
Most residential HVAC systems operate at 24 VAC. A programmable thermostat typically powers itself from a “power‑stealing” circuit and switches a relay to energize the furnace or air‑handler. To monitor the relay state, locate the pair of wires that carry the 24 VAC supply to the HVAC unit.
With a multimeter set to AC voltage, test each wire pair while the HVAC is both on and off. Record the voltage reading; the correct pair should show ~24 VAC (often a little higher, e.g., 28–30 VAC) when the system is active and 0 VAC when it is idle.
Step 3: Add Monitoring Leads
Shut off the HVAC power and confirm with your multimeter that no voltage is present. Carefully remove the thermostat from its mounting base to expose the wiring. Attach two additional wires to the chosen pair of relay conductors. Use quick‑disconnect connectors at the Pi end for easy maintenance.
Step 4: Build the Isolation Circuit
Directly connecting 24 VAC to a Raspberry Pi is impossible. An optocoupler such as the HCPL3700 provides electrical isolation while automatically rectifying the input voltage.
Key points in the schematic:
- Two 3.3 kΩ resistors (rated ≥½ W) on the AC input pins set the trigger threshold. Refer to the HCPL3700 datasheet for exact calculations.
- A 10 µF, ≥10 V electrolytic capacitor across the rectifier’s DC output smooths the signal and prevents the Pi’s GPIO from flickering at the line frequency.
- A 0.1 µF ceramic capacitor across the optocoupler’s supply pins reduces high‑frequency noise.
- Because the HCPL3700 uses an open‑collector output, a pull‑up resistor is required. An 8 kΩ resistor (or a 3.3 kΩ + 4.7 kΩ series) reliably pulls the line high for the Pi’s 3.3 V logic.
Connect the optocoupler’s output to a Pi GPIO pin (e.g., GPIO23). Use the Pi’s 3.3 V rail to power the optocoupler’s supply pins. Ground the Pi and the optocoupler together.
Once wired, the Pi can detect a logic low whenever the HVAC relay is energized and a logic high when it is de‑energized.
For complete schematics and component calculations, see the HCPL3700 application note.
Step 5: Install Software and Log Data
Use Python to read the GPIO pin, timestamp each state change, and write the data to a CSV file or a database. A simple script might look like this:
import RPi.GPIO as GPIO
import time
import csv
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
with open('thermostat_log.csv', 'a') as f:
writer = csv.writer(f)
while True:
state = GPIO.input(23)
timestamp = time.time()
writer.writerow([timestamp, state])
time.sleep(0.5)
For real‑time graphing, upload the CSV to Plotly or use a lightweight library like Matplotlib to generate local charts.
Step 6: Expand with a Temperature Sensor (Optional)
Integrate the ADT7410 over I²C to log ambient temperature alongside relay status. Connect the sensor to the Pi’s SDA/SCL pins, set the address (0x48 by default), and read the 16‑bit temperature register. Store the readings in the same log file with a separate column.
When you have the temperature data, use Plotly to create a dual‑axis chart: HVAC on the primary axis and temperature on the secondary axis, enabling you to correlate thermostat activity with ambient changes.
By following these steps, you can safely monitor and log your existing thermostat’s behavior without modifying the device itself.
Manufacturing process
- Log & Graph 24‑V Thermostat Events with an Optocoupler and Raspberry Pi
- Build a Remote Temperature Sensor with Raspberry Pi and Python – Step‑by‑Step Guide
- Remote Weather Monitoring with Raspberry Pi 3 and PubNub
- Raspberry Pi Photocell Logging & Alert System – Build a Low‑Power Light Sensor with Python
- Control an LED with a PIR Motion Sensor on Raspberry Pi
- Raspberry Pi–Based Bathroom Occupancy Sensor with Voice & SMS Alerts via Twilio
- Integrating Microsoft Kinect with Raspberry Pi for Real‑Time Human Detection on the Sonbi Robot
- Build a Raspberry Pi 3 & Arduino Laptop: Step‑by‑Step Guide
- Smart Home Automation Powered by Raspberry Pi 2 & Windows 10 IoT
- 10 RoboDK & Raspberry Pi Projects for Innovative Robotics