GPIOZero Tutorial 1 – Controlling an LED

Hello everyone and welcome to PioTex! This little blog will post tutorials and for the anything to do with the Raspberry Pi! Today I will be teaching you to control an LED using GPIOZero; a library composed by Ben Nuttall at the foundation who aims to make using the GPIO on the pi simple, and more accessible to beginners.

Firstly, you will need to wire up an LED and for this you will need an LED, breadboard, 330 (or similar) ohms resistor, 2 male to female jumper cables as well as a pi and peripherals. LEDs can be wired without a breadboard, just with 2 female to female jumper cables, however it really depends on what you have available, so do whatever method suits you best!

Here is a circuit diagram for wiring upLED the LED:

The longer, positive leg of the LED (or anode) should be connected to the resistor and then to a GPIO pin, which can be any, however I have used GPIO10. The shorter, negative leg (or cathode) is simply connected to ground. If you want to remember which pin is which, you can print off a raspberry leaf, get a Portplus keyring etc.

After wiring the LED, double check it to make sure the wiring is correct, as you have a risk of damaging your pi through incorrect wiring. When you are sure it is correct, boot up your pi into either the graphical environment or command line, it really does not matter which as Raspbian Jessie now supports sudo-less GPIO access, meaning that you do not need to be a super user on the pi in order to access the GPIO. If you are in the graphical environment, navigate to programming, then Python3, then press control + n for a new document. If you are in the command line then type “nano” to create a new document.

In this program we will code the LED to turn on, wait one second and turn off, making the LED effectively blink.

To do this, copy this code into your python window, remembering to change the LED pin number to the GPIO pin you have selected (GPIOZero uses BCM and not Board for labelling the pins

from gpiozero import LED
import time
led = LED(10) # change this to whatever pin you have selected

while True: # making the program loop forever
# indent the next 3 lines by pressing tab or space 4 times

Thank you for reading! See you next time with some more GPIOZero tutorials!



2 thoughts on “GPIOZero Tutorial 1 – Controlling an LED

  1. My four-year-old and I enjoyed using this to make a toilet-roll-tube robot with a flashing LED on its head. Thank you! The brackets are missing after led.on and (I’m sure they were there in your original) but this was easily spotted and fixed.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s