A circuit connecting a Raspberry Pi to an LED using a breadboard

GPIO Zero – Enabling anyone to hack!

Learning to code is an empowering experience and when we add physical computing, using components to interact with the real world, the rewards are greatly intensified.

The problem is that there is rather a lot to take in, to turn on an LED with a Raspberry Pi we need to

  • Connect an LED to a breadboard.
  • Connect the a resistor to the LED.
  • Connect wires from the GPIO to the breadboard.
  • Import Python libraries.
  • Configure what GPIO pins do.
  • Write code to control the LED logic

Have a go yourself

This project is super easy to complete, all you need to do is open the Python 3 application, found in the Programming menu. When the application opens, click on File >> New to create a new blank document for each of the code examples. Copy the code for each example and save your work, the filenames for each will be given at the start of each section in this tutorial.


So lets build the circuit

Here is a circuit diagram which we drew in Fritzing a free tool to draw circuits.


A circuit connecting a Raspberry Pi to an LED using a breadboard

A simple circuit

To build the circuit we used.

A Raspberry Pi 2 (Any model will work)


A 220 Ohm resistor (RED,RED,BROWN,GOLD)

Male to female wires




Save your work as GPIO-TEST.py

Using the traditional RPi.GPIO library we can write the code to control the LED, and this is still a valid exercise.

So lets do that and see what the code looks like.

On line 1 of the code we import the time library, we will use that on lines 8 and 10 to call the sleep function which will control the pace at which our LED flashes. On line 2 we import the RPi.GPIO library but rename it to GPIO, this will help reduce any future typos. Line 3 sees us setting the GPIO pins to use the Broadcom (BCM) pin numbering. This may seem like nonsense as the numbers are now in an illogical pattern, but this is the official numbering system supported by the Raspberry Pi Foundation and it matches the pin numbering used by the Broadcom system on a chip, the core of the Raspberry Pi.

Next we create a variable, a container for data which we can name, called “LED” and in there we store the GPIO pin used for our project, in this case 17. We then move on to line 5 where we tell the GPIO library that we have an output device attached to pin 17. An output device in this case is an LED which emits light.

We now move on to line 6 and enter into an infinite loop, sometimes known as a forever loop, while True. This loop will carry on forever, or until the user stops the loop. Anything inside of the loop is indented, this is Python’s way of showing what code belongs to a loop, function etc. Line 7 starts by instructing the LED to turn on, then on line 8 we pause the code for 1 second so that we can see the LED lit. If there were no delay then we would not see the LED lit. Line 9 sees us turn off the LED. Finally line 10 pauses for 1 second so that we can see the unlit LED.

With the code and circuit complete click on Run >> Run Module to run the code and see the LED blink.

Congratulations, now have a go with the next code example using GPIO Zero.


Save your work as GPIOZERO-TEST.py

Created in mid 2015, GPIO Zero is an easier way to interact with the GPIO and is especially good for children.

GPIO Zero has come pre-installed on Raspbian since late 2015. The beauty of GPIO Zero is that it can be used to quickly hack together an electronics project.

So lets use it to create the same project.

We start by importing the LED class from the GPIO Zero. This will enable us to control an LED on the GPIO. On line 2 we import the pause function from a library called signal. We shall use it to stop our code from exiting early.

Line 3 sees us create a variable called red, which is the colour of the LED we used. We use the variable to store that we are using an LED on pin 17. Line 4 see us use the blink function for LEDs. The default value for the blink is 1 second on, 1 second off, but this can be changed.

Finally line 5 calls the pause() function to prevent our code from ending.

With the code and circuit complete click on Run >> Run Module to run the code and see the LED blink.


What have we done?

  • We have built an electronic circuit.
  • Tested two pieces of code that will flash an LED. Which one was easier? Leave a comment below.