WiringPi

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 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/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
2
3
4
5
$ sudo ./wiringpi
wPi Pin 1 now is GIGH
wPi Pin 1 now is LOW
wPi Pin 1 now is GIGH
wPi Pin 1 now is LOW

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: VIM1andVIM2 do not provide access to SPI on the 40-PIN HEADER.

1
2
3
4
PIN37 <---> MOSI
PIN35 <---> MISO
PIN15 <---> SS
PIN16 <---> SCLK
1
2
3
4
PIN37 <---> MOSI
PIN35 <---> MISO
PIN26 <---> SS
PIN25 <---> SCLK

I2C

I2C0

1
2
PIN22 <---> SCK
PIN23 <---> SDA

I2C0

1
2
PIN22 <---> SCK
PIN23 <---> SDA

I2C3

1
2
PIN22 <---> SCK
PIN23 <---> SDA

I2C0

1
2
PIN25 <---> SCK
PIN26 <---> SDA

ADC

1
2
PIN10 <---> ADC_CH0
PIN12 <---> ADC_CH2
1
2
PIN10 <---> ADC_CH0
PIN12 <---> ADC_CH2
1
2
PIN10 <---> ADC_CH0
PIN12 <---> ADC_CH3
1
2
PIN10 <---> ADC_CH6
PIN12 <---> ADC_CH3

Serial

Check which nodes are used for serial.

1
2
PIN15 <---> RX
PIN16 <---> TX

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.