Raspberry Pi blinking light

Blinking a single light is a simple project and the easiest way to start physical computing with the Raspberry Pi. There are three basic steps to get the blinking light to work.

1. Wiring.

You will need a single LED light, a breadboard and two pieces of jumper wire. Connect one wire to the Raspberry Pi’s ground pin. Choose another pin to be the controlling pin. My program below uses pin 25. Finally, connect the LED light with the long wire in the same row as the pin’s wire. The short wire on the LED should line up with the wire for the ground pin.

You should end up with a circuit that looks like this:

A light wired to the Pi
A single LED light wired to the Raspberry Pi

2. Programming.

See the annotated program below. The program is a complete Python program, so you can copy and paste it into a file on your Raspberry Pi.

3. Running.

To use the GPIO library and pins, you have to run this program with super-user privileges. So, type the command sudo python blinker.py into the shell. It runs in an infinite loop, so you’ll have to press Control-C to stop it.

Once you have it working, experiment with different values for pause. Try adding a pause2 variable so that there are two different pauses, one for the light being on and one for it being off.

# blinker.py - blink a light-emitting diode on and off

# Add the RPi.GPIO library to interface with hardware pins.
# Add the time library to control the pauses.
import RPi.GPIO as GPIO
import time

# The pause variable makes the light blink on for one second and off for one second.
# Change this value for a faster or slower blink.
pause = 1

# The pin variable tells the Pi which pin the light is connected to.
# Change this value to reflect how you have wired the LED to the Pi.
pin = 25

# Set up the GPIO system to use our chosen pin as an Output
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.OUT)

# The line "while True:" make an infinite loop.
while True:
    # Inside the loop, we turn the light on, pause for one second.
    # Then, turn the light off and pause for another second.
    GPIO.output(pin, GPIO.HIGH)
    time.sleep(pause)
    GPIO.output(pin, GPIO.LOW)
    time.sleep(pause)

Author: Jason

Rev. Jason Wells is the executive director of the New Hampshire Council of Churches. Prior to this position he served Episcopal congregations in New Hampshire for 13 years after his ordination in 2004. Jason is also a board member of the ACLU-NH. He is a former president of the Greater Concord Interfaith Council and has served on the Episcopal Church's committee on ecumenical and interfaith relationships. Jason received a Master of Divinity degree from Princeton Theological Seminary and also holds bachelors degrees in computer science and mathematics from Southern Methodist University.