Skip to content

Instantly share code, notes, and snippets.

@ChickenProp
Created July 4, 2012 23:35
Show Gist options
  • Save ChickenProp/3050085 to your computer and use it in GitHub Desktop.
Save ChickenProp/3050085 to your computer and use it in GitHub Desktop.
Simple Raspberry Pi GPIO example

Introduction

This is a dead-simple way to test that GPIO on the Raspberry Pi is working. I have an SKPang Raspberry Pi starter kit A and some M/M jumper wires. But all you need is

  • A Raspberry Pi.
  • An LED.
  • A button.
  • A resistor, approximately 270Ω.
  • Some way to connect these to each other and the GPIO pins. I used a breadboard, three M/F wires (these come with the starter kit A) and one M/M wire (these come with the larger starter kit B, or you can get them seperately, or probably just do without).

In particular, you don't need to download any software, not even using your package manager. (I'm assuming you already have bash.)

My main sources for this are the elinux page on Rpi low-level peripherals and Gordon Henderson's Tux Crossing.

A glowing LED

We start by not using GPIO at all, and just powering an LED from the Rpi. Refer to this diagram of the pin layout. Pin 1 is the one closest to the SD card; pin 2 is adjacent to pin 1 on the short side, pin 3 is adjacent to pin 1 on the long side, and so on. If the SD card is "top" and the HDMI port is "left", then numbering proceedes left-to-right and top-to-bottom as if you were reading a book.

Pin 1 provides a constant 3.3V, and pin 6 is ground. So connect an LED and a resistor in between these, and the LED should light up. The + terminal of the LED needs to be connected to pin 1, and the - terminal to pin 6. The - terminal is the one with the shorter leg, and also the one where the rim of the cap is slightly flattened.

If you have the starter kit, here's one way you can create that circuit. Attach pin 1 to 1f on the breadboard. Place the LED with + connected to 1g and - connected to 2g. Place the resistor between 2h and the - strip on the right hand side of the breadboard. Connect another terminal on that strip to pin 6.

(The connections in a breadboard work like so: the rows are numbered, and each row has two sets of five columns taking letters a through j. On each row, a through e are all connected, and so are f through j. None of the rows are interconnected. Each side of the breadboard has a + column in which every terminal is connected, and a - column in which every terminal is connected. The purpose of these is to connect them to a source of power or grounding which can then be supplied to the whole board.)

Having done that, the LED should light up. If not, figure out why not and come back.

A controllable LED

Now we'll make it so you can turn the LED on and off. Move the wire from pin 1 to pin 11, which is GPIO pin 17. (I have no idea why the numbers are as they are.)

The LED will be off, but we can turn it on from the command line. Pull up a root shell (using sudo will become tedious), and type:

cd /sys/class/gpio
echo 17 > export
echo out > gpio17/direction
echo 1 > gpio17/value

The LED should turn on. The export line seems to be telling the kernel to turn on that pin; without it, the gpio17 directory doesn't exist. The direction line says GPIO pin 17 will be used for output; the value line turns it on.

To turn it off, echo 0 > gpio17/value.

(To be continued...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment