Multi-purpose system for measuring electrical power supplied by electric sockets
Peter Babič
Department of Theoretical and Industrial Electrical Engineering (DTIEE)
babicpet@gmail.com

Abstract
 The paper shows the process of designing, building and programming of an inter-connected electronic system. It starts with explaining the fundamentals of the physics underlining the electronic power measurement process. The main part includes diagrams describing the inner working of the hardware, and later software running on it. The manufactured device is capable of measuring the electric power provided by the electric socket to the appliance and send the measured values over Wi-Fi to the cloud, to be visualised on a custom web server employing a charting library to plot the measured quantities over time. 
[Sorry. Ignored \begin{IEEEkeywords} ... \end{IEEEkeywords}]
1  Introduction
The idea is to invent way of measuring the electrical power and some more related information, preferably in a non-invasive way. The non-invasive way means, that the appliance that is being measured does not require any modifications, for instance in a form of some probe or a man-in-the-middle plug, suggesting an embedded system. When the data are obtained, they are presented to the user, preferably plotted as a quantity over time, not just and actual measurement. Since the solution is going to be multi-purpose, it has to incorporate at least one additional function, than just the measurement. In this case it is going to be the remote power-on/power-off of the appliance. The name of the thesis also suggests, that the final solution has to be compatible with the electrical sockets used in the local region, in this case the European ones. Since the solution is going to be an embedded system measuring a physical quantity, these two topics are described in following chapters.
2  Electric power fundamentals
In general physics terms, power is defined as the rate at which energy is transferred (or transformed). Electric energy in particular, begins as electric potential energy – what we commonly refer to as voltage. When electrons flow through that potential energy, it turns into electric energy. In most useful circuits, that electric energy transforms into some other form of energy. Electric power is measured by combining both how much electric energy is transferred, and how fast that transfer happens.
The electric power P is equal to the energy consumption E divided by the consumption time t [meade2002foundations] 
	
where P is the electric power in watt [W], E is the energy consumption in joule [J] and t is the time in seconds [s].
Electrical Power, in a circuit is the amount of energy that is absorbed or produced within the circuit. A source of energy such as a voltage will produce or deliver power while the connected load absorbs it. Light bulbs and heaters for example, absorb electrical power and convert it into heat or light. The higher their value or rating in watts the more power they will consume.
2.1  Ohm’s law
Ohm’s Law deals with the relationship between the voltage and the current in an ideal conductor. This relationship states that: the potential difference (voltage) across an ideal conductor is proportional to the current through it [henry2008ohm]. The constant of proportionality is called the resistance. 
	
where I is the current expressed in amperes [A], U is the voltage, bearing the volt units [V] and R is the electrical resistance in ohms [].
The Ohms’s law can be further expanded [beaty1998electric], to get these three quantities in relationship with power, such as 
	
2.2  Direct current (DC) circuits
Generally, Ohm’s law is used on dc circuits, containing a current of fixed magnitude (amplitude) and a definite direction associated with it. dc is produced by power supplies, batteries, dynamos and solar cells to name a few.
We also know that dc power supplies do not change their value with regards to time[herman2012direct], they are a constant value flowing in a continuous steady state direction. In other words, dc maintains the same value for all times and a constant uni-directional DC supply never changes or becomes negative unless its connections are physically reversed.
2.3  Waveforms and alternating current (AC) circuits
An alternating function or ac waveform on the other hand is defined as one that varies in both magnitude and direction in more or less even manner with respect to time making it a “bi-directional” waveform [whitaker2006ac]. An AC function can represent either a power source or a signal source with the shape of an AC waveform generally following that of a mathematical sinusoid as defined by 

 
Figure 1: The common types of waveforms visualised as a function of amplitude
The term AC or to give it its full description of Alternating Current, generally refers to a time-varying waveform with the most common of all being called a Sinusoid better known as a Sinusoidal waveform. Sinusoidal waveforms are more generally called by their short description as Sine Waves. Sine waves are by far one of the most important types of AC waveform used in electrical engineering.
This means then that the ac waveform is a “time-dependent signal” with the most common type of time-dependant signal being that of the Periodic Waveform. The periodic or ac waveform is the resulting product of a rotating electrical generator. Generally, the shape of any periodic waveform can be generated using a fundamental frequency and superimposing it with harmonic signals of varying frequencies and amplitudes but that is out of the waveform fundamentals theory.
Alternating voltages and currents can not be stored in batteries or cells like dc can, it is much easier and cheaper to generate these quantities using alternators or waveform generators when they are needed. The type and shape of an AC waveform depends upon the generator or device producing them, but all ac waveforms consist of a zero voltage line that divides the waveform into two symmetrical halves. The main characteristics of an ac waveform [nicolaides1996electrical] are defined as:
•	Period (T) is the length of time in seconds that the waveform takes to repeat itself from start to finish. This can also be called the Periodic Time of the waveform for sine waves, or the Pulse Width for square waves 
•	Frequency is the number of times the waveform repeats itself within a one second time period. Frequency is the reciprocal of the time period, defined as , with the unit of frequency being the Hertz [Hz] 
•	Amplitude is the magnitude or intensity of the signal waveform 
2.4  Power in AC circuits
When a reactance (either inductive or capacitive) is present in an ac circuit, the Ohm’s law formula does not apply and different approach must be taken to express and calculate power [rawlins2000basic].
Real power (or true power) is the power that is used to do the work on the load: 
	
