LED - Let There Be Light

In this activity you will connect a Light Emitting Diode (LED) to your Raspberry Pi's GPIO (General Purpose Input Output) Pins.

Warning!

  • Make sure your circuit on your breadboard is not connected to your Pi

  • Double check you circuit before you connect it to your Pi

After you have created the circuit you need to write a simple Pyhon program controlling the LED state being on or off.

Your program will introduce the concepts:

  • importing modules

  • setting GPIO pins

  • variables

  • user input from screen

Equipment Used

Electronics:

  • One LED

  • Breadboard

  • Male to Female Breadboard Wires

Raspberry Pi

The model Raspberry Pi we use is the Pi 3 Model B V1.2. Older models are compatible with our programs and electronic diagrams provided you use GPIO.Board for the pin numbering.

Creating the circuit

LED circuit

The LED has to be connected in the right way. The electricity can only flow one way through a diode and a LED (a Light Emitting Diode). For the sake of this activity we assume the flow of electricity is from positive to negative. For a more correct explanation look at this article in WikipediA.

The anode (+) is the larger pin on the LED and the cathode (-) is the shorter pin. Also on the housing of LED is a flat part on the rim on the side of the cathode.

Pin 11 will supply the 3.3 volts to the LED when activated in your Python program.

Pin 6 is the ground pin completing the circuit when Pin 11 is turned on.

Make sure your Pi is turned off from the power when you connect the LED circuit to your Pi!

Check the circuit before connecting the Pi to the power

A bit more about breadboards

The vertical holes on each side of the breadboard are the Power Rails. There are four Power Rails. The holes on each Rail are connected.

The five holes on a Terminal Strip are connected.

Please note!:

  • The four Power Rails are separate and NOT inter-connected.

  • All Terminal Strips are separate and NOT inter-connected.

  • The Terminal Strips are NOT connected across the Divider

The Divider is used for IC chips and to straddle parts such as switches, LEDs and chips.

This site provides a more detailed explanation on the use of a breadboard.

Creating the program

Every line proceeded by # is a comment and ignored by the Python interpreter.

We strongly recommend you do not cut and paste this program but type it in yourself.

# led.py
# Program to turn a LED on and off.
# by Your Name

# Import the GPIO module and refer to it in our program as IO .
import RPi.GPIO as IO

# Only need the sleep function from time module.
from time import sleep

# Use the BOARD settings from the module.
IO.setmode(IO.BOARD)

# Set Pin 11 on the Pi as an output.
# This will supply 3.3 volts when turned on.
IO.setup(7,IO.OUT)

# create variables conataining a message
message_on = "Let there be light\n\n"
message_off = "Let there be dark"

# Turn Pin 11 on - the LED is supplied with 3.3 volts.
# and print a message that the light is on.
IO.output(11, True)
print(message_on)

# Wait 30 seconds. The light will stay on for 30 seconds.
sleep(30)

# Turn off Pin 11. No voltage supplied to the Pin. Light is turned off.
# And print message the light is turned off.
IO.output(7,False)
print("Let there be dark")

# Reset all the pins to the default settings
IO.cleanup()

Last updated

Was this helpful?