Let’s Blink A Light!

Did you just setup your Raspberry Pi an are looking for what to do next!? Here at The Pi Factory, one of the first projects we recommend doing with your Pi is controlling an LED through the use of it’s GPIO pins. You may have noticed these. They are the set of 32 metal pins grouped in a horizontal line on your Pi. These are known as General Purpose Input Output pins, and they allow you’re Pi to communicate with all sorts of things like LED lights, temperature sensors, displays and more. Lets start things off nice and simple by learning how to control an LED with your Raspberry Pi, it’s GPIO pins and simple Python commands.

Thing’s You’ll Need:

  • Raspberry Pi (any model will do)
  • An LED light
  • A breadboard of any size
  • 1 Resistor (330 ohm specifically) You can use this handy tool from DigiKey to figure out which resistor is which
  • 2 male to female jumper wires

If you don’t have these items, we’d recommend grabbing a Raspberry Pi starter kit from Canakit, Adafruit or another reputable retailer. They will have all the components you need to complete basic projects like this one!

Let’s Learn A Bit About The Components We’ll Be Using

The Breadboard

Breadboards are one of the most fundamental pieces when learning how to build circuits. In this tutorial, you will learn a little bit about what breadboards are, why they are called breadboards, and how to use one. Once you are done you should have a basic understanding of how breadboards work and be able to build a basic circuit on a breadboard.

The best way to explain how a breadboard works is to take it apart and see what’s inside. Using a smaller breadboard it’s easier to see just how they function.

SparkFun Mini Breadboard

Terminal Strips

Above we have a breadboard where the adhesive backing has been removed. You can see lots of horizontal rows of metal strips on the bottom of the breadboard.

The tops of the metal rows have little clips that hide under the plastic holes. These clips allow you to stick a wire or the leg of a component into the exposed holes on a breadboard, which then hold it in place.

Once inserted that component will be electrically connected to anything else placed in that row. This is because the metal rows are conductive and allow current to flow from any point in that strip.


[From SparkFun (SparkFun is awesome, go check them out)]


Now that you understand the basics of a breadboard and how it can be used to prototype circuits, it’s time to go over the rest of the components. The Pi Hut does a great explanation of all the components we’ll be using.




LED stands for Light Emitting Diode, and glows when electricity is passed through it.

When you pick up the LED, you will notice that one leg is longer than the other. The longer leg (known as the ‘anode’), is always connected to the positive supply of the circuit. The shorter leg (known as the ‘cathode’) is connected to the negative side of the power supply, known as ‘ground’.

LEDs will only work if power is supplied the correct way round (i.e. if the ‘polarity’ is correct). You will not break the LEDs if you connect them the wrong way round – they will just not light. If you find that they do not light in your circuit, it may be because they have been connected the wrong way round.

The Resistor

330 Ohm Resistor

You must ALWAYS use resistors to connect LEDs up to the GPIO pins of the Raspberry Pi. The Raspberry Pi can only supply a small current (about 60mA). The LEDs will want to draw more, and if allowed to they will burn out the Raspberry Pi. Therefore putting the resistors in the circuit will ensure that only this small current will flow and the Pi will not be damaged.

Resistors are a way of limiting the amount of electricity going through a circuit; specifically, they limit the amount of ‘current’ that is allowed to flow. The measure of resistance is called the Ohm (Ω), and the larger the resistance, the more it limits the current. The value of a resistor is marked with coloured bands along the length of the resistor body.

You will be using a 330Ω resistor. You can identify the 330Ω resistors by the colour bands along the body. The colour coding will depend on how many bands are on the resistors supplied:

  • If there are four colour bands, they will be Orange, Orange, Brown, and then Gold.
  • If there are five bands, then the colours will be Orange, Orange, Black, Black, Brown.

It does not matter which way round you connect the resistors. Current flows in both ways through them.

Jumper Wires

Jumper Wires

Jumper wires are used on breadboards to ‘jump’ from one connection to another. The ones you will be using in this circuit have different connectors on each end. The end with the ‘pin’ will go into the Breadboard. The end with the piece of plastic with a hole in it will go onto the Raspberry Pi’s GPIO pins.

[From The Pi Hut]

What Are GPIO Pins?

The abbreviation GPIO stands for General Purpose Input Output pins. As we said before, they are used as a way for the Raspberry Pi to control and monitor devices/circuits attached to it.

Check out the diagram below. It goes over what every pin’s #/capabilities are. Don’t worry if you don’t know what most of them mean. You’ll learn! The majority are simply numbered, and you can target the numbered pin from your raspberry pi and turn it on or off and communicate with a device through it. You probably recognize three of them from your everyday life, GND (Standard Grounding Point), 3.3V and 5V (power).

