THE OBJECTIVE

Our objective is to build a frequency counter that can identify frequencies up to 1MHz.

pulseIn()

pulseIn() reads a pulse on a pin and waits for a change from HIGH to LOW or LOW to HIGH. This works with times that are between 10μs and 180s. You can provide either two or three parameters:

pulsIn(pin, value)
pulseIn(pin, value, timeout)

pin: The Arduino pin that you want to read the pulse from.
value: If you want to read a LOW or HIGH pulse.
timeout: The time, in μs that you want to wait for the pulse to start. The default is 1s.

pulseIn() will return the length of the pulse in μs. 0 will be returned if no pulse started before the timeout.

THE CODE

First I created a variable called pin, with the data type of int. I then created a second variable called duration, with the data type of unsigned long.

int pin = 7;
unsigned long duration;

void loop() {
  duration = pulseIn(pin, HIGH);
  Serial.println(duration);
}

In the setup() I started the serial monitor, with a baud rate of 9600. I then set the contents of the pin variable (pin 7) to be an INPUT, by using pinMode.

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

In the loop() I then set the duration variable to pulseIn(pin, HIGH). I then printed the contents of the duration variable to the monitor.

void loop() {
  duration = pulseIn(pin, HIGH);
  Serial.println(duration);
}

Improvements

One way that the frequency counter could be improved is by adding an LCD. This would allow the result to be read from the LCD and make the device portable.

A further improvement would be to adapt the device to be able to read frequencies greater than 1MHz. This could be achieved by using a divider or prescaler. A prescaler is a circuit that can take a high frequency and reduce it to a lower frequency. This is accomplished by integer division. This allows a higher frequency to be used and divided to a smaller one to be processed and measured.