I2C on the Raspberry Pi

I2C is s standard means to communicate between integrated circuits, and is used by microcontrollers to talk to integrated circuits or other devices. It is a 2-wire bus, consisting of a clock signal (SCL) and a data signal (SDA). It can be used to interface the Raspberry Pi to LCD displays, memory or realtime clock chips, or to other microcontrollers, such as the Arduino's ATmega328.

Raspberry Pi Configuration

The I2C ports on the Raspberry Pi are disabled in the kernel by default. To enable them, you need to run raspi-config and turn on kernel support. It can be found in the "Advanced Options" section.

raspi-config I2C configuration raspi-config I2C configuration raspi-config I2C configuration

You will be prompted to reboot by raspi-config. Do it to install the kernel I2C module.

Once the kernel I2C support is enabled, you need to install a utility that allows you to verify I2C addresses of your peripheral devices.

sudo apt-get install i2c-tools

Now you need to attach your I2C device. Before you do, shutdown and power off the Raspberry Pi.

sudo shutdown -h now

The pins involved are SDA on pin 3 and SCL on pin 5 of the GPIO connector. Connect your I2C device to the pins, either directly (if it is a 3.3V device) or through a level shifter (if it is a 5V device).

The Raspberry Pi is a 3.3V device

You must not connect a 5V peripheral device to the GPIO header! The inputs on the processor are not 5V tolerant, and they could be damaged by the resulting excessive current.

An I2C LCD connected to the Raspberry Pi

In this example, we connect a 5V LCD display to the Raspberry Pi through a Sparkfun level shifter breakout. The breakout just goes between the Raspberry Pi and the LCD in the SDA and SCL lines. The "LV" pin on the breakout connects to the 3.3V line on the RPi (pin 1). The "HV" pin connects to the 5V line (pin 4) and "GND" connects to the RPi ground (pin 6).

The SCL line (pin 3) from the Raspberry Pi goes to the "LV1" pin on the level shifter and the SDA line (pin 5) goes to the "LV2" pin. The LCD signals connect to the high voltage side. SCL is "HV1" and SDA is "HV2".

To check your work so far, boot the Rasberry Pi and run i2cdetect on the I2C port. The port number will be either 0 or 1, depending on the model of Raspberry Pi you have. It doesn't hurt anything to run it against the wrong port. This unit is a Model 3B, which assigns port 1 to the GPIO header pins 3 and 5.

pi@raspberrypi-dev:~ $ i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- 27 -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --                    

It finds the LCD at address 0x27, right where it belongs.

The code I used to interface with the display was based on code found at RaspberryPi-Spy. Of my different LCD backpacks, it only works with the particular backpack shown in the article.

Arduino Board Logo

 

Arduino-Board is the go-to source for information on many available Arduino and Arduino-like boards, tutorials and projects.

Help and Support

Arduino-Board

Stay updated

Sign up if you would like to receive our once monthly newsletter.