Blink LED

Project

How to make an LED blink with python using the GPIO Zero library.

Description

In this tutorial we are going to make an LED blink on and off using a raspberry pi.

Equipment You Will Need

  • 1 x LED
  • 2 x Male to Female jumper wires
  • 1 x Solderless Breadboard
  • 1 x 330 ohms resistor

Making The Circuit

It is through the GPIO (General Purpose Input Output) pins that the raspberry pi can control and even monitor the outside world by being connected into an electronic circuit. The raspberry pi can control LED’s, motors, button presses, and even buzzers.

We are now ready to create our circuit.

  1. Put the LED into the breadboard with the longer leg (the anode) on the right hand side.

  2. Put the resistor into the breadboard in the same column as the short leg (cathode) of the LED leaving room for a jumper wire.

  3. Take one of the jumper wires and connect the end with the pin into the same column as the resistor and the short leg of the LED and connect the other end of the jumper wire to the ground (gnd) pin on the raspberry pi. The ground pin is the 3rd pin down on the right hand side with the USB slots facing you.

  4. Take the second jumper wire and connect the end with the pin into the same column as the longer leg of the LED and the other side to pin 17 on the raspberry pi which is located at the 6th pin down on the left hand side.

Now that we have created the circuit, we can turn the raspberry pi on and get coding.

Code

Our raspberry pi is all booted up so we are now ready to code. To open python go to menu -> programming and click on idle 3. Now we need to open a new file to do this go to file -> new. We can now create our program to do that type the text below. You don’t have to type the text after # as this is a comment in python giving you information about the code.

import gpiozero # importing the gpiozero python library. This is how python talks to the raspberry pi GPIO
import time # importing the time python library

from gpiozero import LED # this imports the LED module from the gpiozero library

led = LED (17) # This is declaring pin 17 as the LED

while True: # This creates a while loop that will run until we tell it to stop
    led.on () # This is turning the LED on
    time.sleep (1) # This tells the program to pause for 1 second 
    led.off () # This turns the LED off
    time.sleep (1) # This tells the program to pause for 1 second 

Running The Code

Before we can run the code we need to save it. To do this go to file -> save as and name the program BlinkLED.py and press enter on the keyboard or click OK with the mouse. Now we can run our code to do this press F5 on your keyboard. You should now see your LED turn on and off in a blinking motion.

Thats all for now.

Keep the Pi out of the cupboard and keep coding!