RedditPP Mascot

RedditPP

r/arduino • … • u/TomvdZ

PWM for Persistence of Vision project

I would like to build a Hard Drive POV clock (where you open up an obsolete hard drive, cut a slot in the platters, and then, in close synchronization with the rotation of the platters, flash LEDs behind the platters to create the illusion of 3 clock hands or other patterns).

I would like to be able to drive the (RGB) LEDs with PWM to be able to get a wide range of colors. However, I'm not sure how well PWM will play together with the fast timing. I plan on using a 16MHz AtMega328.

**Option 1**: Assuming I want an angular resolution of 1 degree and the drive spins at 3600rpm, one "pixel" has to be rendered in 46 microseconds. If I just use the hardware PWM, I would get 2.875 periods of the duty cycle in that time (assuming the highest PWM frequency of 62.5kHz).

- How does PWM handle having the duty cycle changed in the middle of the period? Does it reset back to the beginning of the period? What if the output is currently on, and I change to a lower duty cycle - does it still turn off or only on the next period (when the timer register matches the duty cycle again)? Will this give any artifacts?

- This would eat up 2 of my timers (since I need 3 PWM) channels, and I'd need to use the 3rd timer for actually drawing the pixels (which needs a different prescaler since 46 microseconds is too long for a prescaler of 1 on an 8-bit timer). But I would also like one additional timer for something else, so this is not ideal.

**Option 2**: Bit-banged PWM. Divide each "pixel" up into (let's say) 10 segments and do some kind of dithering using only a single timer interrupt.

- This would mean each timer interrupt would be spaced only 74 clock cycles apart. That is very (too?) fast, considering the interrupt needs to do quite some work (the dithering) and I still need some CPU time for other tasks.

- How good would this look? The resolution of the brightness values would be limited to 11 different values but you could "evenly" space the pulses out over the interval.

**Option 3**: Frankensteining PWM and timer interrupts. Configure a timer with a prescaler of 8 and set compare match to 92 (to get the 46 microsecond spacing). Use timer interrupts for drawing the pixels, but also use the same timer to get a PWM channel.

- This would require using a timer in both (fast) PWM and also for generating interrupts. Is this even possible? Is it correct that a timer can either generate 2 PWM signals, 1 PWM and 1 timer or 2 timers?

- Second timer needed for additional channels, but third one is not needed.

- PWM resolution of 92 which is good enough, but only one period per pixel.

Could somebody explain which of these options would work best (if at all) or suggest something else? I'm open to suggestions for other, more capable MCU's, but I enjoy the challenge of achieving something with limited resources.

— 0