Raspberry Pi SPI and I2C Tutorial
Background & Software Setup
The Raspberry Pi has three types of serial interface on the GPIO header. You're probably already familiar with the UART serial port, which allows you to open a login session from a serial terminal application, such as PuTTY.
The other two serial interfaces are the Serial Peripheral Interface (SPI) and Inter-Integrated-Circuit bus (I2C). SPI on the Pi allows for up to two attached devices, while I2C potentially allows for many devices, as long as their addresses don't conflict.
The software landscape for the Raspberry Pi has evolved considerably since the introduction of the Pi. Many different operating systems have been ported to the Pi, and the device driver infrastructure has also changed quite a bit.
For this tutorial, we'll be using a recent version of Raspbian (installed via NOOBS), and the wiringPi I/O library for C/C++ (or spidev/smbus for Python).
With the implementation of device tree overlays in Raspbian, some of the specific interface enablement details have changed. If you're working with an older install, it might be worth backing up your SD card, and starting with a fresh install.
OS and Library Install
If you're starting from scratch, with a blank SD card, you'll want to install Raspbian. If you've already got a working Raspbian system, skip ahead to the next section.
- Download the NOOBS image. As of this writing, it's at version 2.8.2.
- Follow the official installation instructions.
If you would like alternative ways to set up your Pi, please refer to the following tutorials:
Raspberry Pi 3 Starter Kit Hookup Guide
Headless Raspberry Pi Setup
Setting up a Raspberry Pi 3 as an Access Point
How to Use Remote Desktop on the Raspberry Pi with VNC
For those programming in C/C++, we recommend looking at the Raspberry gPIo to setup Wiring Pi. For your convenience, we have included the following instructions below.
C/C++ (Wiring Pi) Setup
git://git.drogon.net/wiringPi" is not available.
Wiring Pi is previously not included with early versions of Raspbian. This required users to download and install it. Luckily, Wiring Pi is included in standard Raspbian systems. If you are looking to update using a mirrored Wiring Pi with small updates to support newer hardware, we recommend checking out this GitHub repository.
You'll need git (may be installed by default). If git is not installed, enter the following into the command line.
language:bash sudo apt-get install git-core
We highly recommend using Git to download the latest version. To check what version you have, enter the following command.
language:bash gpio -v
If you receive an output similar to to the following with the
Unknown17, you'll want to update WiringPi on a Raspberry Pi 4 or above.
language:bash gpio version: 2.50 Copyright (c) 2012-2018 Gordon Henderson This is free software with ABSOLUTELY NO WARRANTY. For details type: gpio -warranty Raspberry Pi Details: Type: Unknown17, Revision: 02, Memory: 0MB, Maker: Sony * Device tree is enabled. * --> Raspberry Pi 4 Model B Rev 1.2 * This Raspberry Pi supports user-level GPIO access.
Enter the following to remove the wiringPi and configuration files.
language:bash sudo apt-get purge wiringpi
Then type the following for the Pi to remove all locations that remember wiringPi.
language:bash hash -r
As long as you have Git installed, these commands should be all you need to download and install Wiring Pi.
language:bash git clone https://github.com/WiringPi/WiringPi.git
This will make a folder in your current directory called WiringPi. Head to the Wiring Pi directory.
language:bash cd WiringPi
Then pull the latest changes from the origin.
language:bash git pull origin
Then enter the following command. The
./build is a script to build Wiring Pi from the source files. This builds the helper files, modifies some paths in Linux and gets WiringPi ready to rock.
At this point, the library should work. Run the
gpio command shown below to view some information about the wiringPi version and the Pi that it is running on.
language:bash gpio -v
Entering the following command will draw a table illustrating the configuration for the pins in the 40-pin connector.
language:bash gpio readall
The I2C and SPI interfaces each require some additional configuration and initialization, which we'll cover in later sections.
Python (spidev/smbus) Setup
Follow the Configure Your Pi section in the Python Programming Tutorial to set up Python 3 and install pip.