This is the chapter web page to support the content in Chapter 6 of the book: Exploring Raspberry Pi – Interfacing to the Real World with Embedded Linux. The summary introduction to the chapter is as follows:

This chapter integrates the Linux, programming, and electronics groundwork from earlier chapters to show you how to build circuits and write programs that interface to the Raspberry Pi’s single-wire inputs and outputs. In this chapter, you will see practical examples that explain how to use general-purpose input/outputs (GPIOs) to interface to different types of electronic circuits. GPIO interfacing is first performed using sysfs to ensure that you have skills that are transferable to other embedded Linux devices. Next, memory-mapped approaches are investigated that have impressive performance, but are largely specific to the RPi platform. Finally, the wiringPi library of C functions is discussed in detail. It uses sysfs and memory-mapped approaches to provide a custom GPIO interfacing library for the RPi platform that is very accessible. Examples are provided of how it can be used to communicate with one-wire sensors, to generate pulse-width modulated (PWM) signals, and to generate high-frequency timing signals. Finally, there is a brief discussion on the impact of udev rules and Linux permissions on GPIO interfacing.

After completing this chapter, you should hopefully be able to do the following:

  • Use an RPi GPIO to output a binary signal to a digital circuit, or read in a binary input from a digital circuit.
  • Write shell scripts and efficient C/C++ sysfs code to control GPIOs on the RPi.
  • Describe the impact of the PREEMPT kernel patch and multiple CPU cores on the performance of GPIO applications.
  • Utilize internal pull-up and pull-down resistors for interfacing.
  • Manipulate GPIO state using memory-mapped registers on the RPi’s SoC using the shell prompt and C/C++ program code.
  • Use the wiringPi library of C functions to control the RPi’s GPIOs in an efficient and accessible manner.
  • Communicate bi-directionally with a sensor using a single GPIO.
  • Use PWM on the RPi to fade an LED and drive a servo motor.
  • Use general-purpose clocks to output high-frequency clock signals.
  • Use Linux udev rules and the setuid bit to improve user-level control of GPIO applications.


On the most recent kernel releases you may see an error when you try to view the kernel build settings (as described in the section on “The Preemptible Linux Kernel”). To fix this you must load the configs LKM, as below:

Digital Media Resources

Here the digital resources referred to in the chapter web page are provided. There are high-resolution versions of some of the important figures and links to videos, resources and websites that are described in the chapter.

The first figure illustrates the simplified view of the functionality that is available on the GPIO header of the Raspberry Pi 2/3/Zero. Older models of the Raspberry Pi had 26 pins and finished at the dotted line.

The second figure is a full description of the capabilities of the GPIO header as described in the book.

The circuit below describes the connection of a DHT sensor to the RPi using a one-wire GPIO connection. This connection requires the use of memory-mapped I/O, such as that provided using the wiringPi library.

Source Code Examples

The full source code listing for this chapter is available in the GitHub repository directory exploringrpi/chp06. There are the important source code listings for this chapter, which describe how you can interact with a GPIO using a custom class and how you can interact with the GPIOs using the wiringPi library.

External Resources


None for the moment