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

Control LED lights from your Pi with Python

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, especially for beginner projects such as LEDs and buzzers. With 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

To start the project, 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 with the latest Raspbian (Pi, screen, keyboard, mouse, power, and SD card)
  • A small breadboard
  • A 5 mm LED
  • A 330-ohm resistor
  • Two male to female jumper wires
Parts required for the 'Light an LED with the Raspberry Pi's GPIO' project

Create the Circuit

You'll use two GPIO pins for this project:

  • A ground pin (physical pin 39) for the ground leg of the LED.
  • A generic GPIO pin (GPIO 21, physical pin 40) to power the LED—but only when you decide to—which is where the code comes in.
  1. Turn off the Raspberry Pi. 

  2. Use the jumper wires to connect the ground pin to a lane on the breadboard.

  3. Do the same for the GPIO pin, but connect it to a different lane.

    Connecting pins to the breadboard of Raspberry Pi's GPIO

Add the LED to the Circuit

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

A resistor protects 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: 330 ohms. There is some math behind that, but for now, focus on the project. You can always look into Ohm's law and related topics afterward.

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

  2. Join the longer leg of the LED to the lane connected to the GPIO pin.

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

Create the Python GPIO Code (RPi.GPIO)

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

RPi.GPIO code

The next step is to make a Python file to tell the GPIO pin to send power for five seconds, and then stop. The latest version of Raspbian has the necessary GPIO libraries installed.

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

    touch led.py
    chmod +x led.py
    nano led.py
  2. This opens a blank file where you'll enter the code. Enter the lines below:

    #! /usr/bin/python

    # Import the libraries we need
    import RPi.GPIO as GPIO
    import time

    # Set the GPIO mode
    GPIO.setmode(GPIO.BCM)

    # 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
    GPIO.output(LED,True)

    # Wait 5 seconds
    time.sleep(5)

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

    python led.py
  4. The LED should light for five seconds then turn off, ending the program.