Getting started with GPIO on the Raspberry Pi

Although I’m fairly competent with Linux and programming, I’m a complete newbie when it comes to hardware hacking. I did a bit of electronics theory in A Level Physics (over 20 years ago – and yes, kids, the transistor had been invented then!), but there was no associated practical work, and I’ve never followed up on it. So to keep things nice and simple, I ordered this Electronic Starter Kit for Raspberry Pi from Amazon which seems ideal for a beginner like me. However, it did not come with any kind of documentation. It is simple enough to figure out how to assemble the case for the Pi, just by looking at the photo. One word of caution however, is that this case is not designed for use with composite video output. It is just about possible, but awkward and inconvenient. It is fine used headless or with HDMI output.

Getting started with GPIO is covered in the In Control column of the excellent MagPi magazine. Starting with Issue number 2, the column is showing how to build some simple GPIO circuits. The starter kit mentioned above contains just about everything necessary to build the examples covered in issues 2 and 3, though the resistors are a bit different, and the supplied breadboard has a different layout. All that were missing were a few wire links which I made with some spare wire I had lying around the house. The instructions given in the column are fairly clear and easy to follow so long as you have a basic understanding of electricity at the “V=IR” kind of level. You should be able to roughly make out how I mapped out the circuit from the photo below.

Imag0024

I’d never used a breadboard before – they are great! All you need to know is that the outside “rails” are connected as a full column, and that the central part of the board is wired in rows, with the two halves isolated from one another. Then just make circuits by plugging components into the board. I was never very good at soldering, anyway… Regarding resistors, I found this electronic colour code page to be helpful. My starter kit contained a dozen 27 Ohm resistors and a couple of 10k Ohm resistors. This page on LEDs was also useful. Connecting a 27 Ohm resistor in series with an LED is just enough to bring the current down to a tolerable level.

The other thing to be careful about is the orientation of the Pi board regarding the pin labelling. If you are unsure, the board helpfully has a label “P1” next to Pin 01, which is the 3.3V positive terminal. If you want to go beyond the tutorials in the MagPi magazine, I found the elinux page on RPi Low-level peripherals to be very useful.

The circuit described in Issue 2 of the MagPi magazine worked fine for me, and the instructions for installing the Python GPIO library were very clear. The indentation of the python code for the mybutton.py example didn’t look quite right to me, so I used the code below, which worked as expected:

#!/usr/bin/pythonimport timeimport RPi.GPIO as GPIOGPIO.setup(11,GPIO.IN)while True:        mybutton=GPIO.input(11)        if mybutton==False:                print "Press"        time.sleep(0.2)
Advertisements

Published by

darrenjw

I am Professor of Stochastic Modelling within the School of Mathematics & Statistics at Newcastle University, UK. I am also a computational systems biologist.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s