Tutorial: Flashing LED using GPIO Output
In this example we'll cover how to build a very simple circuit consisting of an LED and resistor connected up to the GPIO port on your Raspberry Pi. This is a simple exercise will demonstrate visual confirmation that the GPIO port is doing what your python program tells it to do.
This exercise requires just a few simple components, available from ModMyPi:
- Medium Breadboard
- Male to Female Jumper Wires
- LED (Light Emitting Diode) any colour you wish
- 270? or higher resisitor, found in ModMyPi’s Ridiculous Resistor Kit
Step 1 – Assembling the Circuit
We will start by assembling the circuit on the breadboard. A Schematic of the circuit is shown below:
. When hooking up to GPIO points on your Raspberry Pi care must be taken, as connecting the wrong points could permanently fry your Pi. Please use a GPIO cheat-sheet
, and double check everything before switching it on. Each GPIO point will be denoted by name and physical location. For example, GPIO P17
is actually located at Pin 11
, denoted: GPIO P17 [Pin 11]
. The irregularities are a result of the pin names being referenced by the on board chip rather than their physical location.
1. Insert the LED so that the Anode(the longer leg) is in one row of the breaboard and the Cathode(shorter leg) is in another.
2. Insert one end of the resistor into the same row as the LED's Cathode and the other end into another row.
3. Connect a jumper wire from the same row as the LED's Anode (the long leg) to GPIO P17 [Pin 11] on your Raspberry’s pin GPIO port.
4. Connect another jumper wire from the row containing only one leg of the resistor to GPIO GND [Pin 6] on your GPIO port.
When finished your circuit should look similar to the one below:
By default the output pins on the GPIO ports are switched off so the LED will not light up. However you can check your circuit is working correctly by moving the wire in GPIO P17 [Pin 11] to GPIO 3.3V [Pin 1]. If the circuit is correctly wired the LED should light up! If not double check the LED is the right way round! Just remember to move the wire back to GPIO P17 [Pin 11] before you continue.
Step 2 – Making a program in Python
We want the LED to do something more interesting than just turn on! Start by opening a new project Python project. For this exercise we will need to use GPIO Python library which allows you to easily configure and read-write the input/output pins on the Pi’s GPIO port within a Python script. Instructions on how to install the GPIO Python library can be found here
With the GPIO library is installed, we’re all ready to start our Python project! Load up the Raspian GUI with startx, and load the program IDLE 3 into which we’ll type our code. As we’re starting a new project, open up a new window, File>>New Window.
Remember that Python is case sensitive, and indentation is fundamental. Indentation, which is used to group statements, will occur automatically as you type commands, so make sure you stick to the suggested layout.
The first line of our code imports the Python library we’ve just downloaded into our project.
import RPi.GPIO as GPIO
Since we want the LED to flash on and off we will need to add a time module to allow Python to understand the concept of time. Add the following:
Next we need to set our GPIO pin numbering, as either the BOARD numbering or the BCM numbering. BOARD numbering refers to the physical pin numbering of the headers. BCM numbering refers to the channel numbers on the Broadcom chip. Either will do, but for this exercise I will be using BOARD numbering. If you’re confused, use a GPIO cheat sheet to clarify which pin is which!
Now you need to define the GPIO pins as either Inputs or Outputs. In this exercise GPIO P17 [Pin 11] is an output. Tell the GPIO library to set GPIO P17 [Pin 11] to output by adding the following:
If you were going to control additional devices you could add more GPIO,setup lines now. In order to switch the pin on to supply 3.3V, known as 'high', use the command GPIO. Output(11, True). To turn the pin off (0V), known as 'low', give the instruction GPIO.output(11, False). Since we want the LED to flash we will need to use the 'while' function so we can loop the program to switch the LED on and off multiple times. Add the following:
Next we need to use the time module so that after the program turns the LED on it waits 1 second before turning it off, and vice versa. Ensure the lines are indented so that they're included in the while loop (the indentation should be automatic in Python):
The finished program should look like the following in Python:
Save the file as LED.py. You won't be able to run the program from Python since most Linux distributions restrict the use of the GPIO to the root user. In order to run the program open a new terminal window on the Pi and type the following command:
sudo python LED.py
If everything has been done correctly the LED should flash on and off!
If it hasn't worked and the LED isn't flashing don't worry. First check the circuit is connected correctly on the breadboard, then that the jumper wires are connected to the correct pins on the GPIO port. If it still fails to work double check each line on the program is correct remembering that python is case-sensitive and take care to check the indentations are right.
To exit a running Python script, simply hit CTRL+C on the keyboard to terminate.
If everything is working correctly you can now play around with the time variable to change the speed at which the LED flashes on and off. To do so simply change the number inside the brackets of the time.sleep() command . Try time.sleep(0.1) to make the LED flash on and off faster. Remember to save any changes you make to the python program before running it again so that those changes will take effect.
This example is basic but provides a good insight into the fundamental concepts of programming the GPIO ports on your Raspberry Pi.