Tuesday, April 8, 2014

GPIO and the Raspberry PI

As we approach the day of full Raspberry PI integration, we have a few things to consider beyond a typical Linux machine like we have on our Ubuntu partitions of our laptops. The Raspberry PI has GPIO pins (General Purpose Input/Output) that are going to be the key to turning these lights on and off. But aside from that, all the rest of our software should work just fine on the PI.

So how do we use these legendary GPIO pins? Well, in my experience from past projects, you can use the complicated way with no software and echoing values directly to the pins, or you can use the easy way where someone has automated the whole process for you and you can control every pin with ease. The software package we are using to make our lives easier is called Wiring PI which can be found at... That's right! you guessed it: http://wiringpi.com . The download and install instructions are on that webpage under "download and install" and they are very clear.

This software package was developed by Gordon Henderson (Twitter: @drogon) and he's made it a lot easier to interact with the Raspberry PI's GPIO interface. Using WiringPI, I am able to run the command:

gpio readall

I will see a chart of all the pins on my PI and their I/O modes and current states:


Now, right now, this probably just looks like gibberish but don't worry, it's actually quite easy. If you look at the "wiringPi" column, you will see the numbers 0 through 20. These are the pins we will be using will probably not go any higher than 7. And in the example above, under "Mode" you can see that each of these pins are on input mode. Now, for us, this is kind of a problem. We don't want to simply wait and observe. We want to MAKE something happen! So the way we make this happen is by putting the pins in question into OUTPUT mode. Let's start with pin 0 because the process is the same for all pins. You can use the command:

gpio mode 0 out

The pin is now in output mode. To check, re-run:

gpio readall

Or, since you are only dealing with one pin, I will introduce you to the command to only read the state of a single pin. It is:


gpio read 0

In our case, we say "read 0" because we are working with pin 0. This will change with your target pin.

A WORD OF CAUTION!
The pins on the Raspberry PI are not buffered. This means that if you have a short circuit or a voltage overload, you can fry not only the pins, but the entire Raspberry PI board! Check, double check, and triple check your wiring before switching it on and NEVER wire things up with the board powered on! This is simply good practice for any type of electronic prototyping because it will most likely eliminate some CRIPPLING mistakes.

No comments:

Post a Comment