Welcome to Solarduino , A blog about DIY Solar PV and Arduino projects

How to measure AC Frequency with Arduino?

Power line frequency or mains frequency is the oscillation cycles of alternating current (AC) in an electric power transmission line. Most of the countries or regions around the world are using 50Hz (50 cycles per second) while Americas and part of Asia countries are using 60Hz (60 cycles per second). In order to determine the AC frequency, we need to use the AC voltage or AC current module to create a waveform signal and calculation the period and length of the waveform. I highly recommend to use AC voltage module to measure frequency as the waveform is stable while current wave is altered subject to loads.

Arduino has the ability to measure AC voltage or AC current using analog input pin. For Arduino UNO, there are 6 analog input pins (A0-A5) where you can use one of the pins. Arduino NANO has 8 pins while Arduino MEGA has 16 input pins. The analog input pins will map input voltages between 0 and 5V into integer values between 0 and 1023 with resolution of 4.9mV per unit (5.00V / 1024 units). 

Arduino UNO (compatible board)

If you still not yet own an Arduino Micro-controller Board, you can get it cheap at our affiliate link here !!!

Single Phase AC Voltage Module

This module is equipped with ZMPT101B high-precision voltage transformer and op amp circuit. It can measures AC voltage within 250V. The corresponding output signal can be adjusted using the trimmer potentiometer. You can grab this module at our affiliate link here !!!

Warning ! You are now dealing with high power source ! We assumed that you have the basic electrical knowledge and know what you are dealing with. You may need guidance from experienced guys if you are new to electrical work. Safety and Precaution must be always have in mind. We shall not be responsible for anything happening to you.

ACS712 Current Sensor Module 

This is a AC or DC current sensor rated at 5A, 20A and 30A which are suitable to most applications. You may get them by our affiliate link here !!! The 5A module has the resolution of 185mV/ampere, 20A module has 100mV/ampere while 30A module has the resolution of 66mV/ampere.

Hall-Effect Split-Core HSTS016L AC current sensor module. 

This Hall-Effect Split Core Sensor can measure AC current and do not need to make re-wiring or adjustment of existing wiring unlike ACS712 current module. The model ranges from 10A up to 200A. You can get it via our affiliate link here !!! The output voltage of this sensor is 2.5V +/- 0.625V with decent accuracy. I highly recommend this sensor for measuring AC current.

At first, I recommend to use AC Voltage module rather than AC module. This is because AC Voltage module is more stable in signal oscillation / fluctuation and the signal still available even when no loads are attached (no current value). When you turn on voltage module and voltage measurement source, it should read by Arduino in analog value in perfect waveform. This is the basic output of the module in analog value. You can copy the below code to try your own. You can see the waveform in Serial Plotter.

void setup() {
Serial.begin(9600);
}

void loop() {
Serial.println(analogRead(A2));
delay(300);

}

The middle point should be technically at 512 (analog value) and it fluctuates within 0 to 1023. This base value can be further processed into usable information. The amplitude value can be adjusted via the built-in trimpot. For more information on how to measure AC voltage and adjustment, kindly visit our blog post here !!!

How the signal being processed

Below is the diagram how to signal being processed and get the frequency value based on the waveform. The calculation is slightly simple which involved some conversion and averaging.

The first step is making the waveform oscillates at analog read 0 value to differentiate between positive and negative side. This is actually optional as it is just based on calculation formula which you also can take 512 value as middle of oscillation. But I prefer to make it 0. The magnitude at output (Serial monitor) is within -512 to 512 limit and the actual wave magnitude is not our concern as we are not measuring the AC Voltage value.

The second step is add counting time and counting quantity of sample. The counting quantity sample starts when wave value is larger than 0 which is the initial point of a sine wave. Whenever the last sample is taken for the averaging calculation, the last count become the “previous count” of the next set of reading so it is not taking into consideration for the next counting set. However, due to the starting time is reset on the last sample count, the starting time was actually started at previous count. For a clear picture, see the diagram as above.

The frequency value will be the total accumulate time of the set divided by the total number of count (quantity of samples). We have been testing using each cycle to count time and the time is reset every cycle but ended up being not accurate. But when taking an average of 50/60 samples in a macro scale of time and average them, everything become quite accurate.

LCD Display Shield 

This is a shield that allows the output value of your Arduino board to be displayed on the screen. Since it is a shield, you can just stack it on Arduino board without the need of extra wiring for the LCD Display. You can get the LCD Display board at our affiliate link here !!!.

