Blink an LED with a Raspberry Pi

Difficulty
Moderate
Steps
11

One of the simplest ways to get started with physical computing is to get an LED to turn on and off. 

In this guide, we will get an LED to blink by connecting it to a Raspberry Pi 3 Model B. We will then program it using the GPIO Zero Python library. 

On completion of this guide, you will have gained a better understanding of how to utilise LEDs as well as on programming your Raspberry Pi.

Step 1  Meet the Breadboard

  • In this tutorial we're going to use a solderless breadboard for prototyping.
  • A modern solderless breadboard consists of a perforated block of plastic with conductive spring clips under the perforations.
  • The holes in the breadboard are sometimes called tie points.

Step 2  Meet the GPIO Breakout

  • To connect our Raspberry Pi to our breadboard we're going to use a GPIO Breakout Board.
  • Take the Breakout Board and Ribbon Cable out of the packaging and discard the protective shipping foam.

Step 3  Inserting the Breakout

  • Insert the Breakout Board so that the top pins are aligned with Row 1 of the Breadboard.
  • Ensure that the Breakout Board is fully inserted into the Breadboard so that the pins make good contact. This will take some force.

Step 4  Meed the LED

  • This is a light-emitting diode (LED).
  • When power is provided to the long leg (+), and the short leg (-) is connected to ground, current flows and light is emitted.
  • No current flows and no light is emitted if a small voltage is applied in the reverse direction

Step 5  Insert the LED

  • Insert the LED into the breadboard with the long leg in tie point H33 and the short leg in H34.

Step 6  Connect Red Jumper to GPIO 21

  • Connect a Red Jumperwire between tie point G20 (which is next to GPIO21) and F33.

Step 7  Connect Black Jumper to GND

  • Connect a Black Jumperwire between tie point B20 (which is next to GND) and F34.

Step 8  Connect the Breadboard to the Raspberry Pi

  • Insert the Ribbon Cable into the Breakout. A notch means you can really only insert it one way.
  • Connect the other end of the Ribbon Cable so that it does not fold, and the white line on the Ribbon Cable is closest to the SD Card Slot end of the Raspberry Pi.
  • Make sure that the Ribbon Cable is on both rows of GPIOs.

Step 9  Installing GPIO Zero

Now that we have the Raspberry Pi hooked up to the breadboard and LED, we will need to program it to turn the LED on and off. 

We will install GPIO Zero, a Python library which builds upon existing GPIO libraries such as RPI.GPIO, rPIO, and pigpio. It will simplify the process by reducing boilerplate code.

To do so, open a terminal window and enter the following: 

sudo apt-get update

sudo apt-get install python3-gpiozero

Step 10  Create a Python file

  1. Click on the Raspberry Pi icon on the top left hand corner to access the main menu. 
  2. Click on Programming > Python 3 (IDLE). 
  3. Now, create a new file by clicking File > New File. 
  4. Next, save the file by clicking File > Save, and naming it blink.py

Step 11  The code


              from gpiozero import LED
from time import sleep
led = LED(21)
while True:
    led.on()
    sleep(1)
    led.off()
    sleep(1)
            

Now, enter the following code into your Python file.

First, we import the LED class from GPIO Zero by using:

 from gpiozero import LED

Then we import the sleep function from time library. This will enable a pause when turning the LED on and off:

from time import sleep

Previously, we connected the LED to GPIO21 and GND of the Raspberry Pi. So, we will assign the led variable to GPIO 21 in the line: 

led = LED(21)

The LED will be powered up whenever we set it to on in the code: 

led.on()

To create a never-ending loop which will switch the LED on and off, we use:

while True: 

To ensure a pause of 1 second between each change, we use:

sleep(1)

 Feel free to change this value from '1' to another number if you would like there to be a longer pause. Remember to save the python file!