Raspberry gPIo

Pages
Contributors: jimblom, MTaylor
Favorited Favorite 18

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.

language:bash
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.

language:bash
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.

language:bash
sudo apt-get purge wiringpi

Then type the following for the Pi to remove all locations that remember wiringPi.

language:bash
hash -r

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

language:bash
git clone https://github.com/WiringPi/WiringPi.git

This will make a folder in your current directory called WiringPi. Head to the Wiring Pi directory.

language:bash
cd WiringPi

Then pull the latest changes from the origin.

language:bash
git pull origin

Then enter the following command. The ./build is a script to build Wiring Pi from the source files. This builds the helper files, modifies some paths in Linux and gets WiringPi ready to rock.

language:bash
./build

At this point, the library should work. Run the gpio command shown below to view some information about the wiringPi version and the Pi that it is running on.

language:bash
gpio -v

Entering the following command will draw a table illustrating the configuration for the pins in the 40-pin connector.

language:bash
gpio readall

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. The following will toggle a pin to turn on/off an LED and then read a button press.

Toggling an LED

Open up a terminal, and try some of these system calls. To configure pin 18, enter the following. By default, the pin is set as an input.

language:bash
gpio -g mode 18 output

To turn the pin HIGH, enter the following.

language:bash
gpio -g write 18 1

To turn it back low, enter the following.

language:bash
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.

Reading a Button Press

To test the button, you will first need to configure pin 17 with the Pi's internal pull-up resistor.

language:bash
gpio -g mode 17 up

To read the pin, enter for the following.

language:bash
gpio -g read 17

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.