Pi-Blaster: Improved Software PWM

Pi-Blaster: Improved Software PWM

What is Pulse-Width Modulation?

Pulse-width modulation (PWM), or pulse-duration modulation (PDM), is a modulation technique that controls the width of the pulse, formally the pulse duration, based on modulator signal information. (More information on Wikipedia)

PWM on the Raspberry Pi

Without an external DAC circuit, getting PWM signals out of multiple pins on the Raspberry Pi can only be achieved by software (thus, the terms software pwm / soft pwm / bitbanging), switching the pins high and low at precisely controlled time intervals.

While having its own limitations, this technique might prove itself satisfactory for projects that do not require perfect signals (i.e. servos). But even then, in a situation where time is of the essence, any piece of software of that kind becomes useless in the face of Pi’s limited CPU resources. As this solution would run simultaneously along with other processes, one cannot entirely rely on the clock cycles needed to switch the state of the pins at exactly the right time.

The Pi-Blaster Project

And this is where Thomas Sarlandie‘s excellent Pi-Blaster comes into play. Just like it’s ancestor, ServoBlaster, it cleverly offloads the task of switching the pins to the direct memory access (DMA) controller. The technique used is extremely efficient: does not use the CPU and gives very stable pulses. I would call it a hybrid PWM solution since it’s basically software that uses some other hardware to get the job done.

Installation

sudo apt-get install autoconf
./autogen.sh
./configure
make

To start pi-blaster and have it relaunched automatically on every reboot:

sudo make install

If you ever need to stop pi-blaster and prevent it from starting automatically on the next reboot:

sudo make uninstall

How to use

Pi-Blaster creates a special FIFO file in /dev/pi-blaster. Any application on your Raspberry Pi can write to it (this means that only pi-blaster needs to be root, your application can run as a normal user).

Important: when using pi-blaster, the GPIO pins you send to it are configured as output.

To set the value of a PIN, you write a command to /dev/pi-blaster in the form <GPIOPinName>=<value> where <value> must be a number between 0 and 1 (included).

For a short example, this is how we set GPIO pin 17 to a PWM of 70%:

echo "17=0.7" > /dev/pi-blaster

More information on the available options, usage examples and other articles is available on the Pi-Blaster project page on github. I’m sure the entire RPi community shares my endless gratitude towards the guys who made it all possible, Thomas Sarlandie and Richard Hirst.

Advertisements

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