Raspberry 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.

pi_wedge_hookup_guide

This tutorial applies to the Raspberry Pi Model B, the Raspberry Pi Model B+ and the new Raspberry Pi 2 Model B.

riving 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.

Some further notes on that bill of materials:

  • Your Raspberry Pi should have an SD card with Raspbian installed on it. Check out our How to Install Raspbian tutorial for help with that.
  • We’re also assuming you have the necessary mouse, keyboard and display hooked up to your Pi.
  • Your Pi will need an Internet connection to download WiringPi. You can use either Ethernet or WiFi (check out our Raspberry Pi WiFi tutorial for help with that.
  • The Pi Wedge isn’t quite required, but it does make life a lot easier. If you want to skip the breakout, you can instead use Male-to-Female jumpers to connect from Pi to breadboard.
  • Of course, feel free to swap in your preferred button and LEDs.

Suggested Reading

This tutorial will assume you have Raspbian installed on your Raspberry Pi. Raspbian is the most popular, well-supported Linux distribution available for the Pi. If you don’t have Raspbian set up, check out our Setting Up Raspbian tutorial before continuing down this rabbit hole.

Other, more general purpose tutorials you might be interested in reading include:

  • Pulse-Width Modulation – You can use PWM to dim LEDs or send signals to servo motors. The RPi has a single PWM-capable pin.
  • Light-Emitting Diodes (LEDs) – To test the output capabilities of the Pi, we’ll be driving a lot of LEDs.
  • Switch Basics – And to test inputs to the Pi, we’ll be using buttons and switches.
  • Pull-Up Resistors – The Pi has internal pull-up (and pull-down) resistors. These are very handy when you’re interfacing buttons with the little computer.

Suggested Viewing

Check out our Raspberry Pi video tutorials if you want a more visual introduction to the Pi!

GPIO Pinout

The Raspberry Pi offers up its GPIO over a standard male header on the board. Over the years the header has expanded from 26 pins to 40 pins while maintaining the original pinout.

highlight

If you’re coming to the Raspberry Pi as an Arduino user, you’re probably used to referencing pins with a single, unique number. Programming the Pi’s hardware works much the same, each pin has its own number…and then some.

There are (at least) two, different numbering schemes you may encounter when referencing Pi pin numbers: (1) Broadcom chip-specific pin numbers and (2) P1 physical pin numbers. You’re usually free to use either number-system, but many programs require that you declare which scheme you’re using at the very beginning of your program.

Here’s a table showing all 26 pins on the P1 header, including any special function they may have, and their dual numbers:

header_pinout

Wedge SilkPython (BCM)WiringPi GPIONameP1 Pin NumberNameWiringPi GPIOPython (BCM)Wedge Silk
3.3v DC Power125v DC Power
SDA8GPIO02 (SDA1, I2C)345v DC Power
SCL9GPIO03 (SCL1, I2C)56Ground
G447GPIO04 (GPIO_GCLK)78GPIO14 (TXD0)15TXO
Ground910GPIO15 (RXD0)16RXI
G17170GPIO17 (GPIO_GEN0)1112GPIO18 (GPIO_GEN1)118G18
G27272GPIO27 (GPIO_GEN2)1314Ground
G22223GPIO22 (GPIO_GEN3)1516GPIO23 (GPIO_GEN4)423G23
3.3v DC Power1718GPIO24 (GPIO_GEN5)524G24
MOSI12GPIO10 (SPI_MOSI)1920Ground
MISO13GPIO09 (SPI_MISO)2122GPIO25 (GPIO_GEN6)625G25
(no worky 14)GPIO11 (SPI_CLK)2324GPIO08 (SPI_CE0_N)10CD0
Ground2526GPIO07 (SPI_CE1_N)11CE1
IDSD30ID_SD (I2C ID EEPROM)2728ID_SC (I2C ID EEPROM)31IDSC
G05521GPIO052930Ground
G6622GPIO063132GPIO122612G12
G131323GPIO133334Ground
G191924GPIO193536GPIO162716G16
G262625GPIO263738GPIO202820G20
Ground3940GPIO212921G21

This table shows the Pi pin header numbers, element14 given names, wiringPi numbers, Python numbers, and related silkscreen on the wedge.

Note: The Broadcom pin numbers above relate to Pi Model 2 and later only. If you have an older Rev1 Pi, check out this link for your Broadcom pin numbers.

As you can see, the Pi not only gives you access to the bi-directional I/O pins, but also Serial (UART), I2C, SPI, and even some PWM (“analog output”).

Hardware Setup

To get a head start you can assemble the circuit now. We’ll use this setup for both the C and Python examples. We’ll use two LEDs to test the output functionality (digital and PWM), and a button to test the input.

Read More:  Raspberry gPIo




© 2015 Powered By Engineering Projects Team, Raspberry Pi Projects

Scroll to top