Single Phase Power Monitor with PIC18F46K22 Microcontroller

This post shows how to build a simple DIY single phase power meter station mainly based on Microchip PIC18F46K22 8-bit microcontroller.
Power quantities which are: Voltage, Current, Apparent Power, Active Power, Reactive Power, Frequency and Power Factor are printed on Nokia 5110 LCD screen as shown in the below images.

Theoretically, this DIY power station can measure electric currents up to 50 Amps and voltages up to 500V, this means it should work with home electric network (2-phase and 1-phase AC loads).
This circuit can measure positive and negative values of Active Power, Reactive Power and power Factor. A negative Active Power means that the power is transferred from the load to the source (reverse power), this occurs only when we have a real power source such as generator. A negative Reactive Power means that the power is transferred from the load to the source, this occurs when we have reactive power source such as capacitor.
CCS C Compiler is used in this project.

Single phase power meter station hardware circuit

Hints:
No warranty is provided with this project, so do it at your own risk!
A part of project circuit may be subjected to a high voltage level which is very harmful to human body, so be-careful!

Abbreviations:
AC: Alternating Current
DC: Direct Current
TRMS: True Root Mean Square.
ADC: Analog-to-Digital Converter
CT: Current Transformer
VT: Voltage Transformer
PCB: Printed Circuit Board
DIY: Do It Yourself

Single phase power meter station with PIC18F46K22 MCU circuit:
Project circuit diagram is shown below (click on the image for full resolution view).

Single phase power meter station using PIC18F46K22 MCU ans Nokia 5110 lcd, ADG609, OPA2191, MCP6V02, MCP1501, BAT54S, Voltage transformer, Current transformer

The main power source voltage of the circuit is 5V, it’s connected to J1 connector (5V Input connector) where pin 1 is negative (GND) and pin 2 is positive terminal.

Hardware required:
This is a summary of circuit required parts (circuit schematic diagram may contain some component parameters not mentioned below).

  • PIC18F46K22 microcontroller (U3)   —>   datasheet
  • Nokia 5110 LCD screen
  • Current-type voltage transformer 2mA/2mA (T1)
  • Current transformer with turns ratio 1000:1 (T2)
  • ADG609 analog multiplexer (U8)   —>  details
  • 2 x OPA2191 op amp (U6, U7)   —>  details
  • MCP6V02 op amp (U5)   —>  details
  • MCP1501-20 2.048V voltage reference (U4)   —> details
  • TPS60403 charge pump voltage inverter (U2)   —>   details
  • AMS1117-3.3 voltage regulator
  • 3 x BAT54S dual serial small signal Schottky diode
  • 2 x SMBJ3.3CA TVS diode
  • 16MHz quartz crystal (Y1)
  • 3 x 10uF capacitor (C2, C3, C4)
  • 4 x 1uF capacitor (C1, C5, C6, C12)
  • 15 x 100nF capacitor (C9, C10, C11, C13, C14, C15, C19, C20, C21, C22, C24, C29, C30, C31, C32)
  • 22nF capacitor (C26)
  • 6 x 10nF capacitor (C17, C18, C23, C25, C27, C28)
  • 1nF capacitor (C16)
  • 2 x 22pF capacitor (C7, C8)
  • 1M Ohm resistor (R1)
  • 2 x 200k Ohm resistor (R27, R43)
  • 2 x 100k Ohm resistor (R36, R41)
  • 2 x 80k Ohm resistor (R35, R40)
  • 4 x 75k Ohm resistor (R19, R20, R22, R23)
  • 6 x 20k Ohm resistor (R15, R17, R24, R28, R31, R39)
  • 2 x 18k Ohm resistor (R33, R38)
  • 7 x 10k Ohm resistor (R13, R14, R16, R25, R26, R29, R42)
  • 7 x 2k Ohm resistor (R7, R8, R9, R10, R11, R30, R37)
  • 5 x 1k Ohm resistor (R2, R3, R4, R5, R6)
  • 300 Ohm resistor (R21)
  • 2 x 100 Ohm resistor (R18, R32)
  • 2 x 10 Ohm resistor (R12, R34)
  • 1k Ohm ferrite bead (FB2)
  • 120 Ohm ferrite bead (FB1)
  • 5V source
  • Breadboard & jumper wires…
  • PIC MCU programmer (PICkit 3, PICkit 4 …)

