Light an LED with the Raspberry Pi's GPIO

1
Simple LED Project

LED with Raspberry Pi
LED projects are simple yet satisfying. Richard Saville

Earlier this year I gave you a tour of the Raspberry Pi's GPIO and also recommended some​ really useful breakout boards for identifying pin numbers. Today we continue that theme and start using these pins combined with code and hardware.

The GPIO is how the Raspberry Pi talks to the outside world - "real things" - using 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 article will show you what you need to light an LED using Python code on your Raspberry Pi, using the traditional 'RPi.GPIO' method.

2
What You Need

Parts required for the project
Just a few simple and cheap parts are required for this project. Richard Saville

Here's a list of everything you'll need for this little starter project. You should be able to find these items in your favourite maker store or online auction sites.

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

3
Create The Circuit - Step 1

Connecting pins to the breadboard
Connect each pin to the breadboard with jumper wires. 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.

4
Create The Circuit - Step 2

Full circuit with LED and resistor fitted
The LED and resistor complete the circuit. Richard Saville

Next we add the LED and resistor to the circuit.

LEDs have polarity - meaning 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 - which could damage both.

There's a bit of a generic resistor rating for standard LEDs - 330ohm. There is some maths behind that, but for now let's focus on the project - 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.

5
Python GPIO Code (RPi.GPIO)

RPi.GPIO code
RPi.GPIO is an excellent library for using the GPIO pins. Richard Saville

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

Let's make a Python file to tell our GPIO pin to send out some power for 5 seconds and then stop. The latest version of Raspbian will have the necassary GPIO libraries installed already.

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

sudo nano led1.py

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
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)

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

sudo python led1.py

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

Why not try changing the 'time.sleep' number to light the LED for different times, or try changing the 'GPIO.output(LED,True)' to 'GPIO.output(LED,False)' and see what happens?