LED On

Project

Learning to control an LED through python using the GPIO Zero library

Description

In this tutorial I am going to show you how to turn an LED on using a raspberry pi and python.

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 can ignore the text after # as this is a comment in python which is telling you what the code does.

import gpiozero # importing the gpiozero python library. This is how python talks to the raspberry pi GPIO
from gpiozero import LED # this imports the LED module from the gpiozero library
led = LED(17) # This is declaring pin 17 as the LED
led.on() # This is turning the LED on

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 LedOn.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 turned on.

Thats all for now.

Keep the Pi out of the cupboard and keep coding!