Project: a smart energy monitor
In short: Most electricity meters flash an LED for every fixed amount of energy used. This project counts those flashes with a light sensor and an ESP32, works out live power in watts and energy in kilowatt-hours, and publishes them to MQTT. It needs no contact with mains wiring, which makes it a safe first energy project.
What you will build and what you need
You will build a monitor that shows how much power your home or lab is using right now and how much energy it used each day. Seeing the numbers live makes it easy to find waste, such as a server rack that never sleeps or an old fridge.
| Part | Notes |
|---|---|
| ESP32 board | Any MicroPython-capable board |
| Light sensor module with digital output | A photodiode or LDR comparator module with an adjustable threshold |
| Black tape or a small housing | Holds the sensor over the LED and blocks room light |
| USB power near the meter | Or a battery if no socket is nearby |
| MQTT broker and dashboard | From the dashboard project |
Look on the meter face for the pulse rate, printed as something like 1000 imp/kWh. At 1000 impulses per kilowatt-hour, each flash means 1 watt-hour has been used.
Design and safety
Safety first. The pulse method touches nothing electrical: the sensor simply sits over the LED on the outside of the meter. Never open a meter or consumer unit, and do not break any seals; the meter belongs to your supplier. Current transformer (CT) clamps can give per-circuit readings, but fitting them means working near live conductors, so in many countries that should be done by a qualified electrician. If you only want to monitor single appliances, a smart plug with a local API is a safer option.
Build steps and code
- Wire the light sensor module to 3.3 V, ground and GPIO 27.
- Tape it firmly over the meter's pulse LED and cover it so room light cannot reach it.
- Adjust the module's threshold so its own indicator LED flashes in step with the meter LED. Check whether your module gives a falling or rising edge on each flash.
- Upload the code, turn on a known load such as a 2 kW kettle, and check the reading is close.
- Publish to MQTT and add a daily energy panel to your dashboard.
import time
from machine import Pin
IMP_PER_KWH = 1000 # printed on your meter
pulses = 0
last_ms = time.ticks_ms()
gap_ms = 0
def on_pulse(pin):
global pulses, last_ms, gap_ms
now = time.ticks_ms()
d = time.ticks_diff(now, last_ms)
if d > 50: # ignore electrical noise under 50 ms
gap_ms, last_ms = d, now
pulses += 1
sensor = Pin(27, Pin.IN)
sensor.irq(trigger=Pin.IRQ_FALLING, handler=on_pulse)
def power_w():
# each pulse is 1000/IMP_PER_KWH Wh; Wh per second x 3600 = W
return round(3_600_000_000 / (IMP_PER_KWH * gap_ms)) if gap_ms else 0
def energy_kwh():
return pulses / IMP_PER_KWH
while True:
time.sleep(30)
print(power_w(), "W", round(energy_kwh(), 3), "kWh") # publish via MQTT here
With a 1000 imp/kWh meter, a flash every second means 3600 W, and a flash every 36 seconds means 100 W. At very low loads the gap is long, so live power updates slowly; that is normal.
Security and privacy checklist
- Energy data is personal data: usage patterns reveal when you are home, asleep or away. Keep it on your own systems where possible.
- Read-only device: the monitor should have no ability to switch loads; add control only as a separate, well-protected project.
- Usual device hygiene: TLS to the broker, per-device credentials, an IoT VLAN and no debug services left on.
- Data integrity: alert if pulses stop for an unusually long time, which may mean the sensor has slipped.
- Respect the meter: no seals broken, nothing attached that could damage it, and no attempt to alter readings.
Practice
A meter is rated 1000 imp/kWh and the LED flashes every 2 seconds. What is the power?
Why is reading the pulse LED safer than fitting a CT clamp?
Why should energy data be treated as personal data?
Key takeaways
- Most meters flash an LED a fixed number of times per kWh.
- Count flashes for energy; time the gap between flashes for live power.
- The pulse method needs no contact with mains; leave CT clamps to qualified people.
- Calibrate with a known load such as a kettle.
- Energy usage is personal data: keep it local and secure the device.
Original content written by Mohamed Fayas for FM TecHub. © 2026 FM TecHub. You're welcome to share a link to this page; please don't copy the text elsewhere without permission.