How To: Breathing LED in Python for RaspberryPi


Because the display of my product serves to represent people rather than time itself, I decided that rather than have the display stay a solid lit or unlit colour that it was more desirable to have a “Breathing” effect LED array instead.

There have been various iterations of this for several devices and in many programming languages such as this one coded in Ruby for Raspberry Pi and this one, coded in C for Arduino. However to my knowledge, as of this week there were no such guides out there for users coding in the language of Python (an extremely versatile language used by the Pi) ; so I’ve put together this quick step-by-step guide to show you how to create such an effect.

I will assume that you have a basic knowledge of the Raspberry Pi and how it works; however if you have any problems feel free to contact me and I will try my best to help you out.

First off you will need the library that tells the Pi how to use the GPIO (General Purpose Input/Output) pins on the board. For this I’ve used a library called “WiringPi” which is available for Python with installation instructions  here

Once you have installed the library successfully through LXterminal you are ready to write your code.

Go ahead and open the “Idle3” program. As with any library useage it must be imported into your program, in python, we use the code “import wiringpi”. This means that your program will now know how to utilise the GPIO pins.

While the WiringPi library has more uses than used here I will solely be writing about its current usage to myself;

The code I have used to command the pin is “io.pwmWrite(1,value)”. If like me you are new to this language it is helpful to break it down so you know exactly how it works. In this instruction it specifies the Input/output pin by io, then PWM which states the mode in which the pin is going to be used (rather than digital or analog) – Using it as a PWM (Pulse Wave Modulator) allows the pin to vary between on and off rapidly to imitate the wave of an analog signal which gives the fading effect. The write command tells the program that we are sending instructions or data rather than receiving. The first number inside the bracket, in our case 1 specifies which pin on the board we are using. Pin1 is chosen in particular because it is the only pin on the RaspberryPi that can be used as a PWM. The second number in the bracket, or in our case here, “value” states a value between 0-1024 which specifies the intensity of the light. The use of “value” instead of a number in this case simply means that it uses a variable number which makes it easier for us to program loops.

Once you have understood this; it is easy to make a basic fading LED script, here’s one I made earlier.

This script uses a simple increasing and decreasing loop and a set of variables. The “while count” controls how long the loop runs – For every incrimental increase or decrease, 1 is added to the counter – so when the specified limit is reached – the process stops. We’ve already mentioned the increment, this is the amount that the intensity increases or decreases per loop of the program. Not that there are also “time.sleep” value’s included in the code; these simply make the delays between each increase or decrease longer.

Now that you understand the variables and their uses you should be able to manipulate the script to give you varying effects. Note it is also possible to create a further loop which varies both the time and intensity variables so you will get a more varied loop or effect.

Once you have your code in the editor, go ahead and save it as a python (.py) file.

Now for the hardware. This is real simple; assuming that you have the GPIO cobbler, set the cobbler into your breadboard. You can find the pinout for the cobbler here. Take one wire from the ground of your header and run it on the ground rail, take another wire from pin 1 (labelled as #18) on the cobbler and run it to a part of the breadboard from which you can run a resistor. Finally complete the circuit with an LED. It should end up looking like this:


So now you are ready to go. Start up LXterminal, navigate to where you saved the document using the Change Directory (cd) command, then execute by using “sudo python (yourdoc).py”

And if you’ve followed correctly; you should see the LED begin to breathe like this:


Leave a Reply

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

You are commenting using your 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