Frequency Measurement using Arduino

Definition

Frequency is the number of complete cycles per second in alternating current direction. The standard unit of frequency is the hertz, abbreviated Hz.

Frequency Measurement

                Frequency Measurement is carried out by measuring the number of cycles in one second, this method requires time of one second. Other method is to measure time period of one cycle F = 1 / T.

In this example time from first rise to the second rise is measured using interrupt on Pin 2 (INT0). It is set to detect rising pulse and at every 100mSec measured frequency is displayed on serial monitor.

Circuit Diagram of Frequency measurement

It does not require any external components; you may connect pull down resistor of 100k on Pin 2 to avoid noise measurement when there is no input. Measurement is carried out on interrupt 0, Use only Pin 2 of Arduino.

Arduino Frequency Measurement Circuit
Arduino Frequency Measurement Circuit

Arduino Frequency Measurement Code

/* 
 *  Frequency Measurement Using Arduino
 *  Inpout is given to Pin 2
 *  Uses Interrupt to get pulse duration
 *  F = 1 / pulse duration
*/

#define MainPeriod 100

long previousMillis = 0; // will store last time of the cycle end
volatile unsigned long duration=0; // accumulates pulse width
volatile unsigned int pulsecount=0;
volatile unsigned long previousMicros=0;

void setup()
{
  Serial.begin(9600); 
  attachInterrupt(0, myinthandler, RISING);
}

void loop()
{
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= MainPeriod) 
  {
    previousMillis = currentMillis;   
    // need to bufferize to avoid glitches
    unsigned long _duration = duration;
    unsigned long _pulsecount = pulsecount;
    duration = 0; // clear counters
    pulsecount = 0;
    float Freq = 1e6 / float(_duration);    //Duration is in uSecond so it is 1e6 / T

    Freq *= _pulsecount; // calculate F
    // output time and frequency data to RS232
    Serial.print("Frequency: ");
    Serial.print(Freq);
    Serial.println("Hz"); 
  }
}

void myinthandler() // interrupt handler
{
  unsigned long currentMicros = micros();
  duration += currentMicros - previousMicros;
  previousMicros = currentMicros;
  pulsecount++;
}

Frequency Measurement Result

Open Serial monitor and give some frequency input to Pin2 of Arduino and Observer the result.

For testing this circuit just take Pin2 wire near AC Supply cable, it will show line frequency.

Frequency Measurement Result
Frequency Measurement Result

3 thoughts on “Frequency Measurement using Arduino

  1. Recently I have acquired your book about measurements, thinking that perhaps the circuits were more developed in the book, but I see that they are as they appear on the web. In particular this circuit interests me especially and I have the doubt of which is the correct scheme, because my English is not good and I am worried about destroying the Arduino. When you say “For testing this circuit just take Pin2 wire near AC Supply cable, it will show line frequency” I’m not sure if it means that I can put AC on an Arduino pin or if I have to put that pin close to an cable with the AC that I want to measure.
    Greetings .
    Translated with Google translator.

    Original
    Recientemente he adquirido su libro sobre medidas, pensando que quizás los circuitos estaban mas desarrollados en el libro, pero veo que son tal cual aparecen en la web. En concreto este circuito me interesa especialmente y tengo la duda de cual es el esquema correcto, por que mi ingles no es bueno y me preocupa destruir el Arduino. Cuando dice usted “For testing this circuit just take Pin2 wire near AC Supply cable, it will show line frequency” no tengo claro si se refiere a que puedo poner AC en un pin de Arduino o que si tengo que poner ese pin cerca de un cable con la AC que quiero medir.
    Saludos .

    Traducido con Google translator.

Leave a Reply