where P is the real power in watts,  is the rms voltage, defined as  in volts,  is the RMS current, defined as  in amperes and ϕ is the impedance phase angle - phase difference between voltage and current.
Reactive power on the other hand, is the power that is wasted and not used to do work on the load. Curiously, it is defined as 
	
with Q being the reactive power in volt-ampere-reactive [var].
Apparent power is the power that is supplied to the circuit. Definition: 
	
where the unit of apparent power S is volt-ampere [VA]. It can be seen that it is not phase-angle dependent.
The relation all these three quantities are in is defined as 
	
however, again, nothing in the real world is perfect, and this relation only applies for a perfectly sinusoidal waveforms!
2.5  Phasor and phase shift
A phasor[2009electrical] is a constant complex number representing the complex amplitude (magnitude and phase) of a sinusoidal function of time. It is usually expressed in exponential form. Phasors are used in engineering to simplify computations involving sinusoids, where they can often reduce a differential equation problem to an algebraic one. The origin of the word phasor comes from phase + vector.
Phasor is a vector that represents a sinusoidally varying quantity, as a current or voltage, by means of a line rotating about a point in a plane, the magnitude of the quantity being proportional to the length of the line and the phase of the quantity being equal to the angle between the line and a reference line.

Figure 2: The phase difference between voltage (blue) and current (red), the origin of phase difference of angle ϕ
Considering the figure 2, the voltage waveform above starts at zero along the horizontal reference axis, but at that same instant of time the current waveform is still negative in value and does not cross this reference axis until 30°later. Then there exists a Phase difference between the two waveforms as the current cross the horizontal reference axis reaching its maximum peak and zero values after the voltage waveform.
As the two waveforms are no longer in-phase, they must therefore be out-of-phase by an amount determined by phi, ϕ. The waveform of the current can also be said to be lagging behind the voltage waveform by the phase angle ϕ. This angle represents the phase shift (also called phase difference) between two sinusoids [maxfield2011electrical].
2.6  Power factor and power factor correction
The power factor is just a specific name for a phase shift between the sinusoids of a current and voltage. So the figure 2 in fact shows the power factor. However, it is not expressed in a plane angle, but rather as a dimensionless number between -1 and 1.
The power factor is defined as , as a ratio of the real power over the apparent power[dixit2010electrical]. If ϕ is the phase angle between the current and voltage, then the power factor is equal to the cosine of the angle, cos ϕ.
If the power factor is 1, it means that all the supplied power is completely consumed by purely resistive load. A positive power factor that is lower than 1 indicates that some power is not consumed by the load and is returned back. The lower the factor, the more power is returned. When power factor is equal to 0, the energy flow is entirely reactive, and stored energy in the load returns to the source on each cycle. A negative power factor means that the device, considered to be power load is in fact a power source (produces more power than consumes).
How can this information be useful? Every load with a power factor other than 1 returns some power back to the transmission line. Since the transmission lines does have some resistance, this returned power translates to some wasted power in a form of heat. Energetic companies want to minimise the power wasted in the transmission lines to increase their profit, so numerous laws are coming into effect to correct [singh2008electric] (increase) the power factor.
2.7  Power measuring integrated circuits
Although it is possible to construct a circuit out of discrete components that would measure [webster2003electrical] the required physical quantities, and such a solution would probably be the cheapest solution out there, it would be highly impractical due to multiple reasons.
The most importantly, the obtained accuracy of the measurements would be dependent on the implementation and used components. It is safe to assume, that without multiple design iterations, the accuracy may be too low to be used in practice.
Another point is that, there is no definitive guide, ready to follow, about how to design such circuit. The reason of this is the vast amount of components available on the market and a lot of design considerations to take into account, depending on the requirements.
A special purpose ic are being developed for the exact purpose of measuring the real, apparent and reactive power, the power factor, and in most cases, gathering some other relevant information.

Figure 3: The simplified block diagram for a power measurement ic
From the block diagram 3, it can be seen that the power measuring ic is just a specialised microcontroller. It takes the data from the sensing circuitry, which in case of voltage can be measured directly, provided that the galvanic isolation is included, for the sake of safety. The current however, must be measured indirectly. There are three common ways [srinivasan2015composite] of doing so:
1.	shunt resistor - a resistor with a very small but precise value, that causes a voltage drop with a current passing through it due to the Ohm’s law, regardless of frequency. The actual voltage drop is so small, that it can be assumed insignificant, but measurable. However, the voltage drop is still present and may cause some issues, if not taken into account. The advantage is really low price. External galvanic isolation must be provided. 
2.	current transformer - a current passing wire inside a current sensing coil. Since it is a magnetic induction based transformer, the galvanic isolation is naturally present. The disadvantage is, that the transformer has a cut-off frequency, below which it’s effect diminishes rapidly. External magnetic fields can cause problems too. Suitable for measuring current of a fixed (or non-decreasing) frequency. 
3.	Hall-effect sensor - a sensor measuring absolute electromagnetic field in a conductor. In contrast to the current transformer, this sensor is able to measure low frequency currents, down to dc, which is a feat that the shunt resistor possesses too. Can be placed anywhere near the current path and doesn’t require physical connection, thus providing galvanic isolation too. The price increases with operating currents range and precision. Prone to be disturbed by external magnetic fields, too. 
Using dedicated power measuring IC has another advantage apart from being more accurate. In fact, the part datasheet can be consulted and if all application notes and advices are abided, the specified accuracy can be guaranteed.
3  Embedded system
An embedded system is some combination of computer hw and sw, either fixed in capability or programmable, that is specifically designed for a particular function [ganssle2008embedded]. Industrial machines, automobiles, medical equipment, cameras, household appliances, airplanes, vending machines and toys (as well as the more obvious cellular phone and pda) are among the myriad possible hosts of an embedded system. Embedded system that are programmable are provided with programming interface, and embedded system programming is a specialized occupation.
3.1  Processing units
The term embedded system is quite broad, so there is no surprise that the spectrum of used processing units is also wide. Since the general purpose microprocessors require external components, namely memories and peripheral, they tend to consume extra power and a board space. Since the design limitations of an embedded system are most of the time low physical size, low power consumption and/or long uptime and ruggedness (more components mean more parts could fail), microprocessors are seldom used. However, most of the commonly used architectures and word lengths are covered. Due to aforementioned reasons, microcontrollers are favored over microprocessors. 
3.2  ESP8266 Wi-Fi module
The ESP8266 Wi-Fi module is a self contained soc with integrated tcpip protocol stack that can give any microcontroller access to your Wi-Fi network. The ESP8266 is capable of either hosting an application or offloading all Wi-Fi networking functions from another application processor. The ESP8266 module is an extremely cost effective solution, with a huge code-base and community, making it a preferable option for many modern projects, mainly the ones that follow the iot trend.

Figure 4: The certified ESP-12E module exposing all gpio
This module has a powerful enough on-board processing and storage capability that allows it to be integrated with the sensors and other application specific devices through its gpio with minimal development up-front and minimal loading during runtime. Its high degree of on-chip integration allows for minimal external circuitry, including the front-end module, is designed to occupy minimal pcb area. 
4  Hardware components breakdown
The device under test will be referred to as appliance and the produced device will be referred to as client node (displayed as a simplified schematic in figure 6) is already integrated as an ESP-8266 module, described in more detail in the chapter @x III-B. The module contains the tcpip stack, micro-controller (application processor) running the user program, wlan and light indication, all in one piece, so this greatly simplifies the design process and allows for more focus on the actual measurement circuitry. The ESP-12E has been chosen as an actual module, because of the available certification[online\s\do5(2)ADUIESP\s\do5(1)2], which allows it to be introduced to the market later. It was already shown in the figure 4. The pwm is present there too, so sound indication requires just a sound emitting device.

Figure 5: The proposed block diagram of a client node
Talking about the measurement circuitry 6, the viable candidate is MAX78615 [online\s\do5(M)AX78615] with the companion ic MAX78700 [online\s\do5(M)AX78700]. The couple should be used, because it provides multiple ways of same voltage level communication with the processor, galvanic isolation via the pulse transformer for improved circuitry protection, great precision, accuracy and utility. The shunt resistor is utilised as a way of obtaining measurements, described in the sub-chapter @x II-G.
For the protection against fire a standard glass fuse or a resettable ptc fuse[wright2004electric] should be used. Because of the variable nature of most used devices, it is hard to calculate the current consumption of the circuit. It can be measured after the first iteration is manufactured. Thus, the easily replaceable standard glass fuse has been chosen because of its versatility. The circuit protection against high voltage should be solved with an isolated DC-to-DC converter[carr1996linear] or with a linear transformer coupled with a linear voltage regulator[2008linear]. Since the former one is either expensive or hard to design, and this work does not want to focus on more complexities, the latter option has been chosen.
Choosing the voltage level for the digital electronics (the output voltage of the linear regulator) is straightforward. Since the ESP-12E works on nominal 3.3V, this is the level that has been chosen. Having ic using the same voltage level removes the need to level-shift the communication between them, thus increasing the simplicity of the design.

Figure 6: Greatly simplified schematic of a client node sketching the inner working
Talking about the measurement circuitry, the candidate is MAX78615 [online\s\do5(M)AX78615], working on nominal 3.3V level, along with the companion ic MAX78700 [online\s\do5(M)AX78700]. The couple has been chosen, because it provides multiple ways of communication with the processor (buses/serial interfaces), galvanic isolation via a pulse transformer for improved circuitry protection, great precision, accuracy and utility. The resistor network, including the shunt resistor is utilised as a way of obtaining measurements. The shunt resistor is also briefly described in the sub-chapter @x II-G.
The remaining part of the client node’s block diagram 5 not yet mentioned is switching. Either a mechanical relay or a semiconductor device, such as a thyristor or a ssr isolated by an opto-coupler[trzynadlowski2015introduction] will do. Mechanical relays tend to be larger and produce sound noise, have slow response time, but have inbuilt separate isolation and are capable of switching higher currents without additional thermal issues than their semiconductor counterparts[blume2008electric]. The disadvantages of the mechanical relay are not relevant here, thus it has been chosen.

Figure 7: The top layer of the designed pcb (client node), exposing mainy tht components

Figure 8: The bottom layer of the designed pcb (client node), exposing mainly smt components
5  Conclusion
The manufactured client node has been inserted into the enclosure[online\s\do5(e)nclosure], portrayed in the figure 9, containing an European mains socket (female) on one side and an European mains plug (male) on the other side, forming a man-in-the-middle adaptor, that can be non-invasively put between wall socket and an appliance. The result can be observed in figure 10.
The client node is capable of measuring RMS voltage and RMS current. By multiplying them together, the apparent power can be obtained, as discussed back in the sub-chapter @x II-C. The intentions are to fix the design, enabling full range of physical quantities, discussed throughout the chapter II, to be measured. The plan is to use multiple client nodes to measure and track power consumption of the inactive, but plugged-in smps chargers, in a form of a collaborative global experiment.

Figure 9: The 3D visualisation of the enclosure, displaying both the plug and the socket

Figure 10: The view into the client node’s enclosure, before the final assembly, exposing top side of the board containing linear transformer T1 (green), mains connectors J1 and J2 (blue), a fuse holder for F1 (yellow-ish), a relay K1 (white) and an ESP-12E module

Acknowledgment
I would like to express my sincere thanks to my supervisor Ing. Tibor Vince, PhD., for his constant and constructive guidance throughout all the struggles that have occurred during this work. And to all other who gave a helping hand, I say thank you very much, too.