Frequency generator

Can arduino generate the frequency with the resolution of 2 decimal places? i.e. 100.12 Hz, 98.23Hz, 1.92Hz...

recently I need to generate a frequency to allow a stepper motor to move accordingly. But to ensure the stepper motor to functions normally, frequency must be generated , and it must be of 2 decimal places.

Can Arduino do that? Or is there any specific product that may generate frequency resolution as such?

• There are better choices (processors with faster clocks, programmable oscillators, FPGAs) but it may be possible via use of interpolation or microstepping to get close enough to the desired behavior to work in your application. This will not be a simple project, so the usual reasons for picking an Arduiono may not apply, especially as some of the other solutions will more easily get you close to this goal. Aug 3 '16 at 3:33
• Do you need accuracy in addition to resolution? The answers so far show you can probably get the resolution you want, but if you also want accuracy, you will have to choose an Arduino clocked off a quartz (e.g. a Leonardo) rather than a crystal resonator (like the Uno). Aug 3 '16 at 15:15
• What frequency? There is a big difference between 123456.22 and 1.22 in terms of significant digits, although they both have 2 decimal places. Aug 4 '16 at 7:36
• @EdgarBonet - good point. I am 2.5678432 meters tall. That is a very precise measurement. It is also inaccurate. :) Aug 4 '16 at 7:38
• thank you Chris, Edgar & Nick. Awesome respond. Edgar: yes I need accuracy as well. Nick: there are several different frequencies I need to set which all of them need to be set at 2 dp. Wow, with that height u can be an NBA player hehe. Aug 4 '16 at 8:09

The standard Arduino Uno is clocked at a nominal 16MHz, and has three timers: Timer0, 1 and 2. Timer1 uses a 16 bit counter (the others are 8 bit), and can be prescaled by 1, 8, 64, 256 and 1024. That lets you do very precise timings (assuming an accurate clock!), simply by choosing different prescaler values and the relevant values.

Using your example rates, and assuming that you want a square wave (hence the "Halved" column), here are the calculations:

Hertz Prescaler  Calculated      Halved  Actual Hz
100.12         8  19976.02876548   9,988  100.120144173...
98.23         8  20360.3787      10,180   98.231827112...
1.92       256  32552.08333333  16,276    1.9200049...

Note that to do this, you need to program the Timer yourself - the standard Arduino library sets Timer1 to a default setting, which won't do what you want.

• thanks for your suggestion. Where can I refer on how to program the Timer myself? Aug 4 '16 at 8:15
• The definitive work is the Atmel datasheet: atmel.com/images/… . You'll want to read chapters 16 and 17. Aug 5 '16 at 2:53

I happened to do a similar analysis for Timer 1 on 328 / 32U4 clocked at 16MHz the other day and produced this plot: This shows the frequency resolution as a proportion of frequency in red assuming optimal prescaling (the jumps are where prescale changes). The blue markers are realized frequency errors at frequencies drawn at the E12 series in magnitudes from 0 to 5. Unsurprisinly these are half or better than the resolution.

As a conclusion, you get a relative error of around 5e-5 for frequencies up to about 1kHz. After that you loose an order of magnitude per order of magnitude (which of course makes perfect sense).

The other answers about the way you can configure the timers are very good, but Edgar Bonet's comment goes to the heart of the matter. Do you need accuracy?

Your example of 98.23Hz suggests that you want 98.23Hz and not 98.22Hz or 98.24Hz. That means therefore that you require an accuracy of around 1 in 10000 (0.01%).

Can arduino generate the frequency ...

You haven't said which Arduino, but assuming one of the basic models, like the Uno, they come with a resonator which will have an accuracy of around 1%.

If the system clock is only good for 1% accuracy there isn't much point worrying about trying to get a frequency within 0.01%.

• thanks and yeah I forgot to mntion the type. Other than the Uno, which Arduino might give the precise and accurate setting for 2 dp? Aug 4 '16 at 8:23