Note for better accuracy that the following resistors should have a tolerance no more than 1%:

  • R19, R20, R22, R23 (75k) –> 0.25W Through hole resistors are recommended in order to handle voltage rating of 500V.
  • R21 (300R)
  • R16, R25, R29, R42 (10k)
  • R15, R24, R28, R39 (20k)
  • R27, R43 (200k)
  • R30, R37(2k)
  • R33, R38 (18k)
  • R35, R40 (80k)
  • R36, R41 (100k)
  • R34 (10R)

Single phase power meter station hardware circuit

Circuit description:
The PIC18F46K22 microcontroller used in this project is a 40-pin 8-bit MCU supplied with 5V at VDD pins (#11 and #32). A Quartz crystal with frequency of 16MHz is used as an external clock source to the MCU, with the PLL enabled in the software the MCU becomes running at frequency of 16 x 4 = 64MHz.

As mentioned earlier, all power quantities are displayed on Nokia 5110 LCD screen. This display contains the PCD8544 controller, it is similar to the one used in Nokia 5110 mobile phone, it uses SPI interface protocol with maximum clock frequency of 4 MHz and requires 5 control pins.

The resolution of this LCD is 84 x 48 which means it has 4032 pixels. This module works with 3.3V only and it doesn’t support 5V (not 5V tolerant), this means interfacing it with 5V microcontroller such as the PIC18F46K22 MCU requires voltage level shifter.

To get a 3.3V supply I used the AMS1117 3.3 LDO regulator, it just gives a regulated 3.3V from the 5V input.
To connect the PIC18F46K22 data lines to the LCD module, I used voltage divider for each line. This means there are 5 voltage dividers, each one consists of 1k and 2k resistors, this drops control voltage from 5V into 3.3V.

Actually, choosing the values of voltage divider resistors depends on many factors such as: maximum frequency and input pin capacitance but 1k & 2k will do the job as it should be in our case, however, using a 3.3V microcontroller is recommended when dealing with 3.3V devices.

So, the Nokia 5110 LCD pins are connected to PIC18F46K22 MCU as follows (each one through voltage divider):
RST (reset) pin is connected to pin RC0 (#15),
CE (chip enable) pin is connected to pin RC1 (#16),
DC (data/command) pin is connected to pin RC2 (#17),
CLK (clock) pin is connected to pin RC3 (#18),
DIN (data in)  pin is connected to pin RC5 (#24).

VCC and BL are connected to AMS1117-3V3 regulator output pin and GND is connected to circuit ground (0V).

ADC Voltage reference:
I used the MCP1501-20 high-precision buffered voltage reference from Microchip to get a precise voltage of 2.048V which is then used as a positive voltage reference for the PIC18F46K22 microcontroller ADC module and a bias voltage for voltage and current AC signals.
I also used 1 op amp of U5 (MCP6V02) as voltage follower to buffer the output voltage of the MCP1501-20.

Voltage signal conditioning circuit:
As we can not connect mains wires directly to the microcontroller due to the high voltage level, I used a small current-type voltage transformer labeled in the circuit schematic as T1. The full name of the one I’m using in this project is DL-PT202D with turns ratio of 1. The input of this transformer is current and the output is also current proportional to the input. I used 4 current limiting resistors (R19, R20, R22, R23) each of 75k to keep transformer input current under its rated one (2mA RMS) when it is subjected to 500V AC.
In order to handle 500V with no problem and for nice accuracy, the last 4 resistors should be a through hole type, 1/4W with tolerance of 0.1%.
With this transformer the high voltage circuit is galvanically isolated from low voltage circuit.

There are many alternatives to the voltage transformer that I’m using such as ZMPT112, any other replacement should have the same voltage rating (or higher) and ratio (2mA/2mA).
This type of transformers gives a significant safety factor to the circuit as it provide a galvanic isolation between the high voltage mains circuit and the low voltage control circuit which is in a direct contact with human body and other electronic equipments. They also have a small size, low cost and require few components.

A resistor of 300 Ohm (R21) is connected across the secondary of the voltage transformer, a voltage drop will be created across this resistor when an electric current passes through it.
With the created voltage, the MCU will be able to get voltage samples and also measure line frequency.

Current signal conditioning circuit:
As shown in project circuit schematic diagram above, the current transformer which noted as T2 has turns ratio of 1000/1, the secondary of the current transformer is connected to a shunt resistor with resistance of 10 Ohm (R34), this means at a load current of 50A AC the voltage generated across R34 will be: 50 x 10 / 1000 = 500 mV AC.

I used the ADG609 analog multiplexer from Analog Devices and OPA2191 op amp (U7) from Texas Instruments with some resistors to build a programmable gain amplifier with gains: 1, 2, 10 & 100 where we can change the gain through the ADG609 inputs A0 (pin #1) and A1 (pin #16). For example if A0 = A1 = LOW then the gain = 100, and so on.

The ADG609 analog multiplexer and the two OPA2191 op amp ICs (U6, U7) are powered with bipolar supply of voltage ±5V, I used the TPS60403 60mA charge pump voltage inverter from Texas Instruments to get -5V where it automatically generates a -5 V from the +5 V supply.

Another alternatives with pin compatible to the ADG609 are: ADG409 –datasheet– and ADG5409 –datasheet-, the best one is the ADG5409 since its typical ON resistance is low (13.5 Ohm), but according to device’s datasheet the ADG409 as well as the ADG5409 require dual supply with minimum voltage of ±9.

Resistor R35 & R40 have a resistance of a not well used value -80k Ohm-, we may connect two resistors in parallel to get 80k, for example in my DIY circuit I used 120k in parallel with 240k.
Note that for better accuracy all the PGA circuit resistors should have a tolerance of 0.1%. A tolerance of 1% can also be used but it will directly affects the accuracy of the measurements.

Single phase power meter with PIC18F46K22 MCU C code:
The C code below is for CCS C compiler, it was tested with version 5.112.
To be able to compile the C code with no error, a driver C file for Nokia 5110 LCD is required, download link is the one below. After the download just put the file in project folder:
NOKIA_LCD.c

Project microcontroller C code calculates average and RMS values of AC signals, the average value is just used in the RMS calculations. The MCU only shows the RMS values of mains voltage and load current on the LCD.

Simply the average (mean or DC offset) value in discrete-time is the sum of all sample values divided by number of samples:

mean value equation

and the RMS value in discrete-time domain can be calculated using the following equation:

RMS value equation

The PIC18F46K22 microcontroller contains a 10-bit ADC module, with a positive voltage reference of 2.048 V, a 0 V is digitally represented by 0 and a 2.048 V is represented by 1023.

Programming hints:
As we have a 10-bit ADC module working in the range [0, 2.048V], the MCU makes 64 samples every 1 cycle for both Voltage and Current.

For a 50 Hz signal the period is 20 ms, the MCU takes one sample every 20000/64 = 312.5 us. I used CCP5 (Capture, Compare, PWM) module to start an ADC conversion every 312.5 us where the module is configured to use Timer5 for the compare operation, once the conversion is complete ADC interrupt saves the digital values on its corresponding array. The array element is incremented every new reading until the filling of all elements and completion of the cycle.

To measure voltage frequency I used PIC18F46K22 MCU internal analog comparator for zero crossing event detection and CCP4 with Timer3 for measuring the pulse widths and therefor line frequency. After every cycle CCP5 registers updated in order to adapt the new cycle period, number of samples which is 64 and ADC sampling frequency.

At all times the microcontroller automatically changes the gain of the PGA circuit according to the load current, if the load current is less than 0.5A the MCU sets the gain to 100, if the load current is between 0.5A & 5A then the MCU sets the gain to 10, if the current is between 5A & 25A then the MCU sets the gain to 10 and if the load current is higher than 25A the MCU sets the gain to 1.

Full CCS C code:

The following small video shows my DIY circuit for this project:

When a capacitive load is connected to the circuit the load then works as a reactive power source and the LCD will show the reactive power value with negative sign as shown below (-479 VAr):

Single phase power meter station hardware circuit

The current transformer (CT) I used in this project is shown below:

Current transformer for DIY power meter station

Related Projects:
PIC18F46K22 MCU Based AC Current Measurement Project
AC Voltage Measurement with Arduino Board and LCD


Discover more from Simple Circuit

Subscribe to get the latest posts sent to your email.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top