Control Raspberry Pi Digital Outputs with Python (LED)

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.

Control Raspberry Pi Digital Outputs with Python LED

Table of Contents

Throughout this tutorial, we’ll cover the following main topics:

  1. Introducing the Raspberry Pi GPIOs
  2. Connect an LED to the Raspberry Pi
  3. Controlling Raspberry Pi Outputs using gpiozero

Prerequisites

Before continuing with this tutorial, check the following prerequisites.

  1. 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.
  2. 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).

Raspberry Pi Random Nerd Tutorials

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:

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!

LED connected to Raspberry Pi GPIO14 Circuit Diagram

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)

View raw code

How the Code Works

Continue reading to learn how the code works.

Importing Libraries

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)

While loop

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:

led.on()
led.off()

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.

sleep(1)

In summary…

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.

led.on()
led.off()

Demonstration

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

python blinking_led.py

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.

toggle()

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)

View raw code

blink()

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

View raw code

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.

Wrapping Up

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:

You can check all our Raspberry Pi projects on the following link:

Thanks for reading.



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Recommended Resources

Build a Home Automation System from Scratch » With Raspberry Pi, ESP8266, Arduino, and Node-RED.

Home Automation using ESP8266 eBook and video course » Build IoT and home automation projects.

Arduino Step-by-Step Projects » Build 25 Arduino projects with our course, even with no prior experience!

What to Read Next…


Enjoyed this project? Stay updated by subscribing our newsletter!

Leave a Comment

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.