DIY ESP32 AC Power Meter (with Home Assistant/Automation Integration)

As you probably know we use electric power
pretty much all the time during our daily life. We use it to create light, cook a meal, print
documents, watch TV and so on. That is why it is no wonder that the average
electrical energy consumption is pretty high nowadays. I have the goal of lowering mine though and
what better way to do so than by being constantly exposed to my power consumption which will
hopefully remind me that having one light one is enough. So in this video I will show you how to use
the ESP32 and some complementary components in order to create a proper power meter that
can spit out pretty much all important power values and at the end I will then show you
how to integrate this meter into my home assistant system which I presented you in one of my
previous videos.

This way I can see my power consumption at
all times on my smartphone which is pretty handy and shocking at the same time. And with that being said let’s get started! This video is sponsored by JLCPCB, who currently
offer a variety of promotions. Not only can you get 4 Layer PCBs for a price
of just 2$ but you can also visit their virtual online exhibition at which you have a 100%
chance of winning prices. So have a look for yourself. First off how can we use a microcontroller
in order to measure/calculate power? Well before giving you a basic overview I
would recommend you to watch my basics video about true, reactive, apparent and deformed
power in order to get a true understanding of the subject.

And with that being said let’s just simply
get an old school light bulb and hook it up to the power grid while having a look at its
current flow and its used voltage. As you can see the voltage features the typical
sinusoidal shape with a top value of 326V, an RMS value of 231V and a frequency of 50Hz,
so all normal values here in Germany. If we now observe the current, we can see
that the waveform is also sinusoidal and it follows the voltage waveform. That means our load is resistive which means
it is the simplest load we can deal with. If we would hook up an inductive load like
a motor then the current waveform would be lagging and if we would hook up a capacitive
load then the current waveform would be leading. And if we got the most difficult load to work
with like a laptop power supply then the current can look something like this with current
spikes near the voltage peaks.

Now you might be surprised but it is actually
not that difficult to calculate the real power of those 4 voltage and current waveforms but
let’s start with the simplest one. The formula for calculating real power looks
something like this which might seem a bit scary but by using the math function of my
oscilloscope I can partly visualize the formula on screen.

The newly shown waveform is the multiplication
of the voltage and current values in each sampled point and it represents the real power
consumption over time. And if we would add up all of the area underneath
this waveform then we would get the average real power consumption. But let’s make it a bit more difficult by
having a look at an inductive load. As you can see we still got mostly positive
power values but this time also some negative power values.

Those values are not real power since this
power form only oscillates between the power source and load. So by subtracting this negative area from
the positive one, we once again get the average real power we are looking for. Similar negative power areas appear for capacitive
loads and also for our laptop charger load but you should now understand how to calculate
the real power consumption of them as well. Now to sample the voltage and current with
our microcontroller we will need a safety transformer for the voltage and a current
transformer for obviously the current. What the microcontroller will do is sample
the current and voltage waveform a specified amount of times per half wave.

Then it will multiply the values with one
another, sum them up and divide them by the amount of samples and just like that we get
an average real power measurement. Along the way the microcontroller will also
calculate the RMS Voltage and RMS current and use it to calculate the apparent power
and then finally the power factor but if you want to learn more about those terms then
definitely have a look at the openenergymonitor.org website which has tons of great information
and I will also be using their EmonLib library in order to make programming easier for me. But anyway with the theory out of the way
how exactly do we have to hook up the two transformers to the microcontroller. To find that out I connected my safety transformer
to mains voltage but let me tell you that working with mains voltage can be lethal if
not handled correctly, you have been warned.

After building

After then adding wire bridges to its output
terminals, like its shown on its label to get 12V AC, I hooked up my oscilloscope probes
in order to find out that I was getting 39V peak to peak which is of course a way too
high for my 3.3V microcontroller. To safely use it, we not only have to reduce
the voltage but also let it swing around an offset of for example half the supply voltage
so that the microcontroller can properly sample the waveform with its Analog to Digital Converter.

So what we need is basically such a circuit. After adding all of the components to the
microcontroller and the transformer we can see how the voltage is now definitely suitable
for our task which brings me to the current transformer. I got this one from EBay for pretty cheap
and it can handle a maximum current of 5A and comes with a Ratio of 1000:1. That means as soon as we pass a live wire
through its core and power a load so that current flows through it, the created magnetic
field of the primary side will create a current through the secondary side which will be 1000
times lower. So at for example 5A we would get a current
flow of 5mA which will create a voltage drop of 1V since the current transformer uses a
burden resistor of 200 ohms.

Now to make this 1V current signal suitable
for the microcontroller I will using this simple circuit which just adds an offset. After building it up, we can see on the oscilloscope
that all values are now in the desired range so it was time to hook up the voltage and
current signal wires to pin 34 and 35 of the ESP32 respectively. For the software side I already told you that
I will be using an ESP32 version of the EmonLib library but what I haven’t told you yet
is that you should definitely browse through their included .cpp file in order to see some
beautiful well explained math on how to calculate all the values.

But anyway after uploading the given example
code and connecting my energy multimeter to the load in order to check whether the calculated
values are all correct, I noticed that pretty much nothing was correct yet. The problem was that the current and voltage
calibration variables were not adjusted yet, but after fiddling around with them for quite
a while I reached a sweet spot. As you can see we can measure pretty similar
power values with my complicated laptop charger but also with more high power loads like my
toaster.

Of course there are accuracy differences between
my 1000€ energy meter and this 30€ ESP32 meter but overall I was rather impressed with
this build. So time to integrate it into my home assistant
setup but sadly I quickly noticed that the EmonLib library is not supported by ESPHome. That is why I browsed through the available
tutorials in order to learn how to create a custom sensor. To do that all I had to do was to upload the
emonlib.h and emonlib.cpp file to the esphome folder and then create a new .h file into
which I more or less copied the example Arduino code. After then altering the code of my new ESP32
node, it compiled and uploaded without any problems. That means it was time for me to add all the
power values to my overview board and as you can see everything still seems to work just
fine, awesome. So as a last step I soldered my ESP32 to a
perfboard and added proper wiring to my complementary components in order to add them to my home
distribution box for a test.

And I am only doing a test here because I
currently do not have the space inside my home distribution box to permanently mount
all of the components there so I will have to come up with a solution for that in the
future. But nevertheless the power meter still works
perfectly fine and I hope you enjoyed watching this project. If so don’t forget to like, share, subscribe
and hit the notification bell. Stay creative and I will see you next time!.

As found on YouTube

Leave a Reply