Once you've setup your Raspberry Pi according to my getting started tutorial, you are ready for your first real project. Let's light up an led using the Python programming language and the GPIO pins on your Raspberry Pi, hereafter called RPi.
Before we get around to writing any code, let's first get acquainted with the pin numbering of our RPi and create a simple circuit. We'll start by simply lighting our led using the 3.3v pin and the ground pin on our RPi. We will use the following schematic for our circuit:
Before starting, unplug your RPi. You wouldn't want to risk shorting it out while working with it 'powered on', especially since this is our first project.
Double-check your connections. When you are done, your circuit should look like this:
Power on your RPi - the led should immediately turn on.
Now that we've tested our basic circuit, it's time to move the positive lead from the 'always on' 3.3v pin to one of the programmable GPIO pins. Here is what our circuit will look like:
When you are done, your circuit should look like this:
I like to write my python scripts using the IDLE IDE because it comes packaged with the Raspbian distribution, it's free, and it makes writing and debugging code a bit simpler than when using Python command line or a text editor. It's important to note that when writing python scripts that utilize the GPIO pins, you must run them as a superuser or your scripts will not run properly.
If you are having trouble getting the led to light up, double-check your wiring, and make sure you have installed the GPIO Python library according to my instructions. You can download the completed script here.
Here is a slightly more advanced script that blinks the led on and off. The only real difference is that we are gathering user input and using the sleep function to set the amount of time the light is on or off.
...or you may download the completed script here.
Keep in mind that indentation is very important when writing Python code. Use the TAB key to match the formatting of the above code as you write it.
If you really want to get your feet wet with a more advanced project with a real-world application, check out my irrigation rain bypass project.