This is the chapter web page to support the content in Chapter 8 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 describes bus communication in detail, explaining and comparing the different bus types that are available on the Raspberry Pi. It describes how you can configure them for use, and how you can communicate with and control I2C, SPI, and UART devices, using both Linux tools and custom-developed C/C++ code. Practical examples are provided using different low-cost bus devices, such as a real-time clock, an accelerometer, a serial shift register with a seven-segment display, a USB-to-TTL 3.3 V cable, and a GPS receiver. After reading this chapter, you should have the skills necessary to begin interfacing almost any type of bus device to the Raspberry Pi.

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

  • Describe the most commonly used buses or interfaces that are available on the RPi, and choose the correct bus to use for your application.
  • Configure the RPi to enable I2C, SPI, and UART capabilities.
  • Attach circuits to the RPi that interface to its I2C bus, and use the Linux I2C-tools to communicate with those circuits.
  • Build circuits that interface to the SPI bus using shift registers, and write C code that controls low-level SPI communication.
  • Write C/C++ code that interfaces to and “wraps” the functionality of devices attached to the I2C and SPI buses.
  • Communicate between UART devices using both Linux tools and custom C code.
  • Build a basic distributed system that uses UART connections to the RPi to allow it to be controlled from a desktop PC.
  • Interface to a low-cost GPS sensor using a UART connection.
  • Add logic-level translation circuitry to your circuits in order to communicate between devices with different logic-level voltages.

Digital Media Resources

An I²C Tutorial Video

In this video I discuss the I²C bus and how we can connect and program devices attached to the bus using C/C++. I explain the use of i2c-tools on embedded Linux and then show how we can interface to a digital accelerometer (Bosch BMA180) or any other I²C device. Finally, I show how we can use sysfs to build a C++ class that wraps the functionality of the digital accelerometer. This video is implemented on the BeagleBone, but the steps are identical for the Raspberry Pi.

Source Code Examples

All of the source code that is described in this book is available in a public GitHub repository: Derek Molloy Exploring Raspberry Pi repository.

You can clone this repository on a Linux desktop computer or your Raspberry Pi using the command:

The code for this chapter can be accessed in the chp08 folder of the cloned repository. The formatted code is provided here for your convenience.

External Resources

  • The I²C Manual, Jean‐Marc Irazabal and Steve Blozis, Philips Semiconductors, TecForum at DesignCon 2003 in San Jose, CA, on January 27, 2003, at
  • The Linux I²C Subsystem , at
  • Serial Programming Guide for POSIX Operating Systems, 5th ed., Michael R. Sweet, 1994‐99, at
  • Serial Programming HOWTO, Gary Frerking, Revision 1.01, at

Some High-Resolution Figures from this Chapter


None for the moment