I've been researching for the past hour, but can't find any solid info on reducing the Arduino PWM frequency down super low, like 5-10Hz for instance. I don't know enough about the nitty gritty of the timers etc.
I just need to run an Arduino Nano output pin at 5-10Hz and don't know the best way to go about it.
Does anyone have any code they could share that would do this?
It's purely to test a component that I know runs at 5Hz.
You want to look at the prescaler register for the clock on the timer associated with the pin you want to use. However don’t use Timer 0 as that will screw up a lot of things like the baud rate and delay values.
I am not sure if it will go that slow, look in the data sheet for the chip. The code to alter this register will only be one line long.
Timer 2 (and Timer 0) won't go that slow. They bottom out near 32 Hz. You have to use Timer1.
16 MHz / 5 Hz = 3.2 million counts.
3200000 / 65536 = 48 so you need a prescale greater than 48 to fit TOP into 16 bits.
The prescale choices are 1, 8, 64, 256, and 1024 so we choose 64.
3200000 / 64 = 50000 (TOP+1)
Pick the Fast PWM mode that has TOP in a register: WGM 14: ICR1 or WGM 15: OCR1A.
Then set ICR1 or OCR1A to 49999.
You can use one of the 'Phase Correct' PWM modes but they count half as fast so you have to use half the count to get the same rate: 24999.
I don't think you would need sub microsecond accuracy for a motor driver... but you can still reach 4 microsecond resolution with only using micros(). You don't need to use timers for this.