All about Raspberry Pi GPIO

Introduction

Relative to its size the Raspberry Pi is a powerhorse of a computer – it can drive HDMI displays, process mouse, keyboard, and camera inputs, connect to the Internet, and run full-featured Linux distributions. But it’s more than just a small computer, it’s a hardware prototyping tool! The Pi has bi-directional I/O pins, which you can use to drive LEDs, spin motors, or read button presses.
This tutorial applies to the Raspberry Pi Model B, the Raspberry Pi Model B+ and the new Raspberry Pi 2 Model B.
Driving the Raspberry Pi’s I/O lines requires a bit of programming. Programming in what language? Take your pick! A quick glance at the Raspberry Pi GPIO examples shows that there are dozens of programming-language-choices. We’ve pared that list down, and ended up with two really solid, easy tools for driving I/O: Python and C (using the WiringPi library).
If you’ve never driven an LED or read in a button press using the Raspberry Pi, this tutorial should help to get you started. Whether you’re a fan of the easily-readable, interpretive scripting language Python or more of a die-hard C programmer, you’ll find a programming option that suits our needs.

Covered In This Tutorial

In this tutorial we’ll show two different approaches to reading and driving the Raspberry Pi’s GPIO pins: python and C. Here’s a quick overview of what’s covered:

  • GPIO Pinout – An overview of the Pi’s GPIO header.
  • Python API and Examples
    • RPi.GPIO API – An overview of the Python functions you can use to drive GPIO.
    • RPi.GPIO Example – An example Python script that shows off both input and output functionality.
  • C (and WiringPi) API and Examples
    • WiringPi Setup and Test – How to install WiringPi and then take it for a test drive on the command line.
    • WiringPi API – An overview of the basic functions provided by the WiringPi library.
    • WiringPi Example – A simple example program that shows off WiringPi’s input and output capabilities.
  • Using an IDE – How to download and install Geany. Our favorite IDE for programming on the Raspberry Pi.

Each programming language has it’s share of pros and cons. Python is easy (especially if your a programming novice) and doesn’t require any compilation. C is faster and may be easier for those familiar with the old standby.

What You’ll Need

Here’s a wishlist-full of everything we used for this tutorial.