I'm controlling a car instrument panel with Arduino using the tone() function, however with Arduino Uno's minimum 31Hz frequency I can only set the speedometer at ~12mph or more. I'd like to be able to set the speed from 0, which would require around 10Hz I believe. Are there any code tricks I could use? Maybe I should use hardware timer, or a different board?
For this, I would consider using Timer 1 in mode 14 (fast PWM, with TOP value controlled by ICR1), with the prescaler set to 64. The timer would then be clocked at 250 kHz, giving you the following specs:
- timer frequency: 250 kHz (time resolution = 4 µs)
- minimum frequency: 3.81 Hz (exact period = 218 µs = 262,144 ms)
- maximum frequency: 125 kHz (period = 8 µs)
As the time resolution is constant, the frequency resolution worsens as you increase the frequency. At 400 Hz the resolution is about 0.16%, or 0.64 Hz.
This is not the only possible solution. Toggle mode would work also, and also other prescaler values, which would give different frequency ranges and resolutions.
Check the datasheet to see how to set the configuration bits (basically: non-inverting PWM on either OC1A or OC1B, waveform generation mode = 14, prescaler = 64). You may be able to find libraries to do this if you prefer that approach, I have not checked this possibility.