Getting Started with GPIO Programming



The Raspberry Pi comes with 8 General Purpose Input Output pins (GPIO).  Each pin can be used as an input or an output, you set it in the program.


This tutorial will help you get your first light to blink.   It assumes that you have installed the most updated software on your Pi.  If this is the first time your Pi  has been used for GPIO you may need to install the GPIO module available on the Python website:



The Board

To get started set up your board like this:

·       Make sure to add a 220 Ω resistor in series with the LED, so it stays nice and healthy.



The Code


import RPi.GPIO as GPIO  # Imports the GPIO Module

import time

GPIO.setmode(GPIO.BOARD) # Gives Python access to the pins

GPIO.setup(11, GPIO.OUT) # Set pin 11 as an output pin


GPIO.output(11,GPIO.HIGH) #Turn pin 11 on


time.sleep(5)             # wait 5 seconds


GPIO.output(11,GPIO.LOW)  #Turn pin 11 off


Important!!!  Pin 11 is actually GPIO pin 17

There is a card in your kit that shows you the correspondence.  Use the green numbers in your code, the grey numbers are on the board.



The card from your kit:


Running Your Program

GPIO programs must be run as a “root user” through the terminal (command prompt).  This is not an uncommon way to execute code.



Step 1: use the “cd” command to change the directory to the folder where your program is saved.


Step 2: Run your program by typing:


 sudo python




If all went well your LED should be blink for 5 seconds and turn off.

Try this:

1.   Use a loop so that the light blinks twice per second.


2.   Add two more lights and make them blink in the order green, yellow, red forever!


Next: Setting up the input pins.