[Thomas] wanted to play around with a few high-power LEDs and a RaspberryPi. LED controllers usually require some form of PWM to change the brightness of a LED, and unfortunately the Pi only has one PWM pin. [Thomas] could have gotten around this with a custom chip or even an Arduino hanging off the Pi’s USB port. He opted to go with software-based PWM, and did so in a way that is far superior to bit banging a pin.
Conventional wisdom says PWM without a real-time operating system is dumb – right up there with starting a land war in Asia. Turning a pin on and off in a while loop will eat up all the processor power in the Pi, so [Thomas] looked for a better way to do things. He came across the ServoBlaster project by [Richard Hirst] that creates pulses of different lengths by playing with direct memory access; [Richard] created a circular buffer that is read every 10μs. With 2000 values in the buffer, he can control eight different pins with very little impact on CPU usage.
For [Thomas], though, [Richard]‘s project wasn’t enough. It was originally written for servos and is only able to drive PWM pins up to about 12%. A quick rewrite of [Richard]‘s code allowed [Thomas] to control eight pins with PWM varying from 0% to 100% – and be able to do other things with his Pi in the process.
[Thomas] now has a 40 Watt RGB LED powered by a Raspberry Pi burned into his retina, and the satisfaction of a really clever way of giving the Pi more PWM pins.