This documentation will introduce how to use WiringPi.
What is WiringPi
WiringPi is a C++ library for Raspberry Pi. With this library you can use many of the functionalities provided by the GPIO header: digital pins
, SPI
, I2C
, UART
, etc.
We have ported this library to also work with Khadas SBCs, and you can use it to control the 40-PIN HEADER
.
Using WiringPi
Command Usage
- Get GPIO information
Run gpio readall
in the Terminal, and it will print a table about the status of all GPIO pins.
GPIO
–> GPIO native numberwPi
–> WiringPi numberMode
–> GPIO Mode ,ALT
mean that this pin defined as a special functionv
–> 1:HIGH 0:lowPU/PD
–> PU:pull up PD:pull down DSBLD:disabled PU/PD
Here’s an example of controlling wpi number 1.
- Set GPIO output
1 | $ sudo gpio mode 1 out |
- Set output value
1 | $ gpio write 1 0 |
Set GPIO output low.
1 | $ gpio write 1 1 |
Set GPIO output high.
C Program Usage
- Get demo source code
1 | $ wget https://dl.khadas.com/development/code/docs_source/wiringpi.c |
- Compile
1 | $ gcc -o wiringpi wiringpi.c -lwiringPi -lpthread -lrt -lm -lcrypt |
- Test
1 | $ sudo ./wiringpi |
You can use gpio read 1
to observe the pin level changes.
Special Pin Functions of WiringPi
Special pin functions of wiringpi includeSPI
,I2C
,ADC
,SoftPWM
.
SPI
Note: VIM1
andVIM2
do not provide access to SPI
on the 40-PIN HEADER
.
1 | PIN37 <---> MOSI |
1 | PIN37 <---> MOSI |
I2C
I2C0
1 | PIN22 <---> SCK |
I2C0
1 | PIN22 <---> SCK |
I2C3
1 | PIN22 <---> SCK |
I2C0
1 | PIN25 <---> SCK |
ADC
1 | PIN10 <---> ADC_CH0 |
1 | PIN10 <---> ADC_CH0 |
1 | PIN10 <---> ADC_CH0 |
1 | PIN10 <---> ADC_CH6 |
Serial
Check which nodes are used for serial.
1 | PIN15 <---> RX |
Note
If you need to use the special pin functions of wiringPi-Python, you’ll need to confirm that the corresponding configuration is enabled in the dts file.
WiringPi includes many functions, not limited to just controlling the output of GPIO pins and reading pin levels. This is only a simple introduction, and you can explore more by yourself.