Free tech learning for everyone, from basics to pro

IoT ProjectsIoT SecurityIntermediate

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.

PartNotes
ESP32 boardAny MicroPython-capable board
Light sensor module with digital outputA photodiode or LDR comparator module with an adjustable threshold
Black tape or a small housingHolds the sensor over the LED and blocks room light
USB power near the meterOr a battery if no socket is nearby
MQTT broker and dashboardFrom 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

Two ways to measureMeter pulse LEDNo contact with mainsWhole-house totalVery accurate (meter grade)Needs a pulse LEDCT clampClips round one cablePer-circuit readingsNeeds calibrationKeep away from live parts
Figure 1. Counting the meter LED is the safest start; current transformer clamps add per-circuit detail.

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.

Energy monitor data pathPulse LEDflashes per WhLight sensordigital outputESP32counts, timesMQTTW and kWhDashboarddaily usage
Figure 2. Each flash of the meter LED is a fixed amount of energy; the gap between flashes gives live power.

Build steps and code

  1. Wire the light sensor module to 3.3 V, ground and GPIO 27.
  2. Tape it firmly over the meter's pulse LED and cover it so room light cannot reach it.
  3. 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.
  4. Upload the code, turn on a known load such as a 2 kW kettle, and check the reading is close.
  5. 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?
Each flash is 1 Wh. One watt-hour every 2 seconds is 0.5 Wh per second, times 3600, which is 1800 W.
Why is reading the pulse LED safer than fitting a CT clamp?
The light sensor sits outside the meter and touches no electrical parts, whereas fitting a CT clamp means working near live conductors.
Why should energy data be treated as personal data?
Patterns of use show when people are home, asleep or away, so it reveals private behaviour and could help a burglar.

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.
Tracks your learning on this device only. See your progress on the learning paths.

About the author

Mohamed Fayas

Founder of FM TecHub. CCNA certified, with a BTEC HND in Network Engineering, and BEng (Hons) in Computer Networking and Cloud Security. He writes practical, plain-English guides on networking, security and cloud.

More about the author

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.

Stay up to date by email

New notes, tools and security news from FM TecHub, explained simply. Free.

We only use your email to send these updates. See our privacy policy.