Better PWM on the Raspberry Pi

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.

Comments

  1. icanhazadd says:

    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)

  2. h1ghz0r says:

    i was planing to do the same, using the Stellaris LaunchPad uDMA. it’s great to know that is really viable.

  3. jacob says:

    Is it possible to get PPM (For RC stuff) output with it?

  4. +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.

  5. Trav says:

    Ha!, I got that “Land war in Asia” reference.

  6. James says:

    PWM – Pi Will Modulate

  7. Maxzillian says:

    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.

  8. noufal says:

    How many hardware PWM pins are there for RPI, and Is it possible to generate a PWM of frequncy above 25kHz with Rasberry Pi?

  9. Scott Barnes says:

    A little late, but has anybody got pi-blaster to work on the Pi with CYCLE_TIME_US defined as anything other than 10000?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 92,391 other followers