Tuesday, March 03, 2015

Measuring frequency with Arduino

by / Wednesday, 02 March 2011 / Published in Arduino

arduino megaThere are many signals that contain necessary information in frequency. For instance, SparkFun's HH100D humidity sensor outputs measurements via frequency signal (although it has i2C interface, those pins are used for reading calibration values. Actual measurements com via FOUT line).

There are two ways to measure a frequency with Arduino:

  • Using FreqCounter library. It can measure frequencies up to several MHz, and is very precise. However, measurement is fixed to digital pin 5. Also it may affect Arduino PWM outputs, increasing their duty cycles. In case you don't use PWM and you need to measure frequency of just one signal, FreqCounter is an excellent choice
  • Using pulseIn() function. The technique I'm suggesting below is free from FreqCounter limitations, so you can use it on any pin. However, it's minimal wave period is 10 uS, with corresponding maximum measurable frequency of 100 kHz

Here you can find documentation for pulseIn() function.

Most people usually take only one measurement with pulseIn() function. In many cases, frequency is not very stable, so we need to calculate an average value over time. Something like 1024 samples will be enough, however, for better results, you may increase the value - up to 32767 samples (you can, certainly, choose any value, but, (for HH100D), there's almost no difference in measured signal frequency for 4096 and 32767 samples. However, 32767-sample measurement takes up to 5 seconds - depending on frequency, of course.)

We will suggest that we have a square wave with 50% duty cycle, and measure the length of the pulse. Then we'll multiply that value for 2 to obtain signal period: (the distance between red lines on the image):


long getFrequency(int pin) {
  #define SAMPLES 4096
  long freq = 0;
  for(unsigned int j=0; j<SAMPLES; j++) freq+= 500000/pulseIn(pin, HIGH, 250000);
  return freq / SAMPLES;

We wait for the pulse to occur in 250 mS. If you are using very slow signals, increase this value as you need. (line 4)

I use value of 500 000 uS instead of 1 000 000 uS (= 1 second), because I want to get signal's period (T), not pulse length(tpulse).
With duty cycle of 50%, so T = 2*tpulse.

Do not forget to configure your pin as input!

Read 4712 times Last modified on Monday, 05 August 2013 16:00

Site still under construction

I will add more sections in the future

  • My works / Photogallery
  • More Joomla!-related articles
  • Personal blog
  • Arduino articles from the old site (done)

Latest Posts

Dealing with nginx “The plain HTTP request was sent to HTTPS port” error

Dealing with nginx “The plain HTTP reque…

In some cases you have to run multiple services on... Read more

21-09-2014 in Linux/BSD

Getting Argyll/dispcalGui working under Windows 8.1 64-bit

Getting Argyll/dispcalGui working under …

If you upgrade to windows 8.1 64-bit, you may find... Read more

03-01-2014 in Blog

How to transfer or mirror your Joomla! / Wordpress / Drupal website from one server to another in one click

How to transfer or mirror your Joomla! /…

The following script may help you to transfer or mirror... Read more

13-10-2013 in Linux/BSD

Setting up your multi-user hosting VPS server (Apache): A secure way to run several Joomla/Worpress/Drupal sites

Setting up your multi-user hosting VPS s…

Keywords: Apache MPM ITK PHP MySQL VPS CentOS In this article... Read more

13-10-2013 in Linux/BSD

How it works: DS18B20 and Arduino

How it works: DS18B20 and Arduino

There are many Arduino sketches on the Web that provide... Read more

05-08-2013 in Arduino

Premium Joomla templates

Premium Joomla templates

Two years ago I discovered a site with tons of... Read more

27-03-2013 in Recommendations

Interfacing HH10D with Arduino

Interfacing HH10D with Arduino

HH10D is capacitive-type relative air humidity sensor. It is cheap... Read more

17-06-2012 in Arduino

Arduino and watchdog timer

Arduino and watchdog timer

Have you ever encountered situations when your Arduino-based device... Read more

16-06-2012 in Arduino

Measuring frequency with Arduino

Measuring frequency with Arduino

There are many signals that contain necessary information in frequency.... Read more

02-03-2011 in Arduino

Reading iButton with Arduino

Reading iButton with Arduino

We all are familiar with iButton devices - a small... Read more

03-12-2010 in Arduino

Prev Next

Scroll to top