Raspberry Pi SPI and I2C Tutorial

Pages
Contributors: Byron J., Shawn Hymel
Favorited Favorite 27

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.

Software Details

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.

If you would like alternative ways to set up your Pi, please refer to the following tutorials:

Raspberry Pi 3 Starter Kit Hookup Guide

Guide for getting going with the Raspberry Pi 3 Model B and Raspberry Pi 3 Model B+ starter kit.

Headless Raspberry Pi Setup

Configure a Raspberry Pi without a keyboard, mouse, or monitor.

Setting up a Raspberry Pi 3 as an Access Point

This guide will show you how to configure a Raspberry Pi as an access point and connect it to your local Ethernet network to share Internet to other WiFi devices.

How to Use Remote Desktop on the Raspberry Pi with VNC

Use RealVNC to connect to your Raspberry Pi to control the graphical desktop remotely across the network.

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.

Raspberry gPIo

October 29, 2015

How to use either Python or C++ to drive the I/O lines on a Raspberry Pi.

C/C++ (Wiring Pi) Setup

Note: Wiring Pi is now pre-installed with standard Raspbian systems. The instructions from the official WiringPi homepage are now depreciated. The original wiringPi source "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.

language:bash
./build

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.

Python Programming Tutorial: Getting Started with the Raspberry Pi

June 27, 2018

This guide will show you how to write programs on your Raspberry Pi using Python to control hardware.