Skip to main content
\(\newcommand{\doubler}[1]{2#1} \newcommand{\binary}{\texttt} \newcommand{\hex}{\texttt} \newcommand{\octal}{\texttt} \newcommand{\prog}{\texttt} \newcommand{\lt}{ < } \newcommand{\gt}{ > } \newcommand{\amp}{ & } \)

Section2.9Accessing the GPIO in C

Now we will move to an even lower level of I/O and learn how to use C to access the individual pins on the General Purpose Input/Output (GPIO) device. There are several online tutorials that show how to set up your hardware. I used electronic components from a SunFounder Super Kit V2.0 for my experiments. They have many tutorials on their website that show how to connect circuits and provide both C and Python code.

The Raspberry Pi has either a 26-pin or 40-pin physical GPIO connector, depending on the model. The physical connector is often called a Header. The 26-pin model has 17 pins connected to GPIO pins, and the 40-pin connector has 26 GPIO pin connections. You can connect appropriate I/O devices to these pins and control them with software.

Raspbian includes the Wiring Pi[A.1.7] libraries, which are required for the work in this section. If they are missing from your installation, instructions for installing them are given on the Wiring Pi website, wiringpi.com.

Wiring Pi includes the gpio utility, which allows you to query the current state of the GPIO. The readall option shows the current state of each of the GPIO pins:

pi@rpi3:~ $ gpio readall
+-----+-----+---------+------+---+---Pi 3---+---+------+---------+-----+-----+
| BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
+-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
|     |     |    3.3v |      |   |  1 || 2  |   |      | 5v      |     |     |
|   2 |   8 |   SDA.1 |   IN | 1 |  3 || 4  |   |      | 5V      |     |     |
|   3 |   9 |   SCL.1 |   IN | 1 |  5 || 6  |   |      | 0v      |     |     |
|   4 |   7 | GPIO. 7 |   IN | 1 |  7 || 8  | 0 | IN   | TxD     | 15  | 14  |
|     |     |      0v |      |   |  9 || 10 | 1 | IN   | RxD     | 16  | 15  |
|  17 |   0 | GPIO. 0 |   IN | 1 | 11 || 12 | 0 | IN   | GPIO. 1 | 1   | 18  |
|  27 |   2 | GPIO. 2 |   IN | 0 | 13 || 14 |   |      | 0v      |     |     |
|  22 |   3 | GPIO. 3 |   IN | 0 | 15 || 16 | 0 | IN   | GPIO. 4 | 4   | 23  |
|     |     |    3.3v |      |   | 17 || 18 | 0 | IN   | GPIO. 5 | 5   | 24  |
|  10 |  12 |    MOSI |   IN | 0 | 19 || 20 |   |      | 0v      |     |     |
|   9 |  13 |    MISO |   IN | 0 | 21 || 22 | 0 | IN   | GPIO. 6 | 6   | 25  |
|  11 |  14 |    SCLK |   IN | 0 | 23 || 24 | 1 | IN   | CE0     | 10  | 8   |
|     |     |      0v |      |   | 25 || 26 | 1 | IN   | CE1     | 11  | 7   |
|   0 |  30 |   SDA.0 |   IN | 1 | 27 || 28 | 1 | IN   | SCL.0   | 31  | 1   |
|   5 |  21 | GPIO.21 |   IN | 1 | 29 || 30 |   |      | 0v      |     |     |
|   6 |  22 | GPIO.22 |   IN | 1 | 31 || 32 | 0 | IN   | GPIO.26 | 26  | 12  |
|  13 |  23 | GPIO.23 |   IN | 0 | 33 || 34 |   |      | 0v      |     |     |
|  19 |  24 | GPIO.24 |   IN | 0 | 35 || 36 | 0 | IN   | GPIO.27 | 27  | 16  |
|  26 |  25 | GPIO.25 |   IN | 0 | 37 || 38 | 0 | IN   | GPIO.28 | 28  | 20  |
|     |     |      0v |      |   | 39 || 40 | 0 | IN   | GPIO.29 | 29  | 21  |
+-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
| BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
+-----+-----+---------+------+---+---Pi 3---+---+------+---------+-----+-----+

The two center columns in the readall display show the physical GPIO pin numbers, when looking at the top of the Raspberry Pi board with the edge of the board to your right. The other columns are arranged in a mirror-like pattern. They provide the following information about the pin in that row, on each side of the center:

V

The value of the pin, \(\binary{0}\) or \(\binary{1}\text{.}\)

Mode

Most pins can be used as an input (IN) or an output (OUT), in addition to other modes that are beyond the scope of this book.

Name

The name of the pin, as defined in the Wiring Pi libraries, or the voltage on the pin.

wPi

The pin number, as defined in the Wiring Pi libraries.

BCM

The pin number, as defined in Broadcom datasheet.

The Broadcom numbering, BCM, will be described in detail in Chapter 19 when we discuss accessing the GPIO in assembly language.

The circuit diagram in Figure 2.9.1 shows how to connect an LED so that you can cause it to blink under program control. (If you have never worked with electronic circuits, you may wish to read Section 6.1 before tackling this project.) The pin numbers in this circuit diagram refer to the physical pins on the GPIO connector, column wPi in the gpio readall display above. Be very careful to connect the components correctly, or you may cause permanent damage to your Raspberry Pi. Most online tutorials have photographs that show you how to make the connections.

Sunfounder provides a C program, Listing 2.9.2, which shows how to blink an LED. Their website shows how to connect the LED to your Raspberry Pi so that this program blinks it.

/**********************************************************************
* Filename    : led.c
* Description : Make an led blinking.
* Author      : Robot
* E-mail      : support@sunfounder.com
* website     : www.sunfounder.com
* Date        : 2014/08/27
**********************************************************************/
#include <wiringPi.h>
#include <stdio.h>

#define  LedPin    0

int main(void)
{
	if(wiringPiSetup() == -1){ //when initialize wiring failed,print messageto screen
		printf("setup wiringPi failed !");
		return 1; 
	}
	printf("linker LedPin : GPIO %d(wiringPi pin)\n",LedPin); //when initialize wiring successfully,print message to screen
	
	pinMode(LedPin, OUTPUT);

	while(1){
			digitalWrite(LedPin, LOW);  //led on
			printf("led on...\n");
			delay(500);
			digitalWrite(LedPin, HIGH);  //led off
			printf("...led off\n");
			delay(500);
	}

	return 0;
}
Listing2.9.2Blink an LED. (C)

You need to explicitly specify the Wiring Pi libraries when you compile the program in Listing 2.9.2:

pi@rpi3: gcc -lwiringPi -o led led.c

Pin 1 in Figure 2.9.1 supplies \(+3.5\) volts. Pin 11 is turned off and on under program control. When it is turned on (HIGH), the voltage on this pin is \(+3.5\) volts, so there is no voltage across the LED, and it is off. Turning the pin off (LOW) causes the voltage on the pin to go to \(0\text{,}\) thus drawing current through the LED, causing LED to turn on. The \(220\) ohm resistor limits the amount of current to protect the LED.