Sometimes in order to learn something, it is good to build and experiment and measure. That’s not always possible due to practicality concerns. This was the case recently, for a review of a piece of equipment that can work with a three phase mains electricity connection. The trouble is, I do not have a three-phase supply in my home, and I do not have easy access to such a thing at work either (and neither do most people unfortunately). Furthermore, I would not trust myself with a mains supply system that I am unfamiliar with.


As a result, I wanted to be able to sufficiently simulate a real three-phase supply in order to understand it, and apply it.


It got me thinking, that such a simulation could also be useful for students that wish to experiment and learn about a three-phase system. At university I did not have access to such a simulation. It could have been rigged up perhaps with several signal generators, but that is a luxury that we didn’t have. Another approach could be to actually generate the supply (using a motor to drive a car alternator perhaps) but that would be a very noisy solution for a room full of students : ) I wanted to construct an electronic simulation, of just a few volts (for safety), but with the correct phasing.


Perhaps the idea could be taken further by even simulating circuit breakers and residual current devices or ground fault circuit interrupter. However that’s for further study; this blog post only discusses how to simulate the three phase supply. It uses a microcontroller boardmicrocontroller board and op-ampop-amp circuit to achieve this.


What is a Three Phase Supply?

Electricity generated in power plants often comes from large machines (known as synchronous generators) consisting of magnets and coils arranged such that there are three AC outputs, but with the three sine waves shifted at 120 degrees to each other. There are several reasons for this. One reason is that the power output is more even with three phases, than a single phase at the same frequency. That helps to eliminate vibrations in machines that consume the power. Another reason is that three phase generation provides huge cost savings because with such a phase shift, the voltages and currents sum up to zero in ideal conditions but at any rate will be less than the current per phase, and as a result only three phase wires plus a thin Neutral wire need to be hauled across the country, instead of six wires, two per phase. That’s a huge copper saving! It would ordinarily be difficult to demonstrate practically, but with a three-phase simulator and three loads (such as three resistors) and a few multimeters, it would be easy to show.


And finally the three phases provide convenience when factories ultimately convert that energy back into motion, because the three outputs or phases can be plumbed to motors with multiple coils (for example synchronous motors and induction motors), often without requiring brushes as with DC motors, therefore saving on maintenance costs.


Three phase electricity generation, and the factories that consume it, were fundamental to the second industrial revolution, and the generator and distribution system was possibly the single most important engineering accomplishment in the late 19th and early 20th century so I believe it is really important to be able to examine some aspects of it using physical circuits, and without needing to connect multimeters to electricity pylons!


A typical power system will use generators to create electricity at around 15kV, and then this is stepped-up using transformers (to hundreds of kilovolts) and sent across the country to stations and then substations where the supply voltage is progressively reduced down to a far lower voltage of 208V RMS phase-to-phase (US) or around 415V (UK) or 380V (Europe). From here it is used to power factories and offices, and individual phases are sent to homes.


It is possible to pay the energy supplier to plumb all three phases to your home, but it is something that would be rare, unless you needed to run industrial machines at home.


So, in the absence of plumbing my home for three-phase, or fitting a generator, I needed some way of creating a safe low-voltage three-phase supply.


Design Overview

A simulation of a three-phase mains supply can be achieved by having three oscillators out-of-phase by 120 degrees. I initially considered doing this by analog-only methods, but in the end it seemed far easier to have a mix of software and analog hardware.


Most microcontrollers do not have many analog outputs, so it is more practical to use pulse width modulation (PWM). The microcontroller can be programmed to output three PWM streams that are rapidly changed over time in order to represent the three sine wave outputs. The PWM can be filtered and amplified using analog circuits (with op amps).


Software Design

I decided to use a  FRDM-KL05ZFRDM-KL05Z board for the project because it is low-cost (around $10) and easy-to-use. The first step was to consider the PWM requirements;  since I wanted to simulate a mains supply (at 50Hz in Europe and 60Hz in the US) a digital representation of this would require a sample rate of at least twice these frequencies to satisfy Nyquist. An even higher sample rate would make life easier when it came to filtering. Another thing to bear in mind was that if the number of samples per mains cycle period (1/50 or 1/60 seconds) was divisible by three then it would reduce jitter in the output waveforms. Too high a sample rate would cause difficulty in generating the PWM signal with a very short period. Taking all these things into consideration, it was decided to have 12 samples per cycle. The diagram here shows what these sample values could look like for the three phases.


Since the microcontroller can only output a positive voltage (0V or 3.3V for logic 0 and logic 1 respectively) I needed to shift the sample values up by 1, so that the numbers on the y-axis now represent the desired PWM duty cycle normalized to between 0 and 1 (for 0% and 100% respectively).


These values were directly taken and placed in a C array, one per phase:

const float sine_arr1[]={0.5, 0.75, 0.933, 1.0, 0.933, 0.75, 0.5, 0.25, 0.067, 0.0, 0.067, 0.25};
const float sine_arr2[]={0.067, 0.0, 0.067, 0.25, 0.5, 0.75, 0.933, 1.0, 0.933, 0.75, 0.5, 0.25};
const float sine_arr3[]={0.933, 0.75, 0.5, 0.25, 0.067, 0.0, 0.067, 0.25, 0.5, 0.75, 0.933, 1.0};


