Controlling LED Lights using Raspberry Pi and Python (GPIO Beginner)
I’ve always been a software guy, and I’ve never worked much with anything hardware related. Because of this, I admit I’m a little lost when it comes to GPIO and building Raspberry Pi things in general. The hardest part for me is always the setup of the hardware so I can get to what I do best.
In this example, I’m using the Canakit Ultimate Raspberry Pi Starter Kit, and specifically these parts:
- 1 x Raspberry Pi2 Model B 1GB
- 1 x Breadboard
- 1 x 40-pin GPIO Ribbon Cable
- 1 x 40-pin T-Shaped GPIO to Breadboard Interface Board
- 1 x Red LEDs
- 1 x 180 Ohm Resistors
- 1 x Jumper
You can see my set-up below. You can ignore the camera I have connected to the Raspberry Pi!
Let’s start with inserting the T-Shaped GPIO to the Breadboard. I was a little confused on this, so I’m including a couple of pictures.
You want the 4 prongs on the outside of the “T” to go in the second row from the top, in the two columns with the +/- signs at the top:
Here’s what the bottom alignment looks like:
Now just push it in:
Next, connect the ribbon from the breadboard to the RPi GPIO pins, like so:
Next up we’ll put an LED bulb into the breadboard. I used rows 25+26, but that doesn’t really matter. What does matter, however is realizing which side is which on the LED.
- IMPORTANT: The longer end is the “positive end, and the short end is the negative end.
In this case, I’m putting the longer/positive end on 25, and the shorter/negative end on 26, like so:
Next, let’s connect a resistor between the negative column and somewhere on row 26. All of the holes on the left side of the row are connected, so just put it in one of the holes. The other goes to the negative hole in the negative-column:
Almost there! Now we just need to put a jumper cable in between our positive row and a row aligned with the T-Shaped GPIO to Breadboard Interface Board. So in this case I’m connecting the jumper from row 26 to row 8, which corresponds to GPIO pin 22 on the Interface Board:
22 is the key number here, not 8. 22 is the GPIO pin we’ll be controlling via Python. Here’s what everything looks like assembled:
Now for the software! I’m going to skim over the set-up of the RPi. The steps are well documented and all can be easily googled with pretty clear instructions. The initial steps for me are (roughly):
- Connect the RPi to Wi-Fi so I can SSH into it from my computer. Connecting to Wi-Fi is pretty easy and can be done via the GUI or command line
- SSH in to the Raspberry Pi and start installing everything for Python. In my case, I use iPython, which is also easy to set-up and instructions are everywhere.
- Install RPi.GPIO:
- sudo apt–get install python–dev python–rpi.gpio
From here, I’m using iPython to control the LED in realtime. It only takes a couple lines and is very simple!
import RPi.GPIO as GPIO
GPIO.output(22,GPIO.HIGH) # Turn on
GPIO.output(22,GPIO.LOW) # Turn off
And there you have it! You can see my setup below.