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

Control LED lights from your Pi with Python

Bundle of Raspberry Pis
Choosing your first Raspberry Pi can be confusing for new enthusiasts. Richard Saville

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.

What You Need to Start the Project

Parts required for the 'Light an LED with the Raspberry Pi's GPIO' project

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

Step 1 - Create the Circuit

Connecting pins to the breadboard of Raspberry Pi's GPIO

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.

  1. Firstly, turn off your Raspberry Pi. 

  2. Now, using the jumper wires, connect the ground pin to a lane on your breadboard.

  3. Next, do the same for the GPIO pin, connecting to a different lane.

Step 2 - Add the LED to the Circuit

Full circuit with LED and resistor fitted on Raspberry Pi's GPIO

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, they have 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 Ohm's law and related topics afterward.

  1. 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.

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

Step 3 - Python GPIO Code (RPi.GPIO)

RPi.GPIO code

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 stop. The latest version of Raspbian will have the necessary GPIO libraries installed already.

  1. Open a terminal window and create a new Python script by entering the following commands:

    chmod +x
  2. 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
    GPIO.setup(LED, GPIO.OUT)

    # Turn the GPIO pin on

    # Wait 5 seconds

    # Turn the GPIO pin off
  3. Press Ctrl+X to save the file. To run the file, enter the following command in the terminal and press enter:

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