## Monday, December 18, 2017

### Power factor measurment using Arduino

In this project i used arduino and XOR gate to power factor measurement.

What is power factor?
Power factor is a number between 0 and 1 refers to phase shift between voltage phase and current phase in AC appliance.

How to measure PF with Microcontroller (Arduino)?

To measuring the power factor with microcontrollers in the first we used Op-Amp to zero cross detection to find the phase shift between current and voltage phase, the op-amp convert the sine wave signal comes from CT and PT with different amplitude to square wave with about 4v amplitude, then we connect these two square wave to XOR gate inputs.
XOR gate output will be 1 just when the inputs have different signals so when the load is resistive XOR gate output is 0 because both voltage and current phases start and ends in the same time, but when the load is inductive or capacitive XOR output is 1 because there are phase shaft between voltage and current,

Hence we can find the power factor by measuring the "ON-time" of XOR output, and put it in the power factor formula :

PF= COS(f*dt*360)

Where:
PF=Power Factor
f= Frequency
dt=Time difference between two phases and/or XOR output ON-time
Circuit:
Here we have two circuits, the first one using Current transformer(CT) and Voltage or potential transformer(PT), the only difference between them is that second circuit with no transformer and no insulation between microcontroller and main voltage so its dangerous if you touch any point in the circuit, but its cheap, small size, light weight and similar to commercial socket power meter.
NOTES:
1-This project deal with main voltage so its dangerous.
2-However transformers haven't polarity but here transformer winding point(dot) is very important, because if CT and PT dot don't match, the arduino will read low PF even with pure resistive load, So after making this project, firs load you should connect to it is a pure resistive load to make sure every thing is correct if the reading PF was lower than 1  just reverse one of the transformers (PT for example).

3-For current transformer you can use an Clamp-on transformer, I opened my old cheap clamp meter and used its transformer and works well.

In this video you can see how this project works:

You can improve this project to power factor correction by adding some relay and capacitor and improving the code.

Parts list:
Arduino board https://goo.gl/XR3mXG
IC 4030 or IC 4070 (XOR gate)
Voltage Transformer 15V or less
Current transformer 5A/5mA
Resistor kit https://goo.gl/p4JvQY
Resistor 100K
Resistor 220 ohm
Resistor 200 ohm
Potentiometer 10K or 100K (Potentiometer Kit https://goo.gl/z5b4X1)
Ceramic Capacitor kit https://goo.gl/q29Du3
Capacitor 47pF (2pcs)
Capacitor 100nF
Capacitor470uF (Capacitor Kit https://goo.gl/6zqbJy)
LCD display 16x2 https://goo.gl/yTjFJS
Diode 1N4148 (4pcs) (Diode kit: https://goo.gl/zA6r7x)
LM324
Connecting wires https://goo.gl/BVC3pM
Energy meter: https://goo.gl/DztkwW

CODE:

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int pin = 13;
float degree = 360;
float frequency = 50;
float nano = 1 * pow (10,-6); // Multiplication factor to convert nano seconds into seconds

// Define floats to contain calculations

float pf;
float angle;
float pf_max = 0;
float angle_max = 0;
int ctr;

void setup()
{
pinMode(pin, INPUT);
Serial.begin(9600);
lcd.begin(16, 2);
}

void loop()
{

for (ctr = 0; ctr <= 4; ctr++) // Perform 4 measurements then reset
{
// 1st line calculates the phase angle in degrees from differentiated time pulse
// Function COS uses radians not Degree's hence conversion made by dividing angle / 57.2958
angle = ((((pulseIn(pin, HIGH)) * nano)* degree)* frequency);
// pf = cos(angle / rads);

if (angle > angle_max) // Test if the angle is maximum angle
{
angle_max = angle; // If maximum record in variable "angle_max"
pf_max = cos(angle_max / rads); // Calc PF from "angle_max"
}
}
if (angle_max > 360) // If the calculation is higher than 360 do following...
{
angle_max = 0; // assign the 0 to "angle_max"
pf_max = 1; // Assign the Unity PF to "pf_max"
}
if (angle_max == 0) // If the calculation is higher than 360 do following...
{
angle_max = 0; // assign the 0 to "angle_max"
pf_max = 1; // Assign the Unity PF to "pf_max"
}
Serial.print(angle_max, 2); // Print the result
Serial.print(",");
Serial.println(pf_max, 2);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("PF=");
lcd.setCursor(4,0);
lcd.print(pf_max);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print("Ph-Shift=");
lcd.setCursor(10,1);
lcd.print(angle_max);
lcd.print(" ");

//delay(500);
angle = 0; // Reset variables for next test
angle_max = 0;
}