Now that you know a bit about the GPIO let’s build a circuit and start using the pins!

Image result for Raspberry Pi gpio pins

Building the Circuit

To create your circuit you will need to use your Raspberry to your LED on when power is supplied. To do that without blowing your LED you will need a resistor to limit the current that can flow through the circuit.

The GND (Ground) pins will act like a standard ground point. Similar to the negative end of a battery. The positive (powered) side of things will be provided by your Pi’s GPIO pin. In this case, we will us the GPIO pin numbered 18. If you have a ribbon cable and breadboard connector, it will most likely have all the pins nicely labeled. (See below)

Image From Adafruit

Now take a look at the circuit diagram below and then shutdown [sudo shudown now] and unplug your Pi. Now try recreating it on your own breadboard and Raspberry Pi! With the GPIO connector, you will connect from the connector in the breadboard, as the ribbon Cable extends all your GPIO connections to it.

See if you can get it correct just looking at the diagram. If not, scroll down and use our steps for reference.

A Single LED

  1. Grab a jumper wire and connect the male end to the negative side rail of the breadboard (marked blue) on the breadboard. Then connect the female end to one of the Pi’s GND pins. This establishes your ground.
  2. Now connect the resistor to the negative side rail of the breadboard, the same row you connected the ground to. Then connect it to an empty space on the breadboard in the same row.
  3. Now place the short leg of the LED into breadboard in the same row as your resistor (It doesn’t matter how far away it is). Then place the right leg into any open space in the same row as the left leg.
  4. Finally, connect your last jumper wire to an open space in the same row as the long leg of the LED and to pin 18 on your Raspberry Pi.

That’s it!! You’re ready to code.

Let’s Right Your Program

You can communicate with your GPIO pins using the cool program called scratch if you wish. It’s great for learning the basics of coding/commands, especially for kids! In this tutorial, we’ll be doing things the more standard way using Terminal.

Let’s write some code!  Turn on your Pi and open the terminal window.

Create a new empty text file named LED.py. To do this, type the following and hit enter. This will create the file with the inputted name, and open it in Terminal to be edited.

nano LED.py

Without specifying a directory for your file i.e. nano /usr/source/LED.py, it will be save to your home directory. That means you can always acces it by typing nano and then the name of your file. Also, if you wanted to name your file, for example, BobsLED.py, that would be fine too. Just make sure you use nano BobsLED.py.

Once you’re editing your file, you can start typing code. The code to control your LED at Pin 18 is as follows.

import RPi.GPIO as GPIO
import time
print "LED on"
print "LED off"

Once you have that all entered correctly you can hit ctrl x, y and then enter to save your file.

Here’s a great breakdown of the code from The Pi Hut. Check them out by the way!! They are a wonderful resource for ordering your Raspberry Pi’s, accessories and other maker goods. We love them!

import RPi.GPIO as GPIO The first line tells the Python interpreter (the thing that runs the Python code) that it will be using a ‘library’ that will tell it how to work with the Raspberry Pi’s GPIO pins.  A ‘library’ gives a programming language extra commands that can be used to do something different that it previously did not know how to do.  This is like adding a new channel to your TV so you can watch something different.
import time Imports the Time library so that we can pause the script later on.
GPIO.setmode(GPIO.BCM) Each pin on the Pi has several different names, so you need to tell the program which naming convention is to be used.
GPIO.setwarnings(False) This tells Python not to print GPIO warning messages to the screen.
GPIO.setup(18,GPIO.OUT) This line tells the Python interpreter that pin 18 is going to be used for outputting information, which means you are going to be able to turn the pin ‘on’ and ‘off’.
print "LED on" This line prints some information to the terminal.
GPIO.output(18,GPIO.HIGH) This turns the GPIO pin ‘on’. What this actually means is that the pin is made to provide power of 3.3volts.  This is enough to turn the LED in our circuit on.
time.sleep(1) Pauses the Python program for 1 second
print "LED off" This line prints some information to the terminal.
GPIO.output(18,GPIO.LOW) This turns the GPIO pin ‘off’, meaning that the pin is no longer supplying any power.

Now Let’s Run Your Code

In Terminal type:

sudo python LED.py

If you built your circuit and entered your code correctly, your LED should turn on for a second and then off again.

If it didn’t work, you should troubleshoot by triple checking your circuit and also checking your code. You can edit your LED file by opening terminal and typing

nano LED.py


We. Are. Done!! Not so bad right. It does make you appreciate all the little LED’s you see turning on and off on cue in your everyday life. Think about all the circuits you take for granted every day!

Sources: [The Pi Hut, The Raspberry Pi Foundation, SparkFun]