May 2013 DocID18304 Rev 1 1/38
AN3322
Application note
Watt-hour meter based on the STM32F101 microcontroller
By Ludek Holoubek
Introduction
This document describes, in detail, the hardware and software implementation of a watt-
hour meter using the STM32F101 microcontroller. This cost effective watt-hour meter uses
shunt with an operational amplifier as a current sensor, an embedded 12-bit ADC for current
and voltage measurement, GPIO for LCD management, and a lot of other peripherals for
communication, tamper detection, keyboard, and power disconnection. Powerful
architecture of the STM32™ microcontroller allows sampling at 1 Msps. The high sampling
rate makes it possible to use methods for ADC resolution enhancement.
Main features
Metrological parts
Microcontroller and external op amp only
Compliant with EN 50470-1:2006 Class B, precision better than 1%
Starting current 20 mA, nominal current 5 A, maximum current 100 A
Reads the voltage and the current signal up to the 21
st
harmonic
Shunt as a current sensor. CT and Rogowski coil is optional
Additional parts included in reference design
LCD driven directly by microcontroller
Keyboard driven directly by microcontroller
An extra external EEPROM for user data
Bi-stable relay, IR communication, serial communication
Backup battery, complete power meter power management
www.st.com
Contents AN3322
2/38 DocID18304 Rev 1
Contents
1 Watt-hour meter using the STM32 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.1 STM32 ADC parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.2 Requirements for meter in Class B . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Definitions of the power meter parameters (according to EN 50407-1:2006) . . . . . 7
1.3 STM32 ADC dynamic measurement range . . . . . . . . . . . . . . . . . . . . . . . . 8
1.3.1 Voltage measurement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.3.2 There are two current ranges in the meter . . . . . . . . . . . . . . . . . . . . . . . 9
1.4 Error sources on the STM32 meter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.5 Linearity of STM32 ADC and its INL reduction . . . . . . . . . . . . . . . . . . . . . 10
1.6 Thermal noise present on STM32 meter platform . . . . . . . . . . . . . . . . . . 10
1.7 Crosstalk between subsequent multiplexed channels . . . . . . . . . . . . . . . .11
1.8 Power and energy measurement by STM32 embedded ADC . . . . . . . . . 13
1.9 ADC enhancement by oversampling technique . . . . . . . . . . . . . . . . . . . . 15
1.10 Output values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Some reference values and constants are defined . . . . . . . . . . . . . . . . . . . . . . . . 16
1.10.1 RMS voltage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
1.10.2 RMS current - high gained . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1.10.3 RMS current - low gained . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.10.4 Power . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.10.5 Energy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
1.10.6 Pulse output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
2 Computational issues and compensation algorithms . . . . . . . . . . . . . 22
2.1 Non-coherent sampling issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.2 Timer evaluation for coherent sampling triggering . . . . . . . . . . . . . . . . . . 24
2.3 Timer setting constraints for coherent sampling triggering . . . . . . . . . . . . 25
2.4 Precise timer setting for coherent sampling . . . . . . . . . . . . . . . . . . . . . . . 26
2.5 Frequency measurement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
2.6 Filtering of input signal for proper zero crossing detection . . . . . . . . . . . . 29