Note: Please remember that these tutorials are provided for educational purposes only. In many cases, the wiring described is designed in a way to produce a quick result and experiment with the Pi. The circuits do not contain protection either for the circuit or the Pi and it is always possible that equipment could be damaged by following these directions! YOU HAVE BEEN WARNED…Now, let’s have some fun:
LED Stoplight for my Pi!
Now that you have your Raspberry Pi, hopefully you have had a chance to play a little with it. If you haven’t already, you should take a look at the tutorial on how to initially setup your Pi with Raspbian at http://www.barryhubbard.com/raspberry-pi/howto-raspberry-pi-initial-setup/. Also, you should take a look at how to get an LED wired into a GPIO port at http://www.barryhubbard.com/raspberry-pi/raspberry-pi-project-led/, which also includes a basic introduction to electronics.
If you have already followed through the previous tutorials, now it’s time to make a stoplight!
As you can see, this is the same basic circuit…times three. Each LED has its own current limiting 1kΩ resistor and each is connected to its own GPIO pin for its 3.3v supply. This allows us to control each LED independently.
You will notice that the ground (negative) side for all of the LED’s are connected together. These have a “common ground” which matches the ground on the board. Regardless of where the power comes from, it goes back into the same ground pin on our Pi!
When wired up, your circuit should look like this:
Now it’s time to move onto the code that will make this work. Again, we will be working in Python as a scripting language. For a brief introduction, check out the LED Pi project at: http://www.barryhubbard.com/raspberry-pi/raspberry-pi-project-led/
There are few new tools that we are going to introduce that allow you to have a little more fun with programming!
Let’s say you want your program to do the same thing multiple times, but you don’t want to type the same thing over and over and over again. That’s why there are “for” loops, which will repeat a given “block” of code.
A simple “for” loop looks like this:
for i in range(0,5): print i print "moving on" print "done"
The first line
for i in range(0,5):
established our “for” loop. It defines a variable ‘i’ and assigns it an initial value of 0. Each time the loop repeats, the value of ‘i’ will increase by 1. The loop will continue to execute as long as ‘i’ is less than 5. Remember to put the ‘:’ after your for loop so that Python knows where your loop starts! Otherwise, you will get an “Indent error” on the next line of code when you try to run your script
Python recognizes “blocks” of code by how many spaces are in front of the first character. Therefore, in this example, the block of code that is included in the for loop and will repeat is:
print i print "moving on"
Now the sample code above will give the following output:
0 moving on 1 moving on 2 moving on 3 moving on 4 moving on done
Notice that the loop stops after 4, since at that point, ‘i’ is no longer less than 5 (it equals 5!).
Try / Except
In this project, we are also going to use a
except statement in our Python script. These statements allow us to “catch” problems and interrupts (such as someone terminating the program) and cleanly exit our code.
You can always stop a program that is running at the shell by typing Ctrl-C. Typically, this will stop the program immediately, without it executing any more commands. Consider the following example:
try: for i in range (0,5000): print i except KeyboardInterrupt: print "Got tired...did you?"
In this code, the for loop will attempt to count from 0 to 4999. If the user hits Ctrl-C before the code finishes, a “Keyboard Interrupt” is generated, and then next block of code will execute, asking the user if they have gotten tired! If the user waits until the script counts to 5,000, the message will not be displayed!
Create a new script called “test.py” by doing the following:
$ nano test.py
and put in the code above to see how it works!
The stoplight.py script
Now that we have learned a little about “for” loops, code blocks, and try statements, let’s put it all together. Let’s review how our circuit is wired:
- Red – GPIO 18 – Pin 12
- Yellow – GPIO 27 – Pin 13
- Green – GPIO 22 – Pin 15
Create a new script by doing the following in a terminal window:
$ nano stoplight.py
Type the following to create your stoplight.py file. Remember to be careful of all spaces and make sure things are spelled right…or you will get an error!
#!/usr/bin/python import RPi.GPIO as GPIO import time # Setup the GPIO ports # Use the pin numbers as they appear on the board GPIO.setmode(GPIO.BOARD) GPIO.setup(12, GPIO.OUT) #Red LED GPIO.setup(13, GPIO.OUT) #Yellow LED GPIO.setup(15, GPIO.OUT) #Green LED GPIO.output(12, False) GPIO.output(13, False) GPIO.output(15, False) # Run 5 times try: for i in range(0,5): # Each cycle should: # 1. make the stoplight green for 5 seconds # 2. turn the light yellow for 1 second # 3. turn the light red for 5 seconds # turn the red light off and green light on GPIO.output(12, False) GPIO.output(15, True) # wait 5 seconds time.sleep(5) # turn the green light off and the yellow light on GPIO.output(15, False) GPIO.output(13, True) # wait 1 second time.sleep(1) # turn the yellow light off and the red light on GPIO.output(13, False) GPIO.output(12, True) # wait 5 seconds time.sleep(5) # cleanup the GPIO loose ends GPIO.cleanup() except Keyboard Interrupt: # cleanup the GPIO loose ends GPIO.cleanup()
Now you can save your file by typing
Ctrl-o and exit nano by typing
Finally, once you have everything connected to your pi, simply type the following at the terminal:
$ python stoplight.py
Watch your stoplight run…have fun!