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:

  1. CF (Power Frequency): Outputs a high-frequency pulse train whose frequency is directly proportional to active power (W).
  2. 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 the SEL (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 interfaceDual Pulse Frequency Outputs (CF, CF1) + Mode Select (SEL)
Active power signal (CF)High frequency (0 to 3.4 kHz0\text{ to }3.4\text{ kHz}) proportional to active power
Voltage/Current signal (CF1)Frequency proportional to VRMSV_{RMS} (when SEL is High) or IRMSI_{RMS} (when SEL is Low)
Measurement accuracyClass 0.5 (±0.5%\pm 0.5\%)
High-voltage sense inputsVPV_P (voltage divider, 43.7 mV\le 43.7\text{ mV}) & VIP/VINV_{IP}/V_{IN} (shunt resistor, 43.7 mV\le 43.7\text{ mV})
Operating current3.0 mA3.0\text{ mA} typical

Pinout (SOP-8 Package)

text
             ┌───┴───┐
          VDD ─┤ 1    8├─ GND
          V1P ─┤ 2    7├─ SEL
          V1N ─┤ 3    6├─ CF1
           V2P ─┤ 4    5├─ CF
             └───────┘
PinNameTypeDescription
1VDDPowerLogic power supply (+4.5 V to +5.5 V DC)
2V1PAnalog InputPositive current sense input (from 1 mΩ1\ \text{m}\Omega shunt)
3V1NAnalog InputNegative current sense input
4V2PAnalog InputVoltage sense input (from resistor divider; V2NV_{2N} connected to GND)
5CFDigital OutputActive power pulse frequency output pin
6CF1Digital OutputVoltage/Current pulse frequency output pin
7SELDigital InputSelect pin (HIGH = read Voltage on CF1, LOW = read Current on CF1)
8GNDPowerGround reference (0 V)

Specifications

ParameterSymbolMinTypMaxUnitConditions
Supply VoltageVDDV_{DD}4.55.05.5VDC
Supply CurrentIDDI_{DD}3.05.0mAActive operation
Current Channel Input RangeVV1PV1NV_{V1P-V1N}-43.7+43.7mV PeakDifferential shunt voltage
Voltage Channel Input RangeVV2PV_{V2P}-43.7+43.7mV PeakSingle-ended voltage
Active Power AccuracyEPE_P-0.5%±0.1%\pm 0.1\%+0.5%Dynamic range 1000:1
Max CF Output FrequencyfCF_maxf_{CF\_max}3.4kHzRated power load
Max CF1 Output FrequencyfCF1_maxf_{CF1\_max}890HzRated voltage/current load

Frequency Math & Time Periods

To read power, voltage, and current on a microcontroller:

  1. Active Power (W): Measure pulse period TCFT_{CF} on pin CF using interrupts:

P (W)=KPTCF(or P=fCF×KP)P\text{ (W)} = \frac{K_P}{T_{CF}} \quad \text{(or } P = f_{CF} \times K_P \text{)}

  1. Voltage (V): Set SEL High, wait 200 ms200\text{ ms} for frequency stabilization, measure TCF1T_{CF1}:

VRMS (V)=KVTCF1V_{RMS}\text{ (V)} = \frac{K_V}{T_{CF1}}

  1. Current (A): Set SEL Low, wait 200 ms200\text{ ms} for frequency stabilization, measure TCF1T_{CF1}:

IRMS (A)=KITCF1I_{RMS}\text{ (A)} = \frac{K_I}{T_{CF1}}

Wiring (Sonoff POW Internal Connections)

HLW8012 PinESP8266 GPIONotes
VDD5V (from internal LDO)Power rail
GNDGNDCommon ground (connected to Neutral AC)
CFGPIO 14 (Interrupt)Power pulse frequency
CF1GPIO 13 (Interrupt)Voltage/Current pulse frequency
SELGPIO 12 (Output)Mode Select pin
Warning

Lethal High Voltage Hazard:

  • HLW8012 GND is 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 SEL without waiting for frequency settling: After toggling SEL between High (Voltage) and Low (Current), wait at least 200 ms200\text{ ms} before measuring CF1 pulse period to allow internal frequency multipliers to settle.
  • Forgetting input pull-up / interrupt edge setup: CF and CF1 output square-wave pulses. Ensure hardware interrupts trigger on FALLING or RISING edges.

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