This week on WeTalkNerdy.tv, CISPA and Futurama both got cancelled. I explain how to stop those nagging OTA Android update messages. And finally, in part three of my introduction to the Raspberry Pi, I show you how to control a circuit using the Pi’s GPIO pins using Python.
Show Notes:This week, I presented part three of my Introduction to the Raspberry Pi. Below you can find the Python Script I reference and a complete transcript.
blink50.py Script:# blink50.py by Rahul Kar
import RPi.GPIO as GPIO
# blinking function
# to use Raspberry Pi board pin numbers
# set up GPIO output channel
# blink GPIO17 50 times
for i in range(0,50):
Show Transcript:How To
And now it’s time for part three of my series on the Raspberry Pi. Today I’m going to show you how to control an LED with the Pi. Now, I realize that this is not an amazing project or anything. But remember the Raspberry Pi was developed as an educational platform. This example project is a good first step that nearly anyone can do. Even if you have no interest in electronics or programming you may find it enlightening. And when you see how easy it is, perhaps you may be inspired to try a project of your own.
To begin, we need our Raspberry Pi all set up and ready to go as I explained in Part two of this series. I’m going to proceed based on the assumption that you’ve already installed Raspbian wheezy on your Pi. Now, you’re naturally going to need an LED, some wire, a resistor, a soldering iron and/or a bread board for making the circuit. A bread board is a simple and inexpensive tool for creating, testing and prototyping circuits. You can purchase all these parts for just a few dollars at a store like Radio Shack or from an online retailer. If you don’t have a breadboard, you can soldier the wires together or even connect them with tape. In this example, I’m going to use a breadboard because it makes things really easy.
On a bread board, the horizontal holes are connected together on either side of a central dividing line. while the holes on the far left and right are connected vertically. This is for powering the circuit and its called a bus strip. Making a circuit is a simple as pushing a wire into the appropriate hole.
Step one is to take a look at the Raspberry Pi and identify what are called the GPIO pins. GPIO stands for General Purpose Input/Output. These pins are what we can use to get input from other devices like thermal sensors for example and we can use them to switch on and off our LED. There are two sets of 13 pins, 26 pins in all. With the SD card at the top and the USB connector on the bottom, as you look down on the Pi, the odd numbered pins are on the left and the even numbered pins are on the right. This pin diagram comes courtesy of Matt from the raspberry pi spy web site.
Unfortunately, the pins are organized in a somewhat confusing fashion. For the sake of simplicity I’m going to refer to the pins by number. For this example, we will be using pins 9, 11 and 17.
Now, If you have an old IDE cable around, you could connect that to the pins. But, in order to make things as obvious as possible I went ahead and soldiered wires to those pins instead. Normally, you would want to use different colored wires, but since I only had green wire, I taped labels to each wire so I wouldn’t get them confused. This is important, because if you connect the wrong wires together, you could short out and possibly ruin your Raspberry Pi.
Before we try to write a program to turn on the LED, I want to start by setting up a test circuit. My goal here is to wire up an LED directly to the Raspberry Pi and make sure that it comes on. Once I’ve got that working properly, I’ll move on to the next step.
If we look at our pin diagram, I can see that pin 9 is our ground wire. If it helps, you can think of that as minus. And, Pin 17 is our +3 volts wire. To start, I’m going to connect those wires to the bread board and see if I can get the LED to glow. But, I have a slight problem. My LED runs off of 2.1 volts, not 3.3, So in order reduce the voltage, I’m going to use a resistor. A resistor, like the name implies, resists the flow of electricity. Resistance is measured in Ohms. The resistor I have here is a 470 Ohm resistor which is actually a little too much, but it’ll still work for our purposes. All you really need to know is that It’s important to use a resistor with each LED in your circuit. You can use anything from 100 Ohms to 470 Ohms and it should work just fine. Ok, moving on…
So, all I have to do is plug the wires into the breadboard and when I turn on the Raspberry Pi, the LED should start to glow. If it doesn’t, you might have the LED in backwards. Unlike the resistor, the LED needs to be hooked up the right way around, otherwise it wont work.
Ok, now we have a working circuit! This is great, but we want to be able to control the LED, not just have it come on when we turn on the Pi. So, we need to change the circuit slightly. Instead of connecting the LED to pins 9 and 17, we want to connect it to pins 9 and 11. Pin 11 is the pin we are going to program. So, I’m going to unhook 17 and put 11 in its place. Now, when I turn on the Raspberry Pi, the LED does not come on. But now, we can write a simple python program to turn it on. Python is a powerful programming language that is included on the Pi.
Before we can write that program, we need to install some python development software. Don’t worry, it’s very easy. Start up your Raspberry Pi, open a terminal window and type:
sudo apt-get install python-dev
Let me explain what this does. Sudo means run this next command as the super user. SU for super user and do as in do this. SUDO. APT is a program called the advanced packaging tool and you’re telling it to get some software and install it. In this case, we want the python development tools. APT is smart enough to know what to do, all you have to do is tell it what you want. Oh course, you have to be connected to the internet for APT to find and download the software for you.
Once you have installed the python developer tools, you need get your hands on the Raspberry Pi GPIO library. This is just extra software that Python needs in order to be able to make use of the GPIO pins. There’s no package for this, you need download it and install it manually.
Simply start up Midori, the Raspberry Pi web broswer and goto this URL: https://pypi.python.org/pypi/RPi.GPIO
Click the green button to download the library. Midori will download a file called: RPi.GPIO 0.5.2a.tar.gz. By the way, this is called a tar ball and it’s the Linux equvalant of a zip file. And just like a zip file, you need to extract the contents. You can use a terminal window or you can use the file browser. Either way, once you have extracted the files from the tar ball, use the command:
sudo python setup.py install
This command tells python to install the library files.
Great! Now we are ready to program! I’m a novice Python programmer, so I have a program here written by Rahul Kar from the Raspberry Pi blog. It makes the LED blink fifty times. It’s a really simple program, It’s only 17 lines, so lets take a look:
The first two lines import modules that we need to make the program work. Lines 3 through 9 define a function called blink. Line 5 sets the GPIO pin to HIGH. In other words, turns on the current. Line 6 tells the program to sleep for 1 second. Line 7 sets the GPIO pin to low, turning off the current and then line 8 makes the program sleep again for one second. So the blink function turns the LED on and off once.
At the bottom of the program there’s a loop. Line 15-16 says to python to blink pin 11 fifty times. And that’s pretty much it. Let me show you how to copy this program and run it on your Raspberry Pi. First open a terminal window on your raspberry Pi. then type:
sudo nano blink50.py
Nano is a text editing program that we can use to save the python program. Next, copy the program from either my show notes on the WeTalkNerdy.tv web site or the raspberry Pi blog. Right click in the terminal window and chose “Paste” from the popup menu. This should paste a copy of the program. Then, press control-x to exit and type “Y” to save the program. If you type LS in the terminal window, you should now see a file called blink50.py in your directory. Finally, to run the program simply type:
sudo python blink50.py
And if all goes well, the LED attached to your Raspberry Pi should blink 50 times and then stop. If you want to experiment, you can try changing to program to make the LED blink faster or slower. Don’t be afraid to play around and experiment with it.
Congratulations, you’ve completed your first Raspberry Pi project. As I said at the outset, this is a simple example for beginners. Blinking an LED is not earth shattering, but this project is meant to open your eyes and get you started. From here, you could replace the LED with a relay for example. Then you could use the Pi to turn on/off other devices at certain times or when certain inputs are recieved. There’s a lot more that you can do with the Raspberry Pi and I hope I’ve started you off in a good direction.