In this blog entry I will demonstrate how one can access the Raspberry Pi’s GPIO in C++. There are two approaches to accomplish this. The first is to directly manipulate the Raspberry Pi’s GPIO’s registers much like one would do when programming a microcontroller without an operating system (OS) or a memory management unit (approach using mmap). The advantage of this approach is that because the OS (Linux) is being completely bypassed, the GPIO pins can toggle very fast. Bypassing the OS however means that if two processes (instances of a programs) are trying to access the same physical GPIO registers at the same time, a unsafe resource conflict can happen. Examples of this approach to GPIO coding can be found here.
The second approach is the “Linux approach” (sysfs). Linux already has a built-in driver for safely accessing the GPIOs. It basically views each property of each GPIO pin as a file. This is the preferred method of GPIO access. One disadvantage of this approach is that it tends to make for slower (but safer) GPIO pin toggling. Example code for this approach can be found here, and a wiki on how to access GPIO signals using this approach can he found here. This approach will be used in this tutorial. The file accesses will be wrapped in a C++ class to make GPIO access easy and foolproof (sort of).
Setting up the test hardware
In order to test our GPIO access program we will need to connect the GPIO pins to actual hardware components. I decided to to use GPIO pin 4 as an output pin that turns on an LED and GPIO pin 17 as an input pin that reads the state of a pushbutton. A connection diagram is provided in Figure 1.
GPIO4 is connected to an LED via a 330Ohm current limiting resistor. GPIO17 is connected to a 10KOhm pull-up resistor and a push-button. An unpressed pushbutton will cause the voltage on GPIO17 to be 3.3V. When the button is pressed, the voltage of GPIO17 will be 0V.
Accessing GPIO From the Terminal
- When using the Linux (sysfs) way to access GPIO, we must ensure that we’re logged in as root:
- we then must “export the pin” in question. For example to use pins GPIO4 & 17 we must first export them by writing the GPIO pin numbers to the file “/sys/class/gpio/export” as follows:
The next step is to set pin 4 as output and pin 17 as input. This is achieved by writing “out” into the “/sys/class/gpio/gpio4/direction” file and writing “in” into the “/sys/class/gpio/gpio17/direction” file as follows:
To set the output GPIO4 pin high and therefore cause the LED to light-up, we need to write a “1” to the “/sys/class/gpio/gpio4/value” file:
To clear the output GPIO4 pin to low and therefore cause the LED to turn off we need to write a “0” to the “/sys/class/gpio/gpio4/value” file:
To read the state of the input pin GPIO17, we need to read the “/sys/class/gpio/gpio17/value” file. This will return either a “0” if the pin is connected to 0V or a “1” if the pin is connected to 3.3V:
And finally, when we’re done with the GPIO pins we must unexport them by writing the GPIO pin numbers to the “/sys/class/gpio/unexport” file!
Notice how the sysfs approach reduces the problem of GPIO access to reading and writing files! This is by the way the norm for Linux. All I/O access (serial ports, console, printers e.t.c.) involves reading to and writing from files. The next step will be to perform the same functions in C++!
Writing a GPIO C++ Class
The goal of the GPIOClass class is to enable the control of a single GPIO pin. The class was designed so that the GPIO pin number must be passed to the GPIOClass object when its created via an overloaded constructor. The “GPIOClass.h” header file is provided below
Each GPIOClass object has member functions that enable us to export/unexport GPIO pins, set the direction of the GPIO pins as well as set and get the value of the GPIO pins. The GPIOClass object has one private variable which is the GPIO pin number. The implementation of these member functions are provided below “GPIOClass.cpp”:
In order to open and close files in C++, the ifstream/ofstream classes where used. Even though the C++ string class was used, the C++ strings had to be converted into C strings since pathnames in Linux can only be interpreted as C strings. The GPIOClass is pretty self explanatory. It is also very basic I intend to clean in up a bit in the future, but for now it will have to do.
For more detail: Introduction to accessing the Raspberry Pi’s GPIO in C++ (sysfs)