Raspberry Pi tutorial: Flashing LED
Ok so in this tutorial I’m going to go through what I went through to get a raspberry pi to produce a flashing LED. Since most tutorials go through only software, assuming you understand the hardware, and I didn’t and had parental guidance, I’m going to attempt to explain how we produced the hardware aswell. *wish me luck* This assumes you know absolutely sod all about hardware, but that you’re old enough and sensible enough to know how not to destroy your raspberry pi and/or solder your fingers together. This works with the GPIO pins which could literally destroy your pi so I take no responsibility. All I know is mine works.
Ok so Hardware Ingredients:
- Raspberry pi (duh). Mine cost £30, buyable from farnell, depending on where you live and whether they’re still in stock mine came after 2 days, unlike some people who ordered months ago and took 3 months or so.
- 26 pin ribbon cable
- some wire lengths which are thin enough to fit the holes of a breadboard
- Solder wire
- an LED
- a breadboard
- some lengths of protective tubing to protect the connections between wire and flat cable
- A soldering iron
- a hot air blowy thing (seriously I have no idea what it’s called, it shrinks the protective tubing around the connections if that helps?)
- A craft knife
- a board to use the craft knife on
- a pair of plyers/implements to cut wire and strip the plastic off.
Addons which are unrelated:
The case shown in the photo was made from 2 pieces of perspex and designed so that wires would be more balanced. It was made from 4 spacers, the perspex mentioned and several screws, and has cutout sections over the pins and the USB socket.
Actual tutorial: I should note the soldering is only because we wanted to ensure the wires were long enough and would fit into the breadboard. If yours will fit already skip the soldering steps.
Okay so step 1 – take the flat cable, cut the wires apart on one end – in this sense I mean take the craft knife and cut about a centimeter down the valley between the wires from the edge of the wire. Then rip these apart a little more so they’re usable. It’s advised to cut them into 5s first because otherwise the process gets messy.
Step 2 – strip about half a centimeter of plastic off each wire at the end you cut. Do this also with a length of wire per wire of the cable. On each wire of the cable, twist the wires together to ensure they don’t split appart, and solder the wire to hold them in this position. If the length of wire isn’t already held into a single position, you should probably solder that one too.
Step 3 – solder together the stripped end of the wire and the stripped end of wire on the cable. It took me hours to get this right because I’m inexperienced – my suggestion is to get a clamp of some sort to hold the cable wire, then put a bit of solder onto each wire (so repeat step 2 with each wire), and while it’s still melted, hold them together. Hold them in place like that until obviously, the solder wire has cooled around the connection. Take a length of tubing which is about a centimeter or 2 longer than the connection you just made, and slide it over the soldered connection. Take your hot air blowy thing (official term) and well…blow hot air over the tubing. Repeat this for each cable wire.
Step 4 – if you haven’t already (because I didn’t tell you to), strip the other end of the wire length and plug each one into the breadboard. I have absolutely no clue how they work, my dad did this part, so you’re going to have to look into using breadboards and making circuits with them. For reference, here’s how ours is wired:
Step 5 – clip the other end of your cable onto the GPIO pins on the raspberry pi. Make sure your raspberry pi is switched off before you do this – also I’m assuming you’ve set up the operating system how you like it.
Step 6 – turn your raspberry pi on. At this point we need to install a few libraries, and the information online is severely confusing so I went through multiple installs and commands to get to this. To my knowledge, this is what we used:
- Open up a terminal, so press start > accessories > LXTerm or root terminal. If you’re running root, the word “sudo” is unnecessary in the commands. Also this’ll be slightly different if you’re not running raspberry wheezy.
- first run this:
sudo apt-get install python-dev
This will install some development libraries for I/O in python
- Next, run this:
wget http://pypi.python.org/packages/source/R/RPi.GPIO/RPi.GPIO-0.3.1a.tar.gz tar zxf RPi.GPIO-0.3.1a.tar.gz cd RPi.GPIO-0.3.1a sudo python setup.py install
I ran into various problems with “python.h” not existing, but hopefully the first packet we installed should mean you don’t get my problems. Google has the answer if not
- That’s all your libraries done.
Step 7 – actual code. Right, personally I don’t like the 2 python shell programs, IDLE and IDLE 3 because they’re a bit like using notepad to code which is fine for HTML (I prefer that to dreamweaver which is too complex) but a bit annoying for other code when you’re used to Visual Studio. I personally went with geany, which is an IDE something similar to borland C++ and visual studio but not quite as clever. It’s up to you, but if you want to go with my route, in your terminal enter:
sudo apt-get install geany
which will get the geany package and install it. After that, obviously open up geany which on raspberian wheezy should appear on the programming sub menu from start menu. When it’s up, click new (from template) > main.py. Geany adds some unneccesary commentary, so delete everything in the file other than the very very first line.
Something I need to mention: the following code you will need to run as root, because we’re accessing GPIO. To do this, if you have root set up, logout of your current account and login as root, or in a terminal enter sudo su, and then passwrd and enter a root password. Note that UNIX doesn’t show any form of character entry when you enter a password. Logout and login using root as the username, and the password you entered as the password.
Here is my code:
import RPi.GPIO as GPIO, time GPIO.setmode(GPIO.BOARD) GPIO.setup(11, GPIO.OUT) while True: GPIO.output(11, True) time.sleep(1) GPIO.output(11, False) time.sleep(1)
The code is fairly self explanatory but a quick line by line explanation:
Line 1 imports your libraries and tells the compiler we’re going to use the GPIO library without the prefix RPi
Lines 2 and 3 are set up telling the program how we’re using the GPIO and which pins to allow us access to
Line 4 keeps the program from ever ending.
Lines 5-8 output our flashes and put a 1 second pause between each one.
Notes about python: the compiler or else Geany won’t accept “true” inplace of “True”. To me that seems really dumb, but whatever. Also, braces do not exist, but indentation is incredibly important. Again remember, as with any program, the program will stop when it’s ran each line if you don’t add a loop, hence why we have “while True:” in there.
Obviously, you can change the pin number (the number inside output and setup) to whatever pin you want, but ensure you check how the pins are set up for Raspberry Pi because GPIO 11 is technically like 17 or something? something weird. Again, I don’t know how breadboards work, so that part I’ll let you figure out how to plug your LED in.
The interval inside time.sleep can be changed to any double, as far as I’ve played with. At the moment, it will flash every second.
Once I have keyboard input figured out, my next tutorial will be on making it flash on keypress, and then on GPIO button press. I hope this helped.