element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Test Instrumentation
  • Challenges & Projects
  • Project14
  • Test Instrumentation
  • More
  • Cancel
Test Instrumentation
Blog Frequency Counter (Square, Sine or Tri angular)
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Test Instrumentation to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: mahmood.hassan
  • Date Created: 3 Oct 2018 3:07 PM Date Created
  • Views 3463 views
  • Likes 9 likes
  • Comments 2 comments
  • ardbeginner
  • arduino uno
  • tachnometers
  • step counter
  • arduino uno r3
  • frequency counter
  • diytestinstruch
Related
Recommended

Frequency Counter (Square, Sine or Tri angular)

mahmood.hassan
mahmood.hassan
3 Oct 2018

Arduino or any other controller need TTL signal levels at input to count pulses and only work with square wave.

So what if we want to find out the frequency of sine ,triangular or low voltage square wave?

 

We can solve this problem with the help of comparator IC like lm339 which can operate down to 2V (Fig. 1) and also we can use separate power supply to power up the IC and use TTL voltage to pull up the output of comparator. (FIG. 2)

image

FIG. 1

image

FIG. 2

 

If we look at arduino UNO R3 specification we can find that it has 3 timers TMR0 (8-bit), TMR1 (16-bit) and TMR2 (8-bit) also shown in block diagram of ATmega 328P (FIG. 3). And Only TMR0 and TMR2 can be used as input.

 

TIMER Arduino IO - pin
TMR0 IO - 4 (INPUT)  IO - 5 & 6 (OUTPUT)
TMR1 IO - 5 & 8 (INPUT)  IO - 9 & 10 (OUTPUT)
TMR2 IO - 3 & 11 (OUTPUT)

 

We can use use either TMR0 or TMR1 as frequency Input. To choose the best option between these two timers we can opt TMR1 as input because it is 16-bit. And use other two timers in cascading to calculate elapsed time for better accuracy. (After editing TMR1 setting we cannot use millis and micro)

 

 

image

FIG. 3

 

Connect the arduino UNO with 16x2 LCD as shown in FIG. 4.

Arduino UNO and LCD Connections

FIG. 4

 

We can use TMR0 and TMR2 to generate 1 second time. To use both timer we have to physically link the output of TMR2 to TMR0. (IO - 3 to IO - 4). The Arduino record the no of pulses at TMR1 input (IO - 5) for 1 second, for each time if TMR1 overflow during 1 second record time the overF variable is incremented by 1. After recording number of pulses for 1 sec it calculate the frequency and time period of input signal.

 

#include <LiquidCrystal.h>

  byte overF=0;
  unsigned long freq;
  double period;
  
   // initialize the library with the numbers of the interface pins
  LiquidCrystal lcd(12, 11, A2, A3, A4, A5);

// the setup function runs once when you press reset or power the board
void setup() {

    lcd.begin(16, 2);   // set up the LCD's number of columns and rows:
    
    //4*250 = 1000 ms - timer0
    OCR0A = 249;
    TCCR0A = _BV(WGM00) | _BV(WGM01); // (Normal port operation, OC0A disconnected.)  
    TCCR0B = _BV(WGM02) | _BV(CS02) | _BV(CS01);  //  Fast PWM mode, input T0 pin D4 (External clock source on T0 pin. Clock on falling edge.)
    
    //4 msec - timer2
    OCR2A =249;
    OCR2B = 125;  
    TCCR2A = _BV(COM2B1) | _BV(COM2B0) | _BV(WGM21) | _BV(WGM20);  //output B in phase, fast PWM mode (Set OC0B on compare match, clear OC0B at BOTTOM, (inverting mode))
    TCCR2B = _BV(WGM22) | _BV(CS22) | _BV(CS21); // set prescaler to 256 and start the timer
    pinMode(3, OUTPUT);
    
    //  counter input T1 pin D5
    OCR1A = 32767;   //32768 counts
    TCCR1A = _BV(WGM10) | _BV(WGM11); //   Fast PWM Mode (Normal port operation, OC1A/OC1B disconnected)
    TCCR1B = _BV(WGM12) | _BV(WGM13) | _BV(CS12) | _BV(CS11); //input pin D5 (External clock source on T1 pin. Clock on falling edge.)
    
    lcd.setCursor(7, 0); // top middle
    lcd.print("hi");

}

  
  // the loop function runs over and over again forever
void loop() {

      //wait for 1-sec
    TIFR0= _BV(OCF0A);
    while(!(TIFR0 & (1<<OCF0A)));

    
    TIFR1 = _BV(OCF1A);    //reset TMR1 overflow flag
    OCR1A = 32767; 
    TCNT1=0;
    overF=0;
    TIFR0= _BV(OCF0A);
    TCNT2=0;
    TCNT0=0;
      //start the count
    while(!(TIFR0 & (1<<OCF0A))){
    if(TIFR1 & (1<<OCF1A)) {++overF; TIFR1 = _BV(OCF1A);}   //overflow 
    }
        //count end
    freq = (unsigned long)TCNT1 + ((unsigned long)overF * 32768);
    period = 1000000 / (double)freq;
    if(freq == 0){period=0;}
    overF = 0;
    lcd.clear();
    lcd.setCursor(3, 0); // top line
    lcd.print(freq,DEC);
    lcd.setCursor(11, 0);
    lcd.print("Hz");
    lcd.setCursor(3, 1);
    lcd.print(period,DEC);
    lcd.setCursor(10, 1);
    lcd.print(" uS   ");
}

 

 

Source of Arduino Code: An Arduino Uno 6 MHz Frequency Counter with LCD

 

Test Instrumentation

  • Sign in to reply
  • mahmood.hassan
    mahmood.hassan over 6 years ago in reply to DAB

    Thanks for the appreciation.

    Only bottle neck in this design is comparator. Already Tested in proteous and its working perfectly.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • DAB
    DAB over 6 years ago

    Nice start.

     

    I will be looking forward to see your design evolve.

     

    DAB

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube