I2C On Arduino And Raspberry Pi

Arduino and Raspberry Pi are the two most common entry platforms for I2C experiments. Both offer hardware I2C on fixed pins, but they differ in supply voltage, pull-up situation and software stack – and exactly those differences cause most of the trouble when both boards meet on one bus.

Arduino UNO and Nano (ATmega328P)

On the UNO, the Nano and other ATmega328P boards, SDA and SCL sit on A4 and A5 – plus on the two dedicated SDA/SCL pins near AREF, which are internally wired to the same lines. The library from the Arduino core is called Wire and drives the I2C hardware of the AVR (known there as TWI). The logic level is 5 V.

Writing one register of a device at address 0x20 takes four lines:

#include <Wire.h>

const uint8_t DEVICE_ADDR = 0x20;   // 7-bit address
const uint8_t REG_CTRL    = 0x03;

void setup() {
  Wire.begin();                     // join the bus as master

  Wire.beginTransmission(DEVICE_ADDR);
  Wire.write(REG_CTRL);             // select the register
  Wire.write(0x41);                 // value to store
  Wire.endTransmission();           // STOP, device applies the value
}

void loop() {
  // nothing to do here
}

Before any register work, it pays to check who is actually on the bus. A scanner simply walks through all valid 7-bit addresses and waits for an ACK:

#include <Wire.h>

void setup() {
  Serial.begin(9600);
  Wire.begin();

  for (uint8_t addr = 8; addr < 120; addr++) {
    Wire.beginTransmission(addr);
    if (Wire.endTransmission() == 0) {   // 0 = ACK received
      Serial.print("Device found at 0x");
      Serial.println(addr, HEX);
    }
  }
  Serial.println("Scan finished.");
}

void loop() {
}

Wire.begin() also switches on the internal pull-ups of the ATmega328P, which are comparatively weak at roughly 20–30 kΩ. For short wires on a breadboard and Standard Mode, that is sufficient. For anything longer or faster, external pull-ups of a few kiloohms are the better choice.

Raspberry Pi

On all Raspberry Pi models with the 40-pin header, I2C1 sits on GPIO2 (SDA) and GPIO3 (SCL), physically pins 3 and 5. The Pi runs at 3.3 V, and these two pins carry hardwired 1.8 kΩ pull-ups to 3.3 V – they are always present, wanted or not. The I2C interface is disabled by default and is enabled via raspi-config (Interface Options → I2C) or dtparam=i2c_arm=on.

The quickest health check is i2cdetect from the i2c-tools package. A device with the address 0x20 answers here; a UU instead of a number would mean that a kernel driver has already claimed the address:

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

From Python, the smbus2 package wraps the kernel interface. One register is written, then read back:

#!/usr/bin/env python3
from smbus2 import SMBus

DEVICE_ADDR = 0x20   # 7-bit address
REG_CTRL    = 0x03

with SMBus(1) as bus:
    # write one register
    bus.write_byte_data(DEVICE_ADDR, REG_CTRL, 0x41)

    # read the same register back
    value = bus.read_byte_data(DEVICE_ADDR, REG_CTRL)
    print(f"Register 0x{REG_CTRL:02X} contains 0x{value:02X}")

Mind the voltage levels

An Arduino speaks 5 V, a Raspberry Pi 3.3 V – and the GPIOs of the Pi are not 5 V tolerant. Connecting both boards directly to one bus quietly overstresses the Pi’s inputs, and with a 5 V-powered slave it can end fatally for the port. Either run the whole bus at 3.3 V (an ATmega328P works fine at 3.3 V with 8 MHz), or put a proper level shifter between the two. Details in Level Shifting.

Common pitfalls

  • Clock stretching: the I2C controllers of the BCM2835 generation (all models up to and including the Pi 4) handle clock stretching only to a limited degree. With slaves that hold SCL low for a long time, transfers can fail. Test such slaves on an Arduino, or drive the bus in software (bit-banging via the i2c-gpio overlay).
  • 7-bit versus 8-bit addressing: Wire and smbus2 expect the 7-bit address (0x20). Some datasheets state the 8-bit write address including the R/W bit (0x40 for the same device). If the scanner finds nothing at the datasheet address, halve it.
  • Missing common ground: without a ground connection between the boards, the signals have no common reference – the classic reason why apparently nothing works.
  • Overly long lines: beyond a few tens of centimetres, capacitance and coupled interference grow, the edges flatten and the bus becomes unreliable. Shorten the line, fit suitable pull-ups, or use a bus buffer. A systematic overview is in Common Problems In Systems.

The two platforms at a glance

PropertyArduino UNO / NanoRaspberry Pi (40-pin header)
SDA pinA4GPIO2, pin 3
SCL pinA5GPIO3, pin 5
Logic level5 V3.3 V
Built-in pull-upsapprox. 20–30 kΩ, enabled by Wire1.8 kΩ, hardwired
Typical libraryWire.h (C/C++)smbus2 (Python), i2c-tools