When we are making projects using Raspberry Pi, most of the time we need a way to connect the Raspberry Pi to other equipment. To do this, we use the Pi’s GPIO pins. In this article, we will discuss how to control LEDs using Raspberry Pi and the Python programming language.

Hardware Requirement

For this tutorial, we will need the following:

  • Raspberry Pi 4B with Raspberry Pi OS installed
  • LED (1 pc)
  • 330 Ohms Resistor (1 pc)
  • Jumper wires
  • Breadboard

Before we proceed, let us first discuss a little bit about the LED and resistor mentioned above.

LED

Light Emitting Diode or LED

The component that we will control using the Raspberry Pi is an LED. LED is an acronym for Light Emitting Diode. It is an electronic component that has two terminals (or legs) and glows when voltage is applied across its terminals.

One of the terminals of an LED is longer than the other. The longer terminal is called the anode and is always connected to the positive side of the circuit. The shorter terminal is called the cathode and should be connected to the negative side of the circuit. If the terminals are connected in reverse, the LED will not get damaged but it will not glow.

Resistor

330 Ohms Resistor

The resistor that we will use for this article has a value of 330 ohms. The value is represented by 3 or 4 color bands. For 330 ohms, the color band would be orange-orange-brown. The gold at the end denotes the tolerance of +/- 5%.

Resistors should always be used when connecting LEDs to the GPIO pins. If the LED is connected directly to the GPIO without using a resistor, the LED will draw too much current and this high current will damage the GPIO pin or worse burn out your Raspberry Pi.

We limit the current draw of the LED by connecting it in series with a resistor.

Connection Diagram

Now that we have a better understanding of the LED and resistor, it is time to connect them to the Raspberry Pi.

On the Raspberry Pi, we will use Ground (pin 6) and GPIO14 (pin 8) to connect the LED (with resistor) to the Raspberry Pi. Technically, any GPIO and GND pin can be used to connect your LED. So if you use pin 6 and pin 8 for something else, feel free to use any other GPIO and GND pin that is available.

Raspberry Pi 4B GPIO layout

Connect the components as shown below.

Connection Diagram

Python Programming

After connecting the hardware components, the next step is to create a python program to switch on and switch off the LED with a one-second pause in between.

The first step is to create a python *.py file. To do this, open a terminal and type nano LED.py and press Enter.

nano LED.py

This will create a file named LED.py and open it in the NANO text editor. This is a text editor that comes with the Raspberry Pi OS. The next step is to create the actual python code to control our LED connected on the Raspberry Pi’s GPIO.

Nano Text Editor

Import libraries

Import the necessary libraries by typing the following:

import RPi.GPIO as GPIO
import time

As you may have noticed in the code above, we used two libraries. The RPi.GPIO library will allow us to control the GPIO pins. And the time library contains the sleep() function that we will use to insert a one-second pause in the code.

Set-up GPIO

The next step after importing the necessary libraries is to initialize the GPIO object. We do this by adding the following:

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(14,GPIO.OUT)

GPIO.setmode(GPIO.BCM) means we will use the BCM pin numbering system in declaring the GPIO pins in our code. The equivalent BCM numbering system for pin 8 is GPIO number 14 (GPIO14).

We use .GPIO.setwarnings(False) to disable the warnings and the GPIO.setup(14,GPIO.OUT) is used to set GPIO14 as an output.

Control GPIO14

After importing the libraries and setting up the GPIO object, the last step in programming is to change the state of GPIO14 every one second. We do this by adding the following code at the end. I have added comments within the code to help you understand better how the code works.

# While loop
while True:
        # set GPIO14 pin to HIGH
        GPIO.output(14,GPIO.HIGH)
        # show message to Terminal
        print "LED is ON"
        # pause for one second
        time.sleep(1)

        # set GPIO14 pin to HIGH
        GPIO.output(14,GPIO.LOW)
        # show message to Terminal
        print "LED is OFF"
        # pause for one second
        time.sleep(1)

The complete code should now look like below:

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(14,GPIO.OUT)

# While loop
while True:
        # set GPIO14 pin to HIGH
        GPIO.output(14,GPIO.HIGH)
        # show message to Terminal
        print "LED is ON"
        # pause for one second
        time.sleep(1)


        # set GPIO14 pin to HIGH
        GPIO.output(14,GPIO.LOW)
        # show message to Terminal
        print "LED is OFF"
        # pause for one second
        time.sleep(1)

Our python program is now complete. Save it by pressing Ctrl + X on your keyboard, press Y, and press Enter.

Run the Program

Run the program by typing the following in the terminal and press Enter.

sudo python LED.py

You should see the LED blinking every one second and see the message in the terminal LED is ON and LED is OFF” in sync with the LED. To stop the program, press Ctrl + C on your keyboard.

The program works by changing the state of GPIO14 to HIGH to switch ON the LED and then after one second, it changes the pin state to LOW to switch off the LED.

Congratulations! We have successfully used the GPIO to control the LED using Python. Our code above can be expanded by controlling multiple GPIO pins. Try it for yourself and comment below.