Overview
The HLW8012 is a low-cost single-phase electricity metering IC manufactured by HLW (Zhongshan Belling). Most famously embedded inside the original Sonoff POW Wi-Fi smart switch, it measures active power (W), RMS voltage (V), and RMS current (A).
Rather than using serial UART data packets, the HLW8012 outputs dual 50% duty-cycle frequency pulse trains:
CF(Power Frequency): Outputs a high-frequency pulse train whose frequency is directly proportional to active power (W).CF1(Voltage/Current Frequency): Outputs a pulse train whose frequency is proportional to either RMS voltage or RMS current, determined by the logic level on theSEL(Select) pin.
It is natively supported in ESPHome, Tasmota, and the Arduino HLW8012 library.
Quick reference
Operating voltage (VDD) | 4.5 V to 5.5 V DC (5.0 V nominal) |
| Output interface | Dual Pulse Frequency Outputs (CF, CF1) + Mode Select (SEL) |
Active power signal (CF) | High frequency () proportional to active power |
Voltage/Current signal (CF1) | Frequency proportional to (when SEL is High) or (when SEL is Low) |
| Measurement accuracy | Class 0.5 () |
| High-voltage sense inputs | (voltage divider, ) & (shunt resistor, ) |
| Operating current | typical |
Pinout (SOP-8 Package)
text
┌───┴───┐
VDD ─┤ 1 8├─ GND
V1P ─┤ 2 7├─ SEL
V1N ─┤ 3 6├─ CF1
V2P ─┤ 4 5├─ CF
└───────┘
| Pin | Name | Type | Description |
|---|---|---|---|
| 1 | VDD | Power | Logic power supply (+4.5 V to +5.5 V DC) |
| 2 | V1P | Analog Input | Positive current sense input (from shunt) |
| 3 | V1N | Analog Input | Negative current sense input |
| 4 | V2P | Analog Input | Voltage sense input (from resistor divider; connected to GND) |
| 5 | CF | Digital Output | Active power pulse frequency output pin |
| 6 | CF1 | Digital Output | Voltage/Current pulse frequency output pin |
| 7 | SEL | Digital Input | Select pin (HIGH = read Voltage on CF1, LOW = read Current on CF1) |
| 8 | GND | Power | Ground reference (0 V) |
Specifications
| Parameter | Symbol | Min | Typ | Max | Unit | Conditions |
|---|---|---|---|---|---|---|
| Supply Voltage | 4.5 | 5.0 | 5.5 | V | DC | |
| Supply Current | — | 3.0 | 5.0 | mA | Active operation | |
| Current Channel Input Range | -43.7 | — | +43.7 | mV Peak | Differential shunt voltage | |
| Voltage Channel Input Range | -43.7 | — | +43.7 | mV Peak | Single-ended voltage | |
| Active Power Accuracy | -0.5% | +0.5% | — | Dynamic range 1000:1 | ||
Max CF Output Frequency | — | 3.4 | — | kHz | Rated power load | |
Max CF1 Output Frequency | — | 890 | — | Hz | Rated voltage/current load |
Frequency Math & Time Periods
To read power, voltage, and current on a microcontroller:
- Active Power (W): Measure pulse period on pin
CFusing interrupts:
- Voltage (V): Set
SELHigh, wait for frequency stabilization, measure :
- Current (A): Set
SELLow, wait for frequency stabilization, measure :
Wiring (Sonoff POW Internal Connections)
| HLW8012 Pin | → | ESP8266 GPIO | Notes |
|---|---|---|---|
VDD | 5V (from internal LDO) | Power rail | |
GND | GND | Common ground (connected to Neutral AC) | |
CF | GPIO 14 (Interrupt) | Power pulse frequency | |
CF1 | GPIO 13 (Interrupt) | Voltage/Current pulse frequency | |
SEL | GPIO 12 (Output) | Mode Select pin |
Warning
Lethal High Voltage Hazard:
- HLW8012
GNDis tied directly to AC Neutral. Connecting an un-isolated FTDI serial adapter or logic analyzer to a powered Sonoff POW will destroy connected computer equipment and create a lethal electric shock hazard. Always flash/debug Sonoff POW boards while disconnected from AC mains power.
Example (ESPHome Configuration)
yaml
sensor:
- platform: hlw8012
sel_pin: GPIO12
cf_pin: GPIO14
cf1_pin: GPIO13
current_resistor: 0.001 # 1 mOhm shunt
voltage_divider: 2400 # 2.4 MOhm / 1 kOhm divider
voltage:
name: "Sonoff POW Voltage"
current:
name: "Sonoff POW Current"
power:
name: "Sonoff POW Power"
Common mistakes
- Switching
SELwithout waiting for frequency settling: After togglingSELbetween High (Voltage) and Low (Current), wait at least before measuringCF1pulse period to allow internal frequency multipliers to settle. - Forgetting input pull-up / interrupt edge setup:
CFandCF1output square-wave pulses. Ensure hardware interrupts trigger onFALLINGorRISINGedges.
Notes
- HLW8012 vs CSE7766: Modern Sonoff devices (Sonoff POW R2 and Sonoff S31) replaced the HLW8012 with the CSE7766 UART power chip, which eliminates interrupt timing overhead on the host MCU.
Assets & downloads
- datasheetdatasheet
- product-pagereference