Establishing those values was 90% of the software task complete. All I needed to do was output these PWM values one at a time, and cycle through each array 50 or 60 times per second (i.e. update the PWM output every 1/(50*12) or 1/(60*12) times per second. The code is very short – just a couple of dozen lines. By default it will output signals for 50Hz, but if a pin is held low at power-up, then it will output at 60Hz.


 * Three-Phase Simulator
 * main.cpp
 * rev 1.0 - shabaz - December 2017
#include "mbed.h"

// definitions
#define LED_ON g=0
#define LED_OFF g=1
#define SINE_ARR_MAX 12
#define PWM_PERIOD 100
#define TICK50 1000000/50/SINE_ARR_MAX
#define TICK60 1000000/60/SINE_ARR_MAX

// constants
const float sine_arr1[]={0.5, 0.75, 0.933, 1.0, 0.933, 0.75, 0.5, 0.25, 0.067, 0.0, 0.067, 0.25};  // blue
const float sine_arr2[]={0.067, 0.0, 0.067, 0.25, 0.5, 0.75, 0.933, 1.0, 0.933, 0.75, 0.5, 0.25};  // red
const float sine_arr3[]={0.933, 0.75, 0.5, 0.25, 0.067, 0.0, 0.067, 0.25, 0.5, 0.75, 0.933, 1.0};  // green

// global variables
DigitalOut g(LED_GREEN);
PwmOut p1(PTB7);
PwmOut p2(PTB6);
PwmOut p3(PTA12);
DigitalOut trig_out(PTA10);
DigitalIn fb(PTA11);

char idx=0; // index into the sine wave tables
int bmax; // used to determine when to turn on/off the heartbeat LED indicator
int bc=0;
char ledstate=0;
Ticker waveform_interrupt;

// interrupt handlers
    // adjust the PWM to the desired value
    // update the heartbeat LED
    if (bc>=bmax)
        if (ledstate)
    // update the trigger output
    if (idx==0)
    else if (idx==1)
    // update the index into the sine wave tables
    if (idx>=SINE_ARR_MAX)

// main function
int main()
    char forever=1;
    // set up the three PWM signals
    if (fb==0) // button pressed? If so, we are in 60Hz mode
        waveform_interrupt.attach_us(waveform_handler, TICK60);
    else // 50Hz mode
        waveform_interrupt.attach_us(waveform_handler, TICK50);

    while (forever)
        wait (1);
    return(0); // warning on this line is ok


Hardware Design

The design is identical per phase, so only one phase is shown here. It consists of a low-pass filter (Sallen-Key topology) followed by an amplifier. The filter will begin attenuating beyond about 70Hz, so it is suitable for both 50 and 60Hz output options. The filter and amplifier were together implemented in a single LM2904 chip (which contains dual op-amps).


The circuit was created on prototyping board, one board per phase, and then sandwiched together via the shared power rails, just for convenience. The circuit requires a dual rail supply (or two 9V batteries could be used).


Testing It!

The input and output of the circuit was connected to an oscilloscope, and it was possible to see the PWM stream and the filtered output; the result was not bad considering the simplicity of the project.


The output was also measured with a voltmeter, and each phase measured approximately 3.3V RMS.


As a brief experiment, I connected a 470 ohm load between each phase and 0V (which simulates the neutral connection). With a multimeter I observed approximately 300uA RMS current through the neutral connection, but 7.2mA through each load, proving that the neutral wire was carrying near-zero current and in theory could be disconnected. The small error is due to gain differences and load inaccuracy (I used 5% tolerance resistors).


Printed Circuit Board

Although it wasn't ordered and tested, a PCB design was laid out. This board will plug on top of the FRDM board. It should work, but it would be great to get feedback from anyone who tries it. The CAD files are attached to this blog post, and they are ready for sending to any PCB manufacturer (e.g. iTead). The full circuit diagram for this PCB is also attached below. Capacitor C19 is a 22uF tantalum22uF tantalum part, and C20 is a 10uF electrolytic capacitor10uF electrolytic capacitor. All other passives are 0603 sized so everything is easy to hand-solder. U4 is a 5V voltage regulator5V voltage regulator used to power the FRDM board, so that it doesn't need connecting to the PC USB port during normal operation.


The revision 2 PCB also contains a DC-DC converter to supply the dual rails, so it can be powered off a single 9-12V DC supply. It uses a TMR1222 DC-DC converter moduleTMR1222 DC-DC converter module, and C21 is any aluminium SMD capacitor of at least 22uF 25V, and C22 and C23 are 4.7uF 25V tantalum capacitors4.7uF 25V tantalum capacitors. The DC-DC converter and capacitors can be seen on the left side of the PCB render here:




It is possible to go a fair way toward simulating a three phase mains system using a microcontroller and an op-amp circuit. Since it is more of an educational circuit than of any other significance, it could be useful for classrooms. It was fun to experiment with three phase power, because I’d never had the opportunity to do this before. If you wish to replicate the project, for more information on how to use the FRDM board see the Working with FRDM Boards and ARM mbed guide. The binary file and the source code is attached to this blog post.