Once you have completed the initial setup of the Pi, it’s time to play a little. So…let’s start playing with GPIO. For this project, we will need the following:
- Breadboard (can be found here at www.jameco.com for $2.95)
- Wire (22 AWG Solid spools work fine, like the ones here)
- Gumdrop LED’s, like those found here.
- 1kΩ resistor (like the ones found here).
Please note, although I have ordered from Jameco in the past and not had problems, there may be cheaper or better suppliers,
What is GPIO?
GPIO stands for General Purpose Input / Output. They are pins on your pi which, depending on how they are configured, can be used either to bring information into the Pi, or send information out. The outputs are at a logical level of 3.3V, and are designed to support very low current. Although the GPIO pins can support a small current draw, such as the current necessary to run an LED, they should not be used to power a motor or other high current devices!
Basics – Breadboard (Prototype Board)
Sometimes called a solderless prototype board, a breadboard is an easy way to begin playing with circuits without the need to solder.
The breadboard that I’m using looks like this:
A breadboard is arranged into rows and columns. Columns are connected, where holes in the same row are isolated (not connected). Typically, breadboards may have a center gap. Columns on either side of this gap will not be connected. Also, some breadboards have common rows at top or bottom, which are separated from the grid. These rows will typically be connected, to provide you with a power and ground rail, if you so choose.
In the case of this board, the connections are shown below:
As you can see, all holes in the columns on the top are connected, and so are the holes in the columns on the bottom.
Basics – Wire
To start working with a breadboard, all you need are some short pieces of wire. It is helpful to have plain wire with the ends stripped, like this:
and some jumpers to connect to the Raspberry Pi headers. I personally like to use headers like these:
which can be found at MicroCenter or online. They make it easy to start playing.
Basics – Resistor
A resistor is one of the most basic electrical components. A resistor makes it harder for electricity to flow around a circuit. The current in a circuit will be a function of the voltage and the overall resistance in the circuit. The relationship between these are defined by Ohm’s Law:
- V = Voltage (Measured in Volts)
- I = Current (Measured in Amps)
- R = Resistance in the circuit (measured in Ohms)
A resistor typically has four colored bands, which indicate its value. The first two bands represent numbers, the third is a “multiplier” and the fourth is a tolerance.
The value of the resistor can be determined using the chart below:
A resistor looks like this:
Using the chart, we calculate the value of the resistor as follows:
Band 1 – Brown = 1 Band 2 – Black = 0 Band 3 – Red Band 4 – Gold
Therefore, are value is 1-0 or 10 x 100 = 1,000Ω or 1kΩ. The gold band indicates that the resistance will be accurate within 10%.
Basics – LED
An LED is a Light Emitting Diode. It is Light Emitting, which means that it will give off light when current is flowing through it. It is a “Diode” which means that it will allow electricity to flow only one way.
It looks like this:
Because these are Diodes, the polarity matters. The longer lead on the LED typically represents the positive terminal. The shorter is the negative terminal. In a circuit, it will be represented by the following symbol:
You need to be careful with LEDs. They have a maximum current they will support and provide little to no resistance in the circuit. Therefore, you should always use an LED in series with a current limiting resistor
Put it together
Our basic circuit will look like this:
You will notice that it is connected to a 3.3v input, which will be from the GPIO pin, has a 1kΩ resistor, an LED, and is connected to a ground pin on the Pi. The current will flow from the positive +3.3v to the GND, through both the resistor and the LED.
When hooked up on the breadboard, it looks something like this:
All of the following commands will be done either from a terminal or through ssh. If you do not know how to open a terminal window, please see the instructions at the end of the Raspberry Pi configuration tutorial located at: http://www.barryhubbard.com/raspberry-pi/howto-raspberry-pi-initial-setup/
An easy way to get started with GPIO programming is through the use of python scripts. For our purposes, let’s create a scripts directory. This can be done with the following command:
$ mkdir scripts
Next, we need to change into the scripts directory by executing the following:
$ cd scripts
Creating our first script
One of the easiest ways to create a text file is with the program
nano which is pre-installed on Raspbian and is available from the terminal window, either locally or through ssh. Information on other command line utilities is available at http://www.barryhubbard.com/linux/useful-linux-commands-working-with-text/
To create or edit a file, we can simply type:
$ nano led.py
If “led.py” doesn’t exist, it will be created, otherwise, the file will be opened for editing.
The first thing to know about python programming are Comments. Any line that starts with a ‘#’ indicates a comment and the entire line will be ignored by the python processor, so if I have a line:
# Setup the GPIO ports
It’s not doing anything other than providing information to us…not the Pi!
So, let’s write a program to turn the LED on for 5 seconds.
The first part of our code must bring the necessary libraries into python for us to use the GPIO pins and timing features. We do this with the following code:
import RPi.GPIO as GPIO import time
Next we need to set our GPIO mode so that we use the pins as they appear on the Pi, and not by GPIO name. We also want to tell the Pi that we will be using Pin 12 (GPIO 18) as an output pin.
GPIO.setmode(GPIO.BOARD) GPIO.setup(12, GPIO.OUT)
Next we want to start our program by sending a low value to pin 12, or “False”
Now, we turn the LED on:
Wait 5 seconds:
Turn the LED off:
Finally, we need to do some GPIO cleanup, so that the Pi knows that they are no longer in use:
Put it all together, and the code looks like this:
#!/usr/bin/python import RPi.GPIO as GPIO # Setup the GPIO ports # Use the pin numbers as they appear on the board GPIO.setmode(GPIO.BOARD) GPIO.setup(12, GPIO.OUT) #Make sure the LED is off GPIO.output(12, False) #Turn the LED on GPIO.output(12, True) # Wait 5 seconds time.sleep(5) #Turn the LED off GPIO.output(12, False) #Clean up GPIO.cleanup()
Note: Save your file in nano by typing Ctrl-o, and exit by typing Ctrl-x
Now, you will need to use the jumper wires to attach the positive end of your circuit, connected to your resistor, to Pin 12 (6th pin on the edge of the board). You can connect the negative end of the LED to any ground pin, in the picture, I have it connected to pin 6 (The third pin on the edge side of the board).
Run and Test
Finally, once you have everything hooked up, you can run the program by typing the following at the terminal:
$ sudo python led.py
Watch your LED, you should see it turn on for 5 seconds and then turn off. If you did…SUCCESS!
Good Luck and Have Fun!