Better PWM On The Raspberry Pi


[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.

13 thoughts on “Better PWM On The Raspberry Pi

  1. Neat-O! I guess i’ll have to hack this hack though, so that the IR reciever still works. (pi-blaster sets all pins to outputs causing your other shiny things to stop working)

    1. If it’s just continually copying data from a buffer to the output pins, then surely if you put the right data in, then you could do PPM or anything that involves continual pulses. So just from reading the description, yes.

  2. +1000 for badass hacks.
    This is right up there with someone’s “hack the HDMI for RGV laser scanning” hack.
    Someone should start a specific repository so people can download every RPi hack ever written in one easy to copy .tar etc and help conserve author’s bandwidth.

  3. Nice!

    Reminds me of a method I worked up for a project at work with an Arduino. I needed a way to generate 8 different frequencies on 8 different pins. Unfortunately the Arduino can only drive 2 or 3 different frequencies at once using the timers.

    Ultimately I had it perform a math function to determine when to turn pins on and off. From a frequency it would calculate the period and then divide the current microsecond time on the controller by the period. A pin was switched high if the result was odd and low if even. Ultimately this allowed me to drive up to about 280-300 Hz while still being able to perform other functions on the Arduino. The waveform isn’t perfect, but it was good enough for an implement controller to think it was receiving signals from inductive sensors.

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.