Quantcast
submit to reddit       
       

Controlling GPIO with wiringPi

The wiringPi libraries are a set of functions written in C that make it easy to control the Raspberry Pi's GPIO pins. You can use the functions in this library to control GPIO pins in your own programs.

You can also use wiringPi on the command line. This is very useful for debugging problems with circuits connected to GPIO pins. It's also useful for controlling GPIO with Bash scripts.

In order to install the libraries, you have to install the 'git' source code management system. Make sure your Pi can access the internet. You can install git by opening a terminal and typing these commands:

$ sudo apt-get install git-core
$ sudo apt-get update
$ sudo apt-get upgrade

Get the wiringPi project using this command:

$ git clone git://git.drogon.net/wiringPi

Change to the new directory, and get the code from the repository at drogon.net:

$ cd wiringPi
$ git pull origin

Build the code:

$ ./build

Test wiringPi by typing this command:

$ gpio readall
+----------+-Rev2-+------+--------+------+-------+ | wiringPi | GPIO | Phys | Name | Mode | Value | +----------+------+------+--------+------+-------+ | 0 | 17 | 11 | GPIO 0 | IN | Low | | 1 | 18 | 12 | GPIO 1 | IN | Low | | 2 | 27 | 13 | GPIO 2 | IN | Low | | 3 | 22 | 15 | GPIO 3 | IN | Low | | 4 | 23 | 16 | GPIO 4 | IN | Low | | 5 | 24 | 18 | GPIO 5 | IN | Low | | 6 | 25 | 22 | GPIO 6 | OUT | High | | 7 | 4 | 7 | GPIO 7 | IN | Low | | 8 | 2 | 3 | SDA | IN | High | | 9 | 3 | 5 | SCL | IN | High | | 10 | 8 | 24 | CE0 | ALT0 | High | | 11 | 7 | 26 | CE1 | ALT0 | High | | 12 | 10 | 19 | MOSI | ALT0 | Low | | 13 | 9 | 21 | MISO | ALT0 | Low | | 14 | 11 | 23 | SCLK | ALT0 | Low | | 15 | 14 | 8 | TxD | ALT0 | High | | 16 | 15 | 10 | RxD | ALT0 | High | | 17 | 28 | 3 | GPIO 8 | IN | Low | | 18 | 29 | 4 | GPIO 9 | IN | Low | | 19 | 30 | 5 | GPIO10 | IN | Low | | 20 | 31 | 6 | GPIO11 | IN | Low | +----------+------+------+--------+------+-------+

You should see a table listing the state of the GPIO pins. Before you can use any of the pins, you must configure them as either outputs or inputs. The following command sets up pin 17 for use as an output:

$ gpio -g mode 17 out

This command sets pin 17 to logic 1:

$ gpio -g write 17 1

The -g option tells the gpio command to use the standard pin numbering scheme rather than the numbering scheme invented for wiringPi. You can test the voltage of pin 17 with a multimeter to see if it changes when you execute the command above.

See also: wiringPi.com


Comments

Comments

comments powered by Disqus



Follow me