6. GPIO Controlling of the Raspberry Pi

Share for us

The Raspberry Pi is a microcomputer and you can learn programming like in your PC. Another highlight of the Raspberry Pi is that it can control the GPIO which the PC fails to do.

6.1 RPi.GPIO and WiringPi Module

We need to use the GPIO module to handle the GPIO function. Let’s check several widely used modules in the following part.


Language: Python

Introduction: The more precise name of this library is raspberry-gpio-python. It is recommended by the official website of the Raspberry Pi and easy to handle. The Python GPIO is a small Python library which can help the users with the operation of the related IO ports. However, this library is not available for the bus line ports like SPI, I2C or 1-wire. Besides the Python GPIO, there are a lot of Python extension libraries like webiopi out there. There is no doubt that the Python and the Raspberry Pi work pretty well with each other.


Language: C

Introduction: The wiringPi is suitable for those who have learnt the basic knowledge of the C language and have already known the microcontrollers or the embedded development before getting to know the Raspberry Pi. The API function of the wiringPi is similar with the Arduino, which makes it quite popular among hobbyists. Its author has provided abundant pages of explanation and sample code including that for the UART, I2C and SPI devices. Definitely the wiringpi is very powerful in use.

6.2 Numbering Method of the GPIO

Since the GPIO modules are changing, the number of the GPIO pin will be different. Type in gpio readall in a terminal and you will see the following table on the display to the Raspberry Pi:

  • BCM: The number of the pin in the Broadcom SOC channel
  • wPi: The number of the pin in the wiringPi
  • Name: The name of the pin
  • Mode: The current mode of the pin
  • V: The current voltage of the pin: high level (1) or low level (0)
  • Physical: The physical number of the pin on the Raspberry Pi.

Besides its own names, there are another 3 numbering methods including Physical, BCM and wPi of the GPIO ports of the Raspberry pi. 

    [1] Numbering by physical pin location

The method focuses more on the physical location of the 40 pin headers on the Raspberry Pi as shown below.

[2] Numbering by BCM

The method focuses more on the CPU register, i.e., the GPIO register of the BCM2835 as shown in the two columns of the BCM on the table above.

    [3] Numbering based on wiringPi 

This numbering method focuses more on logic implementation and numbering the extension GPIO ports from 0 is more convenient for programming. As in the wPi columns above.

In order not to damage the pins on the Raspberry Pi due to frequent plugging in/out, we use a T-Extension Board to lead out the pins. The middle columns in the figure below are the pin names marked on that board. The original pin names and the corresponding ones based on BCM and wiringPi methods are provided on its left and right. The physical numbering method is not presented on this T-Extension board. Please refer to the physical columns above for the physical numbers of the pins.

For example, when you insert the pin of a component into B17, then the corresponding number is 17 in the BCM, 0 in the wiringPi and 11 is the physical location.

So that’s all for the basics of the Raspberry Pi. You should grasp these knowledges firmly and apply them in your later learning and further projects. Now let’s learn through the experiments!