How to Light an LED with the Raspberry Pi's GPIO

The GPIO is how the Raspberry Pi talks to the outside world. It uses code to program the signals and voltages to and from the 40-pin header.

Coding with the GPIO is reasonably simple to get started with, especially for beginner projects such as LEDs and buzzers. With just a couple of components and a few lines of code you can light or flash an LED as part of your project.

This tutorial shows what you need to light an LED using Python code on your Raspberry Pi by using the traditional 'RPi.GPIO' method.

of 04

What You Need to Start the Project

Parts required for the project

courtesy of Richard Saville

To start the project, you'll need to begin with the list of items below. You should be able to find these items in your favorite maker store or online auction sites.

  • A Raspberry Pi workstation running the latest Raspbian (Pi, screen, keyboard, mouse, power, SD card)
  • A small breadboard
  • A 5mm LED
  • A 330 ohm resistor
  • 2 male to female jumper wires
of 04

Step 1 - Create The Circuit

Connecting pins to the breadboard

courtesy of Richard Saville

We're going to use 2 GPIO pins for this project, a ground pin (physical pin 39) for the ground leg of the LED, and a generic GPIO pin (GPIO 21, physical pin 40) to power the LED - but only when we decide to - which is where the code comes in.

Firstly, turn off your Raspberry Pi. Now, using the jumper wires, connect the ground pin to a lane on your breadboard. Next do the same for the GPIO pin, connecting to a different lane.

of 04

Step 2 - Create The Circuit

Full circuit with LED and resistor fitted

courtesy of Richard Saville

Next, add the LED and resistor to the circuit.

LEDs have polarity, which means they have to be wired in a certain way. They usually have one longer leg, which is the anode (positive) leg, and usually a flat edge on the LED plastic head, which denotes the cathode (negative) leg.

A resistor is used to protect both the LED from receiving too much current, and the GPIO pin from giving too much. This could damage both.

There is a bit of a generic resistor rating for standard LEDs: 330ohm. There is some math behind that, but for now focus on the project and you can always look into ohms law and related topics afterwards.

Connect one leg of the resistor to the GND lane on your breadboard, and the other resistor leg to the lane connected to the shorter leg of your LED.

The longer leg of the LED now needs to join the lane connected to the GPIO pin.

of 04

Step 3 - Python GPIO Code (RPi.GPIO)

RPi.GPIO code

courtesy of Richard Saville

At this moment, you have a circuit wired up and ready to go, but now you haven't told the GPIO pin to send out any power yet. So, your LED shouldn't be lit.

The next step is to make a Python file to tell the GPIO pin to send out some power for 5 seconds and then stop. The latest version of Raspbian will have the necessary GPIO libraries installed already.

Open a terminal window and create a new Python script by entering the following command:

sudo nano

This will open a blank file for us to enter our code. Enter the lines below:

#! /usr/bin/python
# Import the libraries we need
import RPi.GPIO as GPIO
import time
# Set the GPIO mode
# Set the LED GPIO number
LED = 21
# Set the LED GPIO pin as an output
# Turn the GPIO pin on
# Wait 5 seconds
# Turn the GPIO pin off

Press Ctrl+X to save the file. To run the file, enter the following command in the terminal and press enter:

sudo python

The LED should light for 5 seconds then turn off, ending the program.