What is WiringPi?
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,
How to Install WiringPi Library?
Step 1
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.
Step 2
We can install WiringPi library in other way also. To install WiringPi library on Raspberry Pi, first we should download it. We can download WiringPi Library here.
On above link, there are more files to download. Just download file which is at the top, is the latest updated file. By clicking on snapshot, we can download it.
Now follow the steps(commands) given below to install the above downloaded library,
cd
tar xfz downloaded_filename.tar.gz
cd downloaded_filename
./build
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.
LED Interfacing With Raspberry Pi
Raspberry Pi LED Blinking code 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.
Program
#include <wiringPi.h>
#include <stdio.h>
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.
Compile and Execute C Program using IDE
In Raspbian OS, there is an installed Geany Programmer’s Editor. We can use this editor as an IDE for developing programs and execute them.
Now, open Geany Proagrammer’s Editor which is shown as follows,
After opening it, create new file and write program in it. To create new file, click on file and select new option which is shown as follows,
- Write a program in editor and save it with .c extension
- Before compiling and executing a program with WiringPi library in Geany Programmer’s Editor, we should add few lines in build settings. To add these lines, click on Build and select Set Build Commands which is shown as follows,
- Modify C and Execute commands section for compiling and executing program using WiringPi Library shown as follows,
Click on OK.
Now, we can build C program and execute it with WiringPi library.
Components Used |
||
---|---|---|
Raspberry Pi 4B Raspberry Pi 4BRaspberry Pi 4B |
X 1 | |
Raspberry Pi Zero Raspberry Pi Zero |
X 1 |
Downloads |
||
---|---|---|
|
LED blinking using WiringPi on Raspberry Pi | Download |