Turning On An Led With Your Raspberry Pi
Turning On An Led With Your Raspberry Pi
• A Breadboard
• An LED
• A 330 ohm resistor
• 2x Male to Female jumper wires
The Breadboard
The breadboard is a convenient way to connect electronic
components to each other without having to solder them
together.
With the breadboard, the top row of holes are all connected
together, marked with a red line.
And so are the second row of holes, marked with a blue line.
The same goes for the matching two blue/red rows of holes at the
bottom of the breadboard.
In the middle, the columns of holes are connected together with
a break in the middle (which comes in handy if you straddle a chip
or similar component across the two sides, allowing you to
connect to both sides of a chip with wires, without the chip's legs
connecting together)
The LED
When you pick up the LED, you will notice that one leg is
longer than the other.
LEDs will only work if power is supplied the correct way round (i.e.
if the ‘polarity’ is correct).
You will not break the LEDs if you connect them the wrong way
round, they will just not light.
If you find that they do not light in your circuit, it may be because
they have been connected the wrong way round.
The Resistor
Jumper Wires
The ones you will be using in this circuit have different connectors
on each end. The end with the ‘pin’ will go into the Breadboard.
The end with the piece of plastic with a hole in it will go onto the
Raspberry Pi’s GPIO pins.
The diagram below left shows the pin layout for the older 26-pin
Raspberry Pi Models which are no longer sold.
The latest 40-pin Raspberry Pi’s share the same layout of pins for
the top 13 rows as the old model.
You should turn your Raspberry Pi off for the next bit, just in case
you accidentally short something out.
• Use one of the jumper wires to connect a ground pin to the
rail, marked with blue, on the breadboard. The female end
goes on the Raspberry Pi's pin, and the male end goes into
a hole on the breadboard.
• Then connect the resistor from the same row on the
breadboard to a column on the breadboard, as
shown above.
• Next, push the LEDs legs into the breadboard, with the
long leg (with the kink) on the right.
• Lastly, complete the circuit by connecting the right hand
leg of the LED to GPIO18. This is shown here with the blue
wire.
The Code
You are now ready to write some code to switch the LED on. Turn
on your Raspberry Pi and open the terminal window.
nano LED.py
Once you have added all the code and checked it, save and
exit the text editor with “Ctrl + x” then “y” then “enter”.
You will see the LED turn on for a second and then turn off.
If your code does not run and an error is reported, edit the code
again using nano LED.py.
Explanation
So, what is happening in the code? Let’s go through it a line at a
time:
The first line tells the Python interpreter (the thing that runs the
Python code) that it will be using a ‘library’ that will tell it how to
work with the Raspberry Pi’s GPIO pins.
import time
Imports the Time library so that we can pause the script later on.
GPIO.setmode(GPIO.BCM)
This turns the GPIO pin ‘on’. What this actually means is that the
pin is made to provide power of 3.3volts. This is enough to turn
the LED in our circuit on.
time.sleep(1)
This turns the GPIO pin ‘off’, meaning that the pin is no longer
supplying any power.
And that's it! You are now able to turn an LED on and off