0

I will like to understand how does Arduino measure a frequency. Also how does my voltmeter can measure a frequency as well. Since I am a beginner on using microcontroler I would read a frequency like this:

void loop()
{
    while(digitalRead(myPin)==HIGH)
    {
       // pin is high
    }
    // measure time using millis (this is the time the pin was high)

    while(digitalRead(myPin)==LOW)
    {
       // pin is low
    }
    // measure time using millis (this is the time the pin was low)

} 

With this code it makes sense to me how to measure a frequency and a duty cycle. The problem with this code is that it will probably be very slow and I it will probably read frequencies up to 100Hz and less.

So my questions are:

  1. How can arduino read frequencies higher than 100Hz? How does it achieve that?

  2. I Googled arduino's frequency and it shows to be 16MHz. That means arudino can read frequencies up to 16Mhz? Ethernet cables specifically CAT5 has a frequency of 100MHz. That means that it sends signals 100,000,000 in one second! How could Arduino send that many signals in one second if I where to buy a Ethernet shield such as this one?

1
  • you can use a digital counter IC to scale down the frequency into something Arduino can handle. Feeding a square wave to an 8-bit counter will make bit 8 oscillate at exactly 1/256th the frequency of the square wave.
    – dandavis
    Commented May 28, 2020 at 20:16

2 Answers 2

3

How can arduino read frequencies higher than 100Hz? How does it achieve that?

Interrupts (if you want to do it in software) or a magical piece of hardware called the Input Compare peripheral that is built into most MCUs.

I Googled arduino's frequency and it shows to be 16MHz. That means arudino can read frequencies up to 16Mhz?

No, that is the speed the CPU operates at. The maximum frequency signal it can read is usually below that (for SPI it's half that, 8MHz, for example).

Ethernet cables specifically CAT5 has a frequency of 100MHz. That means that it sends signals 100,000,000 in one second! How could Arduino send that many signals in one second if I where to buy a Ethernet shield such as this one?

It doesn't. The shield does. The Arduino just tells the shield what to do, and the chip on the shield does it.

1

I would not use millis() to measure the period of a signal unless my program had to deal with extremely low frequencies. And by extremely low I mean below 1 mHz (one millihertz). For frequencies above 1 mHz, the pseudo-code you show would work just as well or better using micros().

As the frequency goes up, you will start to look at more fancy options for measuring it. You could for example count the rising edges from an interrupt and measure the duration of many (not just one) periods with micros(). This should work up to a few hundred kHz. For faster frequencies you can use a hardware timer configured as a counter, and measure the time it takes to overflow. This should be good for frequencies up to almost half the CPU frequency (8 MHz).

And for the Ethernet signals, see Majenko's answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.