Raspberry Pi Project: GPIO LED Stoplight

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:mensclub24.ru

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 <a href="http://www.barryhubbard try here.com/raspberry-pi/howto-raspberry-pi-initial-setup/”>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!

led_stoplight_circuit

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.

Ground

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:

led_stoplight_wired_circuit

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!

For Loop

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:

The first line

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:

Now the sample code above will give the following output:

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 try and 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:

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:

and put in the code above to see how it works!

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:

Create a new script by doing the following in a terminal window:

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!

Now you can save your file by typing Ctrl-o and exit nano by typing Ctrl-x

Finally, once you have everything connected to your pi, simply type the following at the terminal:

Watch your stoplight run…have fun!

Leave a Reply

Your email address will not be published. Required fields are marked *