How to use WiringPi Library on Raspberry Pi

WiringPi is a library written in C used to access GPIO pins on Raspberry for BCM2835 (Broadcom Processor) SoC (System on Chip). There are various libraries are available to access GPIO in C like bcm 2835, sysfs, pigpio, etc. Here, we are using WiringPi library for Raspberry Pi GPIO access.

To use wiringPi, first we should install it on Raspberry Pi. This installation can be done in two ways which is as follows,

WiringPi Library Installation:

This way of installing WiringPi library will use git.

Follow the following steps to install it on Raspberry Pi,

  • Make sure our Raspbian is updated. So, first check for any update and upgrade on Raspberry Pi.

sudo apt-get update
sudo apt-get upgrade

  • Now, we can use git to download WiringPi library. For that we should install git on Raspberry Pi if already not installed.

sudo apt-get install git-core

  • To get WiringPi use git as follows,

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

  • Now, create directory for WiringPi and build it. Also, fetch the updated version from git.

cd wiringPi
git pull origin
./build

Installation of WiringPi library is done.

Test the installation of  WiringPi library:

Now, we can check/test the installation of WiringPi library as follows,

gpio -v

The above screenshot tells that the WiringPi library is installed successfully.

Now, we can access GPIO using WiringPi library. But, the pin numbering used in Wiring Pi library is different than GPIO numbering (BCM) and Physical numbering(BOARD).

To get information about pin numbering on our respective Raspberry Pi version, we can use following command:

gpio readall

The above screenshot shows pin numbering as per WiringPi, BCM (GPIO numbering) and Physical numbering(Board).

Use WiringPi library:

Here, we will access GPIO on Raspberry Pi using WiringPi library to blink LED.

Interfacing Diagram

LED Blinking Using wiringPi on Raspberry Pi

Raspberry Pi LED Blinking using C (wiringPi):

  • Let’s write a C program to access GPIO using WiringPi library. To write a C program, create new file by right clicking and select empty file. Write a program and save that file with .c extension.
  • In this program, we will blink LED connected to Raspberry Pi.

Test Program

#include 
#include 
int LED = 26;			/* GPIO26 as per wiringPi, GPIO12 as per BCM, pin no.32 */
int main(){
	wiringPiSetup();	/* initialize wiringPi setup */
	pinMode(LED,OUTPUT);	/* set GPIO as output */
	
	while (1){
		digitalWrite(LED,HIGH);		/* write high on GPIO */
		delay(1000);
		digitalWrite(LED, LOW);		/* write low on GPIO */
		delay(1000);
		}
}

How to Compile and Execute C Program using command terminal:

Now, we should compile above C program with wiringPi library which is given as follows.

gcc -o led_blink led_blink.c -l wiringPi

The above command will create an executable file of name led_blink. Then, use following command to execute above program.

sudo ./led_blink

After executing above command LED will starts blinking.

Short URL: http://tinyurl.com/y86kekng
   Send article as PDF   

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Do NOT follow this link or you will be banned from the site!