Advertisement

Quick Links

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.
This tutorial applies to the Raspberry Pi Model B, the Raspberry Pi Model
B+ and the new Raspberry Pi 2 Model B.
Example Pi Wedge on a 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
Page 1 of 17

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the Raspberry gPIo and is the answer not in the manual?

Questions and answers

Summary of Contents for sparkfun Raspberry gPIo

  • Page 1 Page 1 of 17   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...
  • Page 2 Who wants pi? The Raspberry Pi has made quite a splash since it wa… SparkFun Pi Wedge KIT-12652 This is the SparkFun Pi Wedge, a small board that connects to the Ra… (2) Breadboard - Self-Adhesive (White) PRT-12002 This is your tried and true white solderless breadboard. It has 2 power …...
  • Page 3: Suggested Reading

    Page 3 of 17 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.
  • Page 4 Page 4 of 17 Element14 pin description, annotated Wedge Python WiringPi P1 Pin WiringPi Python Wedge Name Name Silk (BCM) GPIO Number GPIO (BCM) Silk 3.3v DC 2 5v DC Power Power GPIO02 4 5v DC Power (SDA1, I2C) GPIO03 Ground (SCL1, I2C) GPIO04...
  • Page 5: Hardware Setup

    Page 5 of 17 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), I C, SPI, and even some PWM (“analog output”).
  • Page 6 Page 6 of 17 Our two LEDs are connected to the Pi’s GPIO 18 and GPIO 23 – those are the Broadcom chip-specific numbers. If you’re basing your wiring off the P1 connector pin numbers, that’d be pins 12 and 16. The button is connected to Broadcom GPIO 17, aka P1 pin 11.
  • Page 7 Page 7 of 17 function. For example… GPIO.setmode() GPIO.setmode(GPIO.BCM) …will activate the Broadcom-chip specific pin numbers. Both the lines of code are required, if you want to import setmode use Python. Setting a Pin Mode If you’ve used Arduino, you’re probably familiar with the fact that you have to declare a “pin mode”...
  • Page 8 Page 8 of 17 If a pin is configured as an input, you can use the GPIO.input([pin]) function to read its value. The function will return either a input() True indicating whether the pin is HIGH or LOW. You can use an False statement to test this, for example…...
  • Page 9 Page 9 of 17 1. Create a File To begin, we need to create a Python file. You can do this through the GUI- based file explorer. Or, if you want a terminal-based solution, open up LXTerminal, and navigate to a folder you’d like the file to live (or create one).
  • Page 10: Running The Script

    Page 10 of 17 # External module imports import RPi.GPIO as GPIO import time # Pin Definitons: pwmPin = 18 # Broadcom pin 18 (P1 pin 12) ledPin = 23 # Broadcom pin 23 (P1 pin 16) butPin = 17 # Broadcom pin 17 (P1 pin 11) dc = 95 # duty cycle (0­100) for PWM pin # Pin Setup: GPIO.setmode(GPIO.BCM) # Broadcom pin­numbering scheme GPIO.setup(ledPin, GPIO.OUT) # LED pin set as output GPIO.setup(pwmPin, GPIO.OUT) # PWM pin set as output pwm = GPIO.PWM(pwmPin, 50)  # Initialize PWM on pwmPin 100Hz f requency GPIO.setup(butPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Butto n pin set as input w/ pull­up # Initial state for LEDs: GPIO.output(ledPin, GPIO.LOW) pwm.start(dc) print("Here we go! Press CTRL+C to exit") try: while 1: if GPIO.input(butPin): # button is released             pwm.ChangeDutyCycle(dc)             GPIO.output(ledPin, GPIO.LOW) else: # button is pressed:...
  • Page 11 Page 11 of 17 Python is a great GPIO-driving option, especially if you’re used to it. But if you’re a rickety old programmer, unfamiliar with the whitespace-driven scripting language, and would rather live within the happy confines of C, then let me introduce the WiringPi library. 1) Install Wiring Pi WiringPi is not included with Raspbian, so, to begin, you’ll need to download and install it.
  • Page 12: Digital Output

    Page 12 of 17 #include <wiringPi.h> After you’ve included the library, your first steps should be to initialize it. This step also determines which pin numbering scheme you’ll be using throughout the rest of your program. Pick one of these function calls to initialize the library: wiringPiSetup(); // Initializes wiringPi using wiringPi's siml ified number system.
  • Page 13 Page 13 of 17 Pull-Up/Down Resistors Need some pull-up or pull-down resistors on your digital input? Use the function to pull pullUpDnControl([pin], [PUD_OFF, PUD_DOWN, PUD_UP]) your pin. For example, if you have a button on pin 22 and need some help pulling it up, write: pullUpDnControl(17, PUD_UP);...
  • Page 14 Page 14 of 17 #include <stdio.h>    // Used for printf() statements #include <wiringPi.h> // Include WiringPi library! // Pin number declarations. We're using the Broadcom chip pin  numbers. const int pwmPin = 18; // PWM LED ­ Broadcom pin 18, P1 pin 12 const int ledPin = 23; // Regular LED ­ Broadcom pin 23, P1 pi n 16 const int butPin = 17; // Active­low button ­ Broadcom pin 1 7, P1 pin 11 const int pwmValue = 75; // Use this to set an LED brightness int main(void) {  // Setup stuff: wiringPiSetupGpio(); // Initialize wiringPi ­­ using Broad com pin numbers pinMode(pwmPin, PWM_OUTPUT); // Set PWM LED as PWM output pinMode(ledPin, OUTPUT);     // Set regular LED as output pinMode(butPin, INPUT);      // Set button as INPUT pullUpDnControl(butPin, PUD_UP); // Enable pull­up resisto r on button printf("Blinker is running! Press CTRL+C to quit.\n");...
  • Page 15 Page 15 of 17 That command will create an executable file – “blinker”. The “-l wiringPi” part is important, it loads the wiringPi library. A successful compilation won’t produce any messages; if you got any errors, try to use the messages to track them down.
  • Page 16 Page 16 of 17 …with C and WiringPi Let’s recreate our WiringPi Example with Geany. Open the “blinker.c” created earlier within the confines of Geany. You should immediately be presented with some very pleasant color-coding. Before trying to compile the code, though, you’ll need to tweak some of the build options.
  • Page 17 • RPi.GPIO Homepage – Home of the Raspberry Pi GPIO python module. Great source for API and documentation. If you’re looking for some project inspiration, here are some more SparkFun tutorials where you’ll be able to leverage your newfound Pi programming skills: •...

Table of Contents