RETIRED - Raspberry gPIo

This Tutorial is Retired!

This tutorial covers concepts or technologies that are no longer current. It's still here for you to read and enjoy, but may not be as useful as our newest tutorials.

View the updated tutorial: Raspberry gPIo

Pages
Contributors: jimblom
Favorited Favorite 2

C (WiringPi) Setup

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

Note: Wiring Pi is now pre-installed with standard Raspbian systems. The instructions from the official WiringPi homepage are now depreciated. The original wiringPi source "git://git.drogon.net/wiringPi" is not available.

Wiring Pi is previously not included with early versions of Raspbian. This required users to download and install it. Luckily, Wiring Pi is included in standard Raspbian systems. If you are looking to update using a mirrored Wiring Pi with small updates to support newer hardware, we recommend checking out this GitHub repository.

You'll need git (may be installed by default). If git is not installed, enter the following into the command line.

pi@raspberrypi ~/code $ sudo apt-get install git-core

We highly recommend using Git to download the latest version. To check what version you have, enter the following command.

pi@raspberrypi ~/code $ gpio -v

If you receive an output similar to to the following with the Unknown17, you'll want to update WiringPi on a Raspberry Pi 4 or above.

language:bash
gpio version: 2.50
Copyright (c) 2012-2018 Gordon Henderson
This is free software with ABSOLUTELY NO WARRANTY.
For details type: gpio -warranty

Raspberry Pi Details:
  Type: Unknown17, Revision: 02, Memory: 0MB, Maker: Sony
    * Device tree is enabled.
    * --> Raspberry Pi 4 Model B Rev 1.2
    * This Raspberry Pi supports user-level GPIO access.

Enter the following to remove the wiringPi and configuration files. Then use the hash command for the Pi to remove all locations that remember wiringPi.

pi@raspberrypi ~/code $ sudo apt-get purge wiringpi
pi@raspberrypi ~/code $ hash -r

As long as you have Git installed, these commands should be all you need to download and install Wiring Pi:

pi@raspberrypi ~/code $ git clone https://github.com/WiringPi/WiringPi.git
pi@raspberrypi ~/code $ cd WiringPi
pi@raspberrypi ~/code/WiringPi $ git pull origin
pi@raspberrypi ~/code/WiringPi $ ./build

2) Test Wiring Pi

WiringPi is awesome because it's actually more than just a C library, it includes a command-line utility as well! You can test your installation of WiringPi with the gpio utility.

Open up a terminal, and try some of these system calls:

pi@raspberrypi ~/code $ gpio -g mode 18 output
pi@raspberrypi ~/code $ gpio -g write 18 1
pi@raspberrypi ~/code $ gpio -g write 18 0

As long as your LED is still connected to pin 18 it should blink on and off following the last two commands.

Or, to test the button, type:

pi@raspberrypi ~/code $ gpio -g mode 22 up
pi@raspberrypi ~/code $ gpio -g read 22

Either 0 or 1 will be returned, depending on whether the button is pressed or not. Try typing that last line again while pressing the button.

The gpio utility, as stated in the manual, is a "swiss army knife" command-line tool. We highly recommend checking out the man page (type man gpio) to discover everything it can do.


If you're ready to get on with some C-style programming, head over to the next page. We'll overview some of the most useful functions provided by the WiringPi library.