An introduction to GPIO and physical computing on the Raspberry Pi




One powerful feature of the Raspberry Pi is the row of GPIO (general purpose input/output) pins along the edge of the board, next to the yellow video out socket.

These pins are a physical interface between the Pi and the outside world. At the simplest level, you can think of them as switches that you can turn on or off (input) or that the Pi can turn on or off (output). Seventeen of the 26 pins are GPIO pins; the others are power or ground pins.

An introduction to GPIO and physical computing on the Raspberry Pi

What are they for? What can I do with them?

You can program the pins to interact in amazing ways with the real world. Inputs don’t have to come from a physical switch; it could be input from a sensor or a signal from another computer or device, for example. The output can also do anything, from turning on an LED to sending a signal or data to another device. If the Raspberry Pi is on a network, you can control devices that are attached to it from anywhere** and those devices can send data back. Connectivity and control of physical devices over the internet is a powerful and exciting thing, and the Raspberry Pi is ideal for this. There are lots of brilliant examples of physical computing on our blog.

Note: Not literally anywhere, of course. You need things like access to the network, a network capable computing device, and electricity. Please do not write to us to point this out. πŸ™‚

How the GPIO pins work

Output

WARNING: If you follow the instructions, then messing about with the GPIO is safe and fun. Randomly plugging wires and power sources into your Pi, however, may kill it. Bad things can also happen if you try to connect things to your Pi that use a lot of power; LEDs are fine, motors are not. If you are worried about this, then you might want to consider using a breakout board such as the Pibrella until you are confident enough to use the GPIO directly.

Ignoring the Pi for a moment, one of the simplest electrical circuits that you can build is a battery connected to a light source and a switch (the resistor is there to protect the LED):

When we use a GPIO pin as an output, the Raspberry Pi replaces both the switch and the battery in the above diagram. Each pin can turn on or off,or go HIGH or LOW in computing terms. When the pin is HIGH it outputs 3.3 volts (3v3); when the pin is LOW it is off.

Here’s the same circuit using the Raspberry Pi. The LED is connected to a GPIO pin (which can output +3v3) and a ground pin (which is 0v and acts like the negative terminal of the battery):

The next step is to write a program to tell the pin to go HIGH or LOW. Here’s an example using Python (see Step 2), and here’s how to do it in Scratch.

An introduction to GPIO and physical computing on the Raspberry Pi

Input

GPIO outputs are easy; they are on or off, HIGH or LOW, 3v3 or 0v. Inputs are a bit trickier because of the way that digital devices work. Although it might seem reasonable just to connect a button across an input pin and a ground pin, the Pi can get confused as to whether the button is on or off. It might work properly, it might not. It’s a bit like floating about in deep space; without a reference it would be hard to tell if you were going up or down, or even what up or down meant!

This is why you will see phrases like “pull up” and “pull down” in Raspberry Pi GPIO tutorials. It’s a way of giving the input pin a reference so it knows for certain when an input is received.

If you’d like to have a go at using the GPIO as an input then have a look at our screaming jelly baby and quick reaction game tutorials for Python, or a reaction game for Scratch.

The end of the guide. The start of something amazing

We hope that this has encouraged you to have a go at physical computing using the Pi’s GPIO; it’s really not as daunting as it looks. It all starts with a simple LED, but it can take you to incredible places. Do not underestimate the fun, creativity and sense of achievement you can get from a little computer and a bunch of pins. Have fun! And if you do make something cool please let us know. πŸ™‚

 

For more detail: An introduction to GPIO and physical computing on the Raspberry Pi




Β© 2015 Powered By Engineering Projects Team, Raspberry Pi Projects

Scroll to top