# Lesson 6--Microduino Simple Frequency Meter (serial monitor)

 Language: English  • 中文

## Objective

This lesson will teach you measure the frequency of a sound by Microduino. Want to measure the frequency of a sound or duty cycle, but no frequency meter? We can use Microduino to finish this work.

Features: 1. Measured the signal of 20 ~ 20 KHZ, error is bigger and bigger once out of this range.

2. Error is roughly + 5%. Just to play.

3. Output results are: frequency, duty cycle, cycle, high level, low level of time.

4. Only measure single frequency, can't used for complex waveform.

## Program

```/*
Arduino Frequency meter
Ansifa
2013/1/5
*/
int divider[6] = {0, 1, 8, 64, 256, 1024};
int prescaler = 5;
double count = 0;
double middle = 0;
char x = 0;
ISR(TIMER1_OVF_vect)
{
if (prescaler < 4)
{
prescaler++;
}
}
void interrupt()
{
if (!x)
{
count = TCNT1;
TCNT1 = 0x000;
TCCR1B = prescaler;
attachInterrupt(0, interrupt, FALLING);
}
else
{
middle = TCNT1;
attachInterrupt(0, interrupt, RISING);
}
x = ~x;
}
void setup()
{
Serial.begin(57600);
TIMSK1 = 0x01;
TCCR1A = 0x00;
attachInterrupt(0, interrupt, RISING);
}
void loop()
{
Serial.print("Freq: ");
Serial.print(16000000.0 / divider[prescaler] / count);
Serial.print(" Hz\t\tDuty: ");
Serial.print(middle / count * 100);
Serial.print(" %\t\tPeriod: ");
Serial.print(0.0000625 * divider[prescaler]*count);
Serial.print(" ms\t\tH-time: ");
Serial.print(0.0000625 * divider[prescaler]*middle);
Serial.print(" ms\t\tL-time: ");
Serial.print(0.0000625 * divider[prescaler]*(count - middle));
Serial.println(" ms");
if (prescaler > 1)
{
prescaler--;
delay(200);
}
delay(100);
}```

## Debug

Open the serial monitor, set the baud rate to 57600, display the result. Because there is no any anti interference measures, please ignore particularly large deviation result.

You also can extend this circuit performance:

• If the amplitude is not enough, you can use the op-amp or triode to preamplifier;
• Measuring frequency is not high, can use 74HEF4060 frequency division. 74HEF4060 can support up to 2^14=16384 magnitude frequency division, theoretical range can be 16384 times. But is due to the 74HEF4060 performance limitation, increase the input frequency to 20 MHZ. But after frequency division, it only can see the frequency and waveform measurement is less than the duty ratio. And be careful not to use the CD4060, it is low version.

## Result

Open the serial monitor, set the baud rate to 57600, observe the result