In this article, I use:
- Raspberry Pi model B
- Raspbian Wheezy
What is WiringPi?
Raspberry Pi has a 26-pin General Purpose Input/Output (GPIO) connector and this carries a set of signals and buses. There are 8 general purpose digital I/O pins – these can be programmed as either digital outputs or inputs. Besides input/output capability, one of these pins can be designated for PWM output too. Additionally there is a 2-wire I2C interface and a 4-wire SPI interface (with a 2nd select line, making it 5 pins in total) and the serial UART with a further 2 pins.
The Revision2 Raspberry Pi has an additional 4 GPIO lines on a separate connector which you have to solder on the board.
The I2C, SPI and UART interfaces can also be used as general purpose I/O pins when not being used in their bus modes, giving a grand total of 8 + 2 + 5 + 2 = 17 I/O pins on the P1 connector (plus 4 more on the P5 connector on a Revision 2 Pi).
WiringPi includes a command-line utility gpio which can be used to program and setup the GPIO pins. You can use this to read and write the pins and even use it to control them from shell scripts.
WiringPi is extendable and modules are provided to extend wiringPi to use analog interface devices on the Gertboard, and to use the popular MCP23x17/MCP23x08 (I2C 7 SPI) GPIO expansion chips, as well as module that will allow blocks of up to 4 74×595 shift registers to be daisy-chained together for an additional 32-bits worth of output as a single unit. (You can have several blocks of 4 74x595s if needed) One of the extension modules allows you to use an ATmega (e.g. Arduino, or the Gertboard) as more GPIO expansion too – via the Pi’s serial port.
Additionally, you can easily write your own expansion modules to integrate your own peripheral devices with wiringPi as required.
WiringPi supports analog reading and writing, and while there is no native analog hardware on a Pi by default, modules are provided to support the Gertboards analog chips and other A/D and D/A devices can be implemented relatively easily.
Before you can use WiringPi, of course you have to install it inside your Raspberry Pi. The installation is fairly simple. There are two way to get WiringPi installed to Pi: clone from git, download the source tar ball.
Before we go, it is recommended to check whether our distribution is up to date or not.
sudo apt-get update sudo apt-get upgrade
Remember, this is optional.
Installing using git, involving clone the source code tree from git repository. Make sure you have GIT installed. If you do not have one, you can install it by:
sudo apt-get install git-core
Then, clone the WiringPi source code tree by:
git clone git://git.drogon.net/wiringPi
To build WiringPi, do this command:
cd wiringPi git pull origin ./build
The second command will fetch an updated version of WiringPi source code (if any) and the third script will start building the library. The script will compile and install the library automatically. Remember to use root privilege to do this.
If you can’t or don’t want to use git, use following step for manual download and installation:
Click on following URL: https://git.drogon.net/?p=wiringPi;a=summary
Look for the link marked as “snapshot”. Click on the top one which is the latest source code. You will download a tar.gz file with a name like “wiringPi-f18c8f7.tar.gz”. Note that the numbers and letter after “wiringPi” (f18c8f7 in this case) might be different. They are unique identifier for each release.
Extract and then build the package:
tar xfz wiringPi-f18c8f7.tar.gz cd wiringPi-f18c8f7 ./build
After success installation, we should check our WiringPi. WiringPi has a utility called “gpio” which is used for GPIO access. Here how we check:
gpio -v gpio readall
Pin numbering of the BCM2835 GPIO port(s) on the Raspberry Pi has been a source of great confusion since the designs for the Pi were first published. In the early days (even beofre hardware was avalable) the default usable GPIO pins were simply referred to by number as GPIO0 through GPIO7. Additionally there were pins for other purposes, SPI, I2C and serial. This was highlighted on the original image on the Raspberry Pi Wiki site too.
Reference: Raspberry Pi GPIO Reference
wiringPi supports its own pin numbering scheme as well as the BCM_GPIO pin numbering scheme, and as of Version 2, it also supports the physical hardware pin numbers (for the P1 connector only). However, simplified wiringPi numbering is really recommended. In this way, our programs will be portable over different hardware revisions without needing any changes.
The following tables give the mapping of the Raspberry Pi GPIO Pins to the (P1) GPIO connector in relation to the pin numbers and the physical location on the connector. This is a representation of the GPIO connector as viewed looking at the board from above. The GPIO connector is to the top-right of the board with the Ethernet and USB sockets to the bottom.