In this guide, you’ll learn how to set the Raspberry Pi GPIOs as digital outputs and control them using a Python program. As an example, we’ll show you how to control an LED connected to one of the Raspberry Pi GPIOs using the gpiozero interface.
Table of Contents
Throughout this tutorial, we’ll cover the following main topics:
- Introducing the Raspberry Pi GPIOs
- Connect an LED to the Raspberry Pi
- Controlling Raspberry Pi Outputs using gpiozero
Before continuing with this tutorial, check the following prerequisites.
- Get familiar with the Raspberry Pi board—if you’re not familiar with the Raspberry Pi, you can read our Raspberry Pi Getting Started Guide here.
- You must know how to run and create Python files on your Raspberry Pi. We like to program our Raspberry Pi via SSH using an extension on VS Code. We have a detailed tutorial about that subject: Programming Raspberry Pi Remotely using VS Code (Remote-SSH).
Introducing the Raspberry Pi GPIOs
GPIO stands for General Purpose Input Output pins and those allow you to connect and control electronic hardware, like LEDs, motors, and sensors to your Raspberry Pi.
This means they can be used to both read and send information, allowing your Pi to interact
with the outside world.
Most models of Raspberry Pi boards have a double row of 40 GPIO pins. The layout of the pins is usually the same for most Raspberry Pi models.
In this tutorial, we’ll take a look at how we can set the Raspberry Pi GPIOs as outputs to control an LED or any other actuator that can be controlled with high (3V3) and low (0V) signals.
Raspberry Pi GPIO Numbering
There are two different ways to refer to a GPIO pin: its name (which is known as GPIO numbering or Broadcom numbering) or its corresponding pin physical number (which corresponds to the pin’s physical location on the header).
For example, GPIO 25 corresponds to pin 22 (see the picture below). Throughout this tutorial, we’ll refer to GPIO pins by their GPIO numbering (Broadcom numbering).
To learn more about the Raspberry Pi GPIOs, check this guide: Raspberry Pi Pinout Guide: How to use the Raspberry Pi GPIOs?
GPIO pins can be set to HIGH, which outputs 3.3V and turns a component on, or LOW,
which outputs 0V and turns the component off.
Wiring the Circuit
Wire an LED to one of the Raspberry Pi GPIOs. We’ll connect one LED to GPIO 14 (pin 8). You can use any other pins, except GPIO 0 and GPIO 1.
Here’s a list of components you need:
- Raspberry Pi Board – read Best Raspberry Pi Starter Kits
- 220 Ohm resistor (or similar)
You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!
Controlling Raspberry Pi Outputs using gpiozero
The gpiozero library provides a collection of interfaces for everyday components like LEDs, buttons, potentiometers, sensors, and much more. So, instead of setting the GPIO properties to control an LED, the gpiozero provides an LED interface with methods that are useful to control LEDs. Instead, you can also use the DigitalOutputDevice interface that can be used to control digital outputs in general (including LEDs). Let’s see how it works.
The gpiozero library should already be installed if you’re running Raspberry Pi OS — if not, you can run the following command:
python3 -m pip gpiozero
Create a new python file on your Raspberry Pi called blinking_led.py and copy the following code.
# Complete Project Details: https://RandomNerdTutorials.com/projects-raspberry-pi/ from gpiozero import LED from time import sleep led = LED(14) # blinking an LED forever while True: #set the led ON for one second led.on() sleep(1) #set the led ON for one second led.off() sleep(1)
How the Code Works
Continue reading to learn how the code works.
First, you import the LED component from the gpiozero library to control the GPIO that the LED is connected to. Then, you also need to import the sleep() function from the time module to create delays in the code.
from gpiozero import LED from time import sleep
Instead of the LED component, you can use the DigitalOutputDevice component which works exactly the same.
from gpiozero import DigitalOutputDevice
Declaring the LED
Next, you create an LED object called led that refers to GPIO 14, which is the GPIO that the LED is connected to. Change the number if you’re using another GPIO.
led = LED(14)
When you create and use this LED object, your program knows that GPIO 14 is an output that can be set to HIGH or LOW. After this declaration, you can use led to refer to your GPIO 14. You can use this LED object to control other components than LEDs, as long as they can be controlled with HIGH (3.3V) and LOW (0V) signals.
Note: if you want to use the DigitalOutputDevice component instead, the declaration would be as follows:
led = DigitalOutputDevice(14)
Then, to keep the LED blinking forever, we create a while loop that is always True—the code will be running forever unless you stop the program. The lines after the loop declaration are indented, telling Python that this is the content of the loop to be run as long as the while condition is met.
# blinking an LED forever while True: #set the led ON for one second led.on() sleep(1) #set the led ON for one second led.off() sleep(1)
Controlling the Digital Output
The LED object has two methods that you can use to turn a GPIO on and off—the on() and off() methods. You can use them as follows:
The led.on() instruction turns GPIO 14 on and the led.off() turns GPIO 14 off. There is a pause of one second between each LED state using the sleep() function, creating the blinking effect. The sleep() function accepts as an argument the time in seconds.
1) To control a Raspberry Pi Digital Output, you can use the LED or the DigitalOutputDevice interface of the gpiozero library. You need to import it first like this:
from gpiozero import LED
or like this:
from gpiozero import DigitalOutputDevice
2) Define the GPIO that you want to control. Using the LED interface:
led = LED(GPIO_NUMBER_OF_YOUR_CHOICE)
Or if you use the DigitalOutputDevice interface:
led = DigitalOutputDevice(GPIO_NUMBER_OF_YOUR_CHOICE)
3) Then, use the on() and off() methods to turn the GPIO on and off.
Save your python file. Then run it on your Raspberry Pi. Run the following command on the directory of your project file (use the name of your file):
The LED connected to GPIO 14 should be blinking.
You can stop the execution of the program by pressing CTRL+C.
Other Useful Methods
The LED and DigitalOutputDevice interfaces provide other useful additional methods.
The toggle() method reverses the current state of the GPIO. Here’s an alternative script to blink an LED using the toggle() method.
# Complete Project Details: https://RandomNerdTutorials.com/projects-raspberry-pi/ from gpiozero import LED from time import sleep led = LED(14) # blinking an LED forever while True: led.toggle() sleep(1)
The blink() method blinks an LED on and off. You can set the on and off times, as well as how many times the LED will blink. Here’s a script blinking an LED using the blink() method.
# Complete Project Details: https://RandomNerdTutorials.com/projects-raspberry-pi/ from gpiozero import LED from signal import pause led = LED(14) # blinking an LED forever led.blink() pause()
You can pass the following parameters to the blink() method:
blink(on_time=1, off_time=1, n=None, background=True)
- on_time: number of seconds the LED will be on. The default is one second.
- off_time: number of seconds the LED will be off. The default is one second.
- n: number of times to blink. Set to None to run forever. None is the default value.
- background: if True (the default), start a background thread to continue blinking and return immediately.
In this tutorial, you learned how to set the Raspberry Pi GPIOs as digital outputs and set them on and off using the LED or the DigitalOutputDevice interfaces of the gpiozero library. We’ve also seen three different ways to blink an LED using different methods.
We hope you found this tutorial useful. If you’re a beginner to the Raspberry Pi, you can get started with the following tutorials:
- Getting Started with Raspberry Pi
- Raspberry Pi Pinout Guide
- Programming Raspberry Pi Remotely using VS Code (Remote-SSH)
Thanks for reading.