What is WiringPi
WiringPi is a GPIO Controller basic on C Program.Originally a library for Raspberry Pie.Now we have migrated to VIMs.You can control the 40 pin header by it.
Begin to Use WiringPi
Control command
- run
gpio -h
, you can see all the control command with WiringPi.gpio: Usage: gpio -v
gpio -h
gpio [-g|-1] ...
gpio [-d] ...
[-x extension:params] [[ -x ...]] ...
gpio [-p] <read/write/wb> ...
gpio <mode/read/write/aread/awritewb/pwm/pwmTone/clock> ...
gpio <toggle/blink> <pin>
gpio readall
gpio unexportall/exports
gpio export/edge/unexport ...
gpio wfi <pin> <mode>
gpio drive <group> <value>
gpio pwm-bal/pwm-ms
gpio pwmr <range>
gpio pwmc <divider>
gpio load spi/i2c
gpio unload spi/i2c
gpio i2cd/i2cdetect
gpio rbx/rbd
gpio wb <value>
gpio usbp high/low
gpio gbr <channel>
gpio gbw <channel> <value> - run
gpio readall
, It prints a table showing the status of all pins.
You will see a table with many columns.GPIO --> GPIO native number
wPi --> WiringPi number
Mode --> GPIO Mode ,`ALT` mean that this pin defined as a special function
v --> 1:HIGH 0:low
PU/PD --> PU:pull up PD:pull down DSBLD:disabled PU/PDControl by command
Here’s an example of controlling wpi number 1. - run
gpio mode 1 out
Now, The wpi number 1 mode is out. - run
gpio read 1
root@Khadas:~# gpio read 1
1 - run
gpio write 1 0
to change the pinout level - run
gpio read 1
againyou can see the wpi number 1 Output changed from high to low.root@Khadas:~# gpio read 1
0
Control by Linux C program
- Here is a simple control program.
#include <stdio.h>
#include <wiringPi.h>
const int gpio_pin = 1;
int main()
{
if(-1 == wiringPiSetup()){
printf("set up error");
exit(1);
}
pinMode(gpio_pin,OUTPUT);
while(1){
digitalWrite(gpio_pin,HIGH);
printf("wPi Pin %d now is GIGH\n",gpio_pin);
delay(5000);
digitalWrite(gpio_pin,LOW);
printf("wPi Pin %d now is LOW\n",gpio_pin);
delay(5000);
}
exit(0);
} - And you can use gcc to compile it .here is the compile command
gcc -o test test.c -lwiringPi -lpthread -lrt -lm -lcrypt
. - run
./test
to control wpi number 1you can usewPi Pin 1 now is GIGH
wPi Pin 1 now is LOW
wPi Pin 1 now is GIGH
wPi Pin 1 now is LOWgpio read 1
to observing pin level changes.
Special pin functions of wiringpi
Special pin functions of wiringpi includeSPI,i2C,ADC,SoftPWM
SPI
BecauseVIM1
andvim2
don’t export SPI
to pin40 of GPIO, only vim3
is supported for SPI
. The corresponding pins of physical pin and SPI function are as follows:
PIN37 <---> MOSI |
I2C
VIM1
and vim2
use i2c0
and vim3
use i2c3
. The physical pin connections are as follows:
PIN22 <---> SCK |
ADC
VIM1
and vim2
uses channel0
and channel2
of ADC
, and vim3
uses channel0
and channel3
. The physical pins are connected as follows:
PIN10 <---> ADC_CH0 |
Serial
Please confirm the node name for Serial before using
PIN15 <---> RX |
WiringPi Function List
int wiringPiSetup (void) ; |
notes
If you need to use the special function pin of wiringPi-Python, you need to confirm that the corresponding configuration is opened in DTB.
WiringPi itself includes many functions, not just controlling the output of GPIO pins and reading pin levels. Here is only a simple introduction and use, more use needs to be explored by users themselves.