RTM_TimerCalc helps you explore Arduino Timer Frequency generation for Nano, Uno and Mega2560 boards. Each MCU has Timers you can use to create PWM or variable frequency signals.
However, there are several important problems using these Timers for frequency generation;
- Frequency Gaps -- numerous unreachable frequencies between frequency steps.
- Range Limits -- maximum and minimum frequencies the hardware cannot exceed.
Arduino Timer Frequency Gaps
The above Arduino boards use a standard 16 MHz clock. They can be programmed to use an Internal 8 MHz clock when running below 5 Volts.
Alhough Timer outputs cover a wide range of frequency, one must realize there are unavoidable gaps between those frequencies. These gaps may limit usefulness of an Arduino frequency generation project!
For example, you may wish to generate 12 MHz from your 16 MHz Arduino. Unfortunately, your Arduino Timer can only divide the clock by an Integer value.
Test for Fraction Method
To see if your Arduino has any chance of generating a particular frequency, just divide the Clock by the desired frequency. If you get a fraction, it won't work (at least not without some sort of OFFSET).
- EXAMPLE: If you take 16 MHz and divide by 12 MHz, you get a fraction -- (16 / 12 = 4 / 3 = 1.3...).
So, we can't get a 12 MHz output for a 16 MHz Arduino using a Timer or divider scheme. The closest we can come is 8 MHz (dividing by 2 -- an Integer).duino timers can't divide by fractions! But 8 Mhz is possible (16 / 8 = 2 divider). And 4 MHz is possible (16 / 4 = 4 divider). But (again) 6 MHz is not possible as it requires a fractional divide (16 / 6 = 2.666... fraction problem).
Because of the integer-only divide problem, frequency gaps exist across the entire generation range! That's part of why RTM_TimerCalc tests if the frequency you requested is the same as what was calculated. If any difference exists, you're given a warning and shown the error (called out as OFFSET).
This often means generating a signal close to the desired frequency, but missing by some amount of error. One way around is change the Arduino's clock crystal. But there are other frequency generation problems to watch for.
Arduino Timer Frequency Limits
Since we have a fixed clock, the Timer and Pre-Scaler can only generate a fixed number of frequencies. But Maximum and Minimum output frequency also limit the output range. This is important if you intend your Arduino project to serve as a signal generator.
The highest and lowest frequencies are determined by the Clock, Pre-Scaler and size of the Timer in bits (8-bit VS 16-bit). The 16 bit Timer can generate far more frequencies and reach down to sub-audio range. The 8-bit has much less capability.
Here's a table showing Maximum and Minimum output Frequencies. It covers 8 and 16 bit Timers for Nano, Uno and Mega2560 running at 16 MHz. It also shows periods relative to limit frequencies.
This table reveals the longest and shortest Periods possible. And the highest and lowest Frequencies you can attain (clocking at 16 MHz).
Of course, I'm only looking at output modes you can select using the RTM_TimerCalc application.
Remember, RTM_TimerCalc lets you spot Gap-Frequencies and tells you the amount of OFFSET to expect. Sometimes the error is so small its not an issue. Other times the OFFSET is huge. Its up to you to decide if the numbers make sense for your project.