Hardware Connection – Single Phase AC Voltage Module

Since we are using AC Voltage Module to indirectly measuring power line frequency, the wiring is exactly the same as how er hook up the AC Voltage module for AC Voltage measurement.

In order to connect wiring between Arduino board and module, you need the dupont line cables male to female. You can get it at our affiliate link here !!!

You need the connector that can secure cables and isolate from accidental touch. Get the fast connector at our affiliate  link  here !!!

You need a fuse to protect your safety and cables!! You can get it here !!

Software Codes

The next step would be adding source code onto Arduino board. I assume you have installed the Arduino Software. If you have not installed the software, the link here can bring you to the official download site. Once you have downloaded the software, you may download the code file (.ino) for this application below (right click save link). 

There are 2 source codes file attached which are source code with and without LCD display shield function. If you don’t have LCD Display shield with you, kindly choose the code that is without LCD Display Shield and monitor the output frequency value through Serial Monitor at Arduino IDE Software.

With LCD Display Shield, once the code is uploaded to the Arduino board, the current value will be shown in Serial Monitor using Arduino Software and on the same time shown on the LCD Display. I would recommend you to add the LCD Display shield which make very convenient for the value to be shown.

Screw Shield / Expansion Shield

When there are a lot of wiring around especially more than 1 sensor, sharing pins will be difficult as existing pins (ground and 5V) are limited. This shield provides a lot of convenient terminals for each of the input and output pins. The shield can be mounted directly on top of the Arduino Uno board or in between the shields which made it very convenient to use. You can get it at our affiliate link here !!!

Codes for AC frequency using AC Voltage Module (with LCD Display Shield). Note: the codes shown here may not be 100% correct due to translation error. For accurate code, kindly download the .ino file. 
</div>
<div>

// AC Frequency with LCD By Solarduino

// 0- General

int decimalPrecision = 2;

// 1- frequency measurement

unsigned long startMicros;
unsigned long currentMicros;
int expectedFrequency = 50;
int frequencyAnalogPin = A2;
float frequencySampleCount = 0;
float frequency =0 ;
float a;
float switch01 = 0;
float vAnalogRead = 0;

// 2 - LCD Display

#include LiquidCrystal LCD(8,9,4,5,6,7);
unsigned long startMicrosLCD;
unsigned long currentMicrosLCD;

void setup()
{

// 0- General

Serial.begin(9600);

// 1- frequency measurement

startMicros = micros();

// 2 - LCD Display

LCD.begin(16,2);
LCD.setCursor(0,0);
startMicrosLCD = micros();
}

void loop()
{

// 1- frequency measurement

currentMicros = micros();
vAnalogRead = analogRead(frequencyAnalogPin) - 512;

if(vAnalogRead &gt;=0 &amp;&amp; switch01 == 0)
{
frequencySampleCount = frequencySampleCount +1 ;
switch01 = 1;
}

if(vAnalogRead &lt; 0 &amp;&amp; switch01 == 1) { switch01 = 0; } if(frequencySampleCount == expectedFrequency) { a = currentMicros-startMicros ; frequency = 1/((a/1000000)/frequencySampleCount); Serial.print(frequency,decimalPrecision); Serial.println(" Hz"); startMicros = currentMicros; frequencySampleCount = 0; // 2 - LCD Display currentMicrosLCD = micros(); if(currentMicrosLCD - startMicrosLCD &gt;=1000000)
{
LCD.setCursor(0,0);
LCD.print("f=");
LCD.print(frequency,decimalPrecision);
LCD.print("Hz ");
LCD.setCursor(0,1);
LCD.print(" ");
startMicrosLCD = currentMicrosLCD ;

}
}

}

Before we end, we would like to give gratitude to you for taking the time to read the post. We would need readers like you to support us in order to keep growing. You can support us in the following ways :

Donate & Fund Raising

If you like my work, please send me a donation to encourage me to do more. Thanks

Aliexpress Affiliate

We are the member of Aliexpress affiliate marketing. Do support us by clicking the affiliate product links if you do wish to purchase them.

Like and Share

If you like our post, we need your support to like and share our posts or videos so that it can reach more and more people like you !!

Result – In Serial Monitor

Result – On LCD Display

For Arduino Code Files, Remember to Right Click > Save Link As … You may alter the internal code as you wish. Happy coding !! 

AC Frequency.ino
AC Frequency with LCD Display.ino