Raspberry Pi With Arduino: I2C Communication (Pi 5 + UNO, Python, C++)

In this tutorial, we’ll make a Raspberry Pi with Arduino work together over I2C: the Raspberry Pi 5 is the I2C master, and an Arduino UNO is an I2C slave at address 0x08. You’ll learn how to wire the two boards safely (and what the UNO’s own pull-ups do to a 3.3v Raspberry Pi), how the Arduino Wire library’s onReceive and onRequest handlers work, and how to turn the UNO into a register device that the Raspberry Pi reads like any I2C sensor. The Raspberry Pi side is written twice, in Python (smbus2) and C++ (Linux i2c-dev).

We’ll answer the question: can a 3.3v Raspberry Pi 5 and a 5v Arduino UNO share an I2C bus safely, and how does the Raspberry Pi read data back from the Arduino? You’ll see four complete examples (sending bytes, reading them back, a register protocol with a repeated START, and controlling the UNO’s LED from a potentiometer it reads), an oscilloscope test of clock stretching on the Raspberry Pi 5, and error counts at 100kHz and 400kHz. Without further ado, let’s get right into it!

Raspberry Pi With Arduino Series: Part 3 of 4
1 USB Serial2 UART3 I2C4 SPI

Note: All examples in this tutorial are tested with: Raspberry Pi 5 (8GB) with Raspberry Pi OS (64-bit) Trixie, an Arduino UNO (Arduino IDE 2), and a BSS138 level shifter.

This tutorial assumes your Raspberry Pi is set up with Raspberry Pi OS, reachable over SSH, and has I2C enabled. If it isn’t, follow the Raspberry Pi Setup Guide first, and enable I2C as shown in my Raspberry Pi I2C tutorial, which also covers the I2C basics this tutorial builds on.

Raspberry Pi With Arduino I2C Communication

To connect a Raspberry Pi to an Arduino over I2C, wire the Pi’s SDA (GPIO2, pin 3) and SCL (GPIO3, pin 5) to the Arduino UNO’s A4 (SDA) and A5 (SCL) through a 3.3v to 5v I2C level shifter, with a common ground. Make the UNO an I2C slave with Wire.begin(0x08), handle incoming bytes in Wire.onReceive() and requests in Wire.onRequest(), and talk to it from the Pi with smbus2:

Arduino Code example (Arduino UNO I2C slave at 0x08, prints what it receives and echoes it back)

Python Code example (Raspberry Pi I2C master, sends 6 bytes and reads them back)

Raspberry Pi with Arduino I2C: Python smbus2 sends Pi5:42 to the UNO at 0x08 and reads the same 6 bytes back
Raspberry Pi with Arduino I2C: Python smbus2 sends Pi5:42 to the UNO at 0x08 and reads the same 6 bytes back

The rest of this tutorial explains the wiring and the pull-up problem, the Wire slave rules, a register protocol for real projects, clock stretching on the Raspberry Pi 5, and how fast the link runs without errors.


I2C vs SPI vs UART Between a Raspberry Pi and an Arduino

A Raspberry Pi and an Arduino can talk over four links. This tutorial is about I2C; pick the link by what your project needs:

Raspberry Pi with Arduino: 4 ways to connect them, USB, UART, I2C and SPI compared

USB SerialUART (GPIO)I2CSPI
Wires1 USB cableTX, RX, GNDSDA, SCL, GNDSCLK, MOSI, MISO, CS, GND
Level shifter for a 5v UNONoYes (on the UNO’s TX)Yes (5v pull-ups)Yes
Who starts a transferEither sideEither sidePi (master)Pi (master) only
Several ArduinosOne per USB portOne per UARTUp to 112 by addressOne CS pin each
Speed in practice115,200 baud typical115,200 baud typical100–400kHzUp to a few MHz
Best forProgramming + data on one cableSimple text commandsSeveral devices on two wires, sensor-style registersFast, fixed-size frames

I2C is the link to pick when the Arduino should look like one more sensor on the Raspberry Pi’s bus: two wires, an address, and registers. Its limits are speed and the 5v pull-up problem, which the next section solves. Every link in this series runs on the same Raspberry Pi 5 and Arduino UNO; for why a project uses both boards at all, see Raspberry Pi vs Arduino.


Raspberry Pi Arduino I2C Wiring: Level Shifter or Direct?

Note: Use an I2C level shifter between a 5v Arduino UNO and a Raspberry Pi. The Raspberry Pi’s GPIO pins are 3.3v only and aren’t 5v tolerant, per the Raspberry Pi documentation, and the UNO expects 5v logic levels on its I2C pins.

Many guides wire the UNO straight to the Raspberry Pi with the argument that the UNO board has no I2C pull-up resistors, so only the Pi’s 1.8kΩ pull-ups to 3.3v set the bus level. Two facts from the datasheet and the Arduino source say otherwise:

  • Wire.begin() turns on the ATmega328P’s internal pull-ups. The AVR core’s twi_init() does it on purpose (“activate internal pullups for twi”, twi.c), and they are 20–50kΩ to 5v (ATmega328P datasheet, Table 30-1). Against the Pi’s 1.8kΩ to 3.3v, that divider lifts the idle bus to roughly 3.36–3.44v, above the Pi’s 3.3v supply, with a small current flowing into the Pi’s 3.3v rail through its pull-ups. It rarely damages anything, but it’s outside what the Pi is specified for.
  • The UNO needs 3.5v for a guaranteed HIGH. The ATmega328P’s I2C inputs are specified at VIH = 0.7 × VCC = 3.5v at 5v (datasheet, Table 29-14), and a 3.3v bus never reaches it. It often works on the bench because the real threshold is lower than the guaranteed one, but there’s no noise margin left, and errors show up with longer wires or higher speeds.

A bidirectional level shifter solves both. I use a common 4-channel BSS138 level shifter: each channel is a BSS138 MOSFET with a 10kΩ pull-up on each side, the circuit from NXP’s AN10441. Its LV side stays at the Raspberry Pi’s 3.3v, its HV side swings the UNO’s full 5v, and either side can pull the line low, which is what I2C’s open-drain lines need. On my bench, the Raspberry Pi side of the shifter idles at 3.31v (SDA) and 3.33v (SCL), right at the Pi’s own 3.3v rail, measured on the oscilloscope at GPIO2 and GPIO3.

Wiring Diagram and Parts

QTY.Component NameAmazon.comAliExpress
1Raspberry Pi 5 (4GB or 8GB)AmazonAliExpress
1Raspberry Pi 27W USB-C Power SupplyAmazonAliExpress
1Arduino UNO R3––
1BSS138 4-Channel I2C Logic Level Shifter––
110kΩ Potentiometer––
1LED (5mm) + 330Ω Resistor––
1BreadboardAmazonAliExpress
1Jumper Wires (Female-Male + Male-Male)AmazonAliExpress
Raspberry Pi with Arduino I2C wiring diagram: Raspberry Pi 5 GPIO2 SDA and GPIO3 SCL to an Arduino UNO through a BSS138 level shifter, LED on D9, potentiometer on A0

Raspberry Pi 5 Level Shifter / UNO 3.3v (pin 1) Shifter LV GND (pin 6) Shifter GND (LV) GPIO2 SDA (pin 3) LV1 ↔ HV1 ↔ UNO SDA GPIO3 SCL (pin 5) LV2 ↔ HV2 ↔ UNO SCL
Arduino UNO Connects To 5V Shifter HV + pot GND Shifter GND + pot SDA (A4) Shifter HV1 SCL (A5) Shifter HV2 D9 330Ω → LED → GND A0 Potentiometer wiper

The Pi’s GPIO2 (SDA) and GPIO3 (SCL) go through the shifter to the UNO’s SDA (A4) and SCL (A5); the UNO R3 also brings them out next to AREF. The shifter’s LV side takes 3.3v from the Pi, its HV side takes 5V from the UNO, and both GND pins tie the grounds together. On the UNO, D9 drives an LED through a 330Ω resistor, and A0 reads a 10kΩ potentiometer between 5V and GND.

Power and Ground

Power each board from its own USB supply: the Pi from its USB-C supply, and the UNO from your PC (that’s also how you upload sketches and watch its Serial Monitor). Don’t connect the Pi’s 5v pin to the UNO’s 5V pin; the only connections between the boards are SDA, SCL, and ground. On this shifter the LV and HV GND pins are one net, so the Pi’s GND (pin 6) and the UNO’s GND meet there. Without that common ground, the Pi gets no answer at all (see Troubleshooting).


Raspberry Pi i2cdetect Finds the Arduino at 0x08

Upload the echo sketch from the code example to the UNO, then scan the bus from the Raspberry Pi. The UNO answers at the address you passed to Wire.begin():

Raspberry Pi i2cdetect -y 1 shows the Arduino UNO I2C slave at address 0x08
Raspberry Pi i2cdetect -y 1 shows the Arduino UNO I2C slave at address 0x08

The scan itself reaches the UNO’s sketch: i2cdetect probes each address with a zero-byte write, so the echo sketch’s onReceive runs with howMany = 0 and prints “Received 0 bytes” (see the Serial Monitor in Example 1). Guard your handler against empty writes, as the register sketch does with if (howMany < 1) return;.

Pick any address from 0x08 to 0x77. Addresses 0x00–0x07 and 0x78–0x7F are reserved by the I2C specification (general call, CBUS, high-speed mode, 10-bit addressing), which is why i2cdetect doesn’t even scan them; my Raspberry Pi I2C tutorial explains the scan in detail. If you add sensors to the same bus, give the UNO an address none of them uses.


Arduino Wire Slave Explained: onReceive and onRequest

The Arduino Wire library turns the UNO into an I2C slave with one call, Wire.begin(address), and two handlers. The master (the Raspberry Pi) starts every transfer; the UNO only reacts:

  • Wire.onReceive(handler): the handler runs after the Pi has written bytes to the UNO, at the STOP or at a repeated START. Its argument is the byte count, and Wire.read() returns the bytes.
  • Wire.onRequest(handler): the handler runs when the Pi starts a read from the UNO, right after the address byte. Whatever it passes to Wire.write() goes back to the Pi.
Arduino Wire slave: onReceive runs at the STOP or repeated START of a write, onRequest runs after the address of a read while the UNO stretches SCL

Rules for Code Inside the Handlers

Both handlers run inside the ATmega328P’s TWI interrupt (twi.c in the AVR core calls them from ISR(TWI_vect)). While onRequest() runs, the UNO holds SCL low and the whole bus waits. So:

  • No Serial.print() and no delay() in a handler. Set a flag and print from loop(), as both sketches here do. Serial output is interrupt-driven too, and its 64-byte buffer can fill inside the handler; then Serial.print() waits for room while the UNO holds the bus.
  • Mark shared variables volatile, and copy multi-byte values in loop() with interrupts off ( noInterrupts() / interrupts()), or the handler can see half of an update.
  • Keep them short: copy bytes in, copy bytes out. Do the real work (analogWrite, sensor reads) in loop().
  • One Wire.write() buffer per request: several Wire.write() calls in onRequest() append to the same buffer (AVR core 1.8.x).

The 32-Byte Wire Buffer Limit

The AVR Wire library has one 32-byte buffer per direction: #define BUFFER_LENGTH 32 in Wire.h and TWI_BUFFER_LENGTH 32 in twi.h. That sets three limits for a UNO slave:

  • A write from the Pi can carry at most 32 bytes. The UNO ACKs bytes 1–32 and NACKs byte 33, so the Pi’s write fails. On the Raspberry Pi 5 this NACK on the last byte is not reported: a 33-byte write returned no error at all, and the UNO’s Serial Monitor showed “Received 32 bytes”. The 33rd byte was silently lost.
  • A single Wire.write(buf, n) with n over 32 in onRequest() is rejected completely: twi_transmit() refuses the whole block, yet Wire.write() still returns n (AVR core 1.8.x source). Keep each reply at 32 bytes or less.
  • If the Pi reads more bytes than onRequest() wrote, the UNO stops driving SDA after its last byte and the Pi reads 0xFF for the rest. Reading 8 bytes after a 6-byte message returned b’Pi5:42\xff\xff’ on my bench.

For longer messages, split them into chunks of up to 32 bytes, or let the Pi read a register map piece by piece, as in Example 3.


Example 1: Send Bytes From the Raspberry Pi to the Arduino

Upload the Arduino_I2C_Slave_Echo sketch from the code example to the UNO and open the Serial Monitor at 115200 baud. The Raspberry Pi writes a 9-byte text message in one I2C transaction; the UNO’s onReceive handler stores it, and loop() prints it.

Python Code example (Raspberry Pi sends a text message to the Arduino)

smbus2’s i2c_msg.write() with i2c_rdwr() sends raw I2C bytes. The SMBus-style calls ( write_byte(), write_i2c_block_data()) work too, but they add a command byte in front; that’s exactly what Example 3 uses as the register number.

C++ Code example (Raspberry Pi sends a text message to the Arduino)

In C++, ioctl(fd, I2C_SLAVE, 0x08) sets the address once, and a plain write() sends one transaction. Build and run it on the Pi:

Raspberry Pi sends Hello UNO to the Arduino over I2C, Python and C++ output
Raspberry Pi sends Hello UNO to the Arduino over I2C, Python and C++ output

Arduino UNO Serial Monitor: Received 9 bytes: Hello UNO, written by the Raspberry Pi over I2C


Example 2: Read Bytes From the Arduino on the Raspberry Pi

Reading is the part most Raspberry Pi to Arduino I2C guides skip. In an I2C read, the Pi clocks the bytes out of the UNO: it sends the address with the read bit, the UNO’s onRequest handler loads its reply with Wire.write(), and the Pi ACKs each byte until the last one, which it NACKs. The echo sketch answers with the last message it received.

Python Code example (Raspberry Pi writes 6 bytes, then reads them back from the Arduino)

C++ Code example (Raspberry Pi writes 6 bytes, then reads them back from the Arduino)

Raspberry Pi reads bytes back from the Arduino UNO over I2C, Python and C++ output
Raspberry Pi reads bytes back from the Arduino UNO over I2C, Python and C++ output

The Pi decides how many bytes to read; the UNO can’t know in advance. If you ask for more than the UNO wrote, the extra bytes come back as 0xFF (see Troubleshooting). That’s why real projects use a protocol with fixed sizes, the next example.


Example 3: Arduino I2C Register Protocol With Repeated START

I2C sensors all work the same way: the master writes a register number, then reads bytes starting at that register. Give the UNO the same behavior and the Raspberry Pi reads it with the same calls it uses for a BME280 or an MPU6050. Here’s the register map of the Arduino_I2C_Slave_Registers sketch and the two frames it understands:

Arduino UNO I2C register protocol: register map (WHO_AM_I, LED, PWM, A0, UPTIME, RX_COUNT) and the write and read frames

Arduino Code example (Arduino UNO as an I2C register device)

The first byte of every write is the register pointer; any further bytes are values, written to consecutive registers. onRequest() sends from the pointer to the end of the map, and the Pi’s NACK sets how many of those bytes it reads. Multi-byte values are high byte first, and the uptime is copied into the map inside onRequest(), so all four bytes belong to the same moment.

Repeated START vs Two Transactions

A register read can go out in two ways. With a repeated START (Sr), the Pi writes the register and turns the bus around without releasing it: one transaction, and no other master can step in between. With two transactions, the Pi writes the register, sends a STOP, then starts a new read. Both work with Wire, because the AVR core runs onReceive at a repeated START exactly as at a STOP ( TW_SR_STOP in twi.c), so the pointer is set before onRequest runs.

Python Code example (read registers with an SMBus call, a repeated START, and two transactions)

C++ Code example (read registers with I2C_RDWR and with two transactions)

Raspberry Pi reads the Arduino's WHO_AM_I and uptime registers over I2C with a repeated START and with two transactions
Raspberry Pi reads the Arduino’s WHO_AM_I and uptime registers over I2C with a repeated START and with two transactions

Here is what the bus looks like in each case, on my oscilloscope with I2C decoding on (CH1 yellow = SDA, CH3 = SCL, at the Raspberry Pi’s pins):

Raspberry Pi to Arduino I2C register write on the oscilloscope: 0x08 W, register 0x01, values, decoded
Raspberry Pi to Arduino I2C register write on the oscilloscope: 0x08 W, register 0x01, values, decoded

Raspberry Pi reads an Arduino register with a repeated START: 0x08 W 0x12, Sr, 0x08 R and 4 data bytes, decoded on the oscilloscope
Raspberry Pi reads an Arduino register with a repeated START: 0x08 W 0x12, Sr, 0x08 R and 4 data bytes, decoded on the oscilloscope

Raspberry Pi reads an Arduino register as two I2C transactions: write 0x12 and STOP, then START and read 4 bytes, decoded on the oscilloscope
Raspberry Pi reads an Arduino register as two I2C transactions: write 0x12 and STOP, then START and read 4 bytes, decoded on the oscilloscope

In the repeated-START read, the bus is never released between the register byte and the read: one transaction. As two transactions, the Pi sends a STOP after the register byte and starts the read 134µs later (the second ioctl call from Python), and the UNO still answers with the right register, because the pointer survives between transactions. In both reads, notice that SDA’s LOW level sits a little higher whenever the UNO drives it: the UNO’s LOW reaches the Pi through the BSS138, so you can tell from the trace who is talking.

The Python helper class uno_i2c.py wraps the repeated-START read and the write; Example 4 uses it:


Example 4: Control the Arduino LED and Read A0 From the Raspberry Pi

The Raspberry Pi has no analog inputs; the UNO has a 10-bit ADC (one of the reasons to use both boards). In this example the Pi reads the potentiometer on the UNO’s A0 every 50ms, sets the LED’s PWM duty on D9 to the same position (0–1023 → 0–255), and turns the on-board LED on in the upper half. Both outputs go out in one 3-byte write, [0x01, LED, PWM], thanks to the auto-increment.

Python Code example (Raspberry Pi reads the Arduino’s A0 and sets its LED and PWM)

C++ Code example (Raspberry Pi reads the Arduino’s A0 and sets its LED and PWM)

Raspberry Pi reads the Arduino UNO's A0 potentiometer over I2C and sets the LED PWM duty
Raspberry Pi reads the Arduino UNO’s A0 potentiometer over I2C and sets the LED PWM duty

Raspberry Pi with Arduino I2C demo: turning the potentiometer on the UNO’s A0 changes the LED brightness set by the Raspberry Pi


Raspberry Pi 5 I2C Clock Stretching With an Arduino

A slave may hold SCL low to make the master wait: that’s clock stretching, and the ATmega328P does it on every byte its interrupt has to handle (“While the TWINT Flag is set, the SCL low period is stretched”, ATmega328P datasheet). The longest stretch comes in onRequest(), because the UNO runs your handler before it can send the first byte.

This used to be a real problem. The BCM2835 I2C controller in older Raspberry Pi models has a known clock-stretching bug (Advamation’s analysis, raspberrypi/linux issue #254): it ignores a stretch at certain points of the transfer, and the data gets corrupted, which is why old tutorials lower the bus speed or avoid Arduino slaves. The Raspberry Pi 5 uses a different controller, the Synopsys DesignWare I2C block in the RP1 chip (RP1 peripherals datasheet). Here is what it does with the UNO:

Raspberry Pi 5 I2C clock stretching: the Arduino UNO holds SCL low while onRequest runs, measured on the oscilloscope
Raspberry Pi 5 I2C clock stretching: the Arduino UNO holds SCL low while onRequest runs, measured on the oscilloscope

The UNO’s interrupt holds SCL LOW for about 20µs right after the address of the read, instead of the usual 5µs LOW of a 100kHz clock, while onRequest() fills the buffer (the register sketch sends 6 bytes). With the bench sketch, whose onRequest() copies 32 bytes, the LOW grew to about 50µs. The Raspberry Pi 5 waited every time: 0 errors in 100,000 transactions across both speeds.

Arduino onRequest copying 32 bytes stretches the Raspberry Pi 5 I2C clock longer, measured on the oscilloscope
Arduino onRequest copying 32 bytes stretches the Raspberry Pi 5 I2C clock longer, measured on the oscilloscope

To find the limit, the bench sketch adds a delay of 0.1ms to 25.5ms inside onRequest(), set over I2C from the Pi, and the Pi reads the WHO_AM_I register after each setting. A 1ms stretch is no problem for the Raspberry Pi 5:

Raspberry Pi 5 waits through a 1ms I2C clock stretch from the Arduino and reads 0xA5
Raspberry Pi 5 waits through a 1ms I2C clock stretch from the Arduino and reads 0xA5

At 12ms, the Pi aborts the transfer:

Raspberry Pi 5 aborts after a 12ms I2C clock stretch and the Arduino UNO is left holding SDA low
Raspberry Pi 5 aborts after a 12ms I2C clock stretch and the Arduino UNO is left holding SDA low

Extra delay in onRequest()SCL held low (measured)Read result on the Pi
None (register sketch)≈ 20µs (normal LOW 5µs)0xA5, OK
1ms≈ 1.04ms0xA5, OK
2ms / 5ms / 10ms0xA5, OK
10.5msOSError: [Errno 121] Remote I/O error
12ms≈ 12.0msErrno 121, then SDA stuck LOW
20msErrno 121, then SDA stuck LOW

So the Raspberry Pi 5 handles clock stretching correctly, unlike the BCM2835 models, up to about 10ms per stretch. Beyond that, the RP1 controller gives up with Errno 121, and the UNO is left in the middle of its byte, holding SDA LOW: every following transfer fails (Errno 5, and the kernel logs “SDA stuck at low”) until the UNO is reset. Normal Wire handlers take microseconds, far below the limit; the lesson is to never put delay() or slow code in onRequest().


Raspberry Pi Arduino I2C Speed: 100kHz vs 400kHz

The Raspberry Pi 5’s I2C bus runs at 100kHz by default; dtparam=i2c_arm_baudrate=400000 in /boot/firmware/config.txt (and a reboot) sets 400kHz, as shown in my Raspberry Pi I2C tutorial. The ATmega328P is specified up to 400kHz. To see what that buys and what it costs, I ran 10,000 echo transactions (write 16 random bytes, read them back, compare) at each setting, and timed one 2-byte register read 2,000 times in Python and C++ ( bench/i2c_bench.py and i2c_bench.cpp in the download):

SettingErrors / transactionsOne register read, Python (median)One register read, C++ (median)
100kHz (default)0 / 10,000 Python, 0 / 10,000 C++569µs565µs
400kHz0 / 10,000 Python, 0 / 70,000 C++228µs224µs
Raspberry Pi to Arduino I2C round trip of one register read at 100kHz and 400kHz, Python vs C++

Both speeds ran without a single error through the BSS138, so the UNO is a good 400kHz citizen, unlike the PCF8574 LCD backpack in my I2C LCD tutorial, which is rated for 100kHz only and NACKed at 400kHz through the same shifter. 400kHz cuts one register read from about 570µs to about 225µs, 2.5 times faster. Python and C++ are within 5µs of each other: the time goes to the bus and the UNO’s interrupts, not to the language, so pick the language for the rest of your program.


Several Arduinos (or Sensors) on One I2C Bus

Every device on the bus needs its own address, and that’s all: give each Arduino a different Wire.begin() address (0x08, 0x09, 0x0A…) and wire all SDA pins together and all SCL pins together. The Raspberry Pi then picks the Arduino by address in every call, exactly as it picks sensors. Three rules keep a mixed bus healthy:

  • 5v devices on the HV side, 3.3v devices on the LV side of one shifter. Several UNOs and 5v modules can share the HV side; 3.3v sensors (a BMP280, for example) connect straight to the Pi’s GPIO2 and GPIO3.
  • The slowest device sets the bus speed. The UNO ran error-free at 400kHz, but a PCF8574 LCD backpack is rated for 100kHz only (see my Raspberry Pi I2C LCD tutorial); with both on one bus, stay at 100kHz.
  • Every pull-up adds up. Each 5v module and each UNO’s internal pull-ups sit in parallel on the HV side. Two or three are fine; with many modules, remove the extra pull-up resistors from the modules.

Each Arduino on the bus also clock-stretches during its own handlers, which only delays the transfers addressed to it.


Raspberry Pi Arduino I2C Troubleshooting

The Arduino Doesn’t Show Up in i2cdetect

Check, in this order: the common ground (without it the Pi sees nothing), SDA and SCL not swapped, the shifter powered on both sides (LV from the Pi’s 3.3v, HV from the UNO’s 5V), the sketch running (an i2cdetect scan during the UNO’s 1.6s reset shows an empty bus), and the address in Wire.begin(). The address must be 0x08 to 0x77; Wire accepts reserved addresses, but i2cdetect doesn’t scan them.

OSError: [Errno 121] Remote I/O error

Errno 121 is Linux’s EREMOTEIO: nobody ACKed the address. It’s the same causes as above, a wrong address in the Python code (here 0x09 instead of 0x08), or the UNO busy resetting. It also appears when the UNO stretches the clock for more than about 10ms (see the clock-stretching section).

Raspberry Pi smbus2 OSError: [Errno 121] Remote I/O error when reading address 0x09 with no device
Raspberry Pi smbus2 OSError: [Errno 121] Remote I/O error when reading address 0x09 with no device

Reads Return 0xFF or Old Data

0xFF means the UNO wasn’t driving SDA: you read more bytes than onRequest() wrote, onRequest() wasn’t registered, or the register pointer is past the end of the map. Old data usually means the register pointer isn’t where you think: every write moves it (auto-increment), so set the register in the same transaction as the read (repeated START). A write that’s longer than you meant can also overwrite registers: on my bench, a 33-byte test write to register 0x00 landed in LED, PWM and the bench registers that followed.

Raspberry Pi reads 8 bytes from an Arduino that wrote 6: the last two bytes are 0xFF
Raspberry Pi reads 8 bytes from an Arduino that wrote 6: the last two bytes are 0xFF

More Than 32 Bytes

Keep every write at 32 bytes or less, including the register byte. On the Raspberry Pi 5 the 33rd byte isn’t an error, it’s silently dropped. To move more data, split it into 32-byte chunks with a register address per chunk.

Raspberry Pi writes 33 bytes to the Arduino with no error; reading back gives 32 bytes then 0xFF
Raspberry Pi writes 33 bytes to the Arduino with no error; reading back gives 32 bytes then 0xFF

Serial.print() Inside onReceive or onRequest

The echo sketch’s onReceive() only copies bytes and sets a flag; loop() prints. That’s the pattern to keep. Printing inside a handler delays the ACK of the next byte and, once the 64-byte Serial buffer is full, waits for the UART with the I2C bus stretched, which can push a transfer into the Pi’s 10ms limit.

The Arduino Resets When Its Serial Port Opens

Opening the UNO’s USB serial port (the Serial Monitor, a terminal program, or a script) asserts DTR, and the UNO’s auto-reset circuit restarts the sketch. For about 1.6s the UNO isn’t on the bus, and the Pi’s transfers fail with Errno 121. In Part 1 of this series, the sketch was back 1.57s after every port open. On my bench, the UNO stopped answering the Pi for 1.565s each time its port opened; the Pi’s reads failed with Errno 121 until the sketch was back. Open the Serial Monitor before you start the Pi program, or retry a failed read after a short pause.

Raspberry Pi loses the Arduino UNO on the I2C bus for 1.565s while the UNO resets after its serial port opens
Raspberry Pi loses the Arduino UNO on the I2C bus for 1.565s while the UNO resets after its serial port opens


Raspberry Pi Arduino I2C FAQ

Can the Raspberry Pi be an I2C slave?

Not on the Raspberry Pi 5 with Raspberry Pi OS. The RP1’s I2C controller supports slave mode in hardware (RP1 peripherals datasheet, section 3.5), but the kernel driver doesn’t offer it: i2cdetect -F 1 reports “Target mode: no” on my Raspberry Pi 5. Make the Arduino the slave, as in this tutorial.

Can I connect the Arduino to the Raspberry Pi without a level shifter?

It works on many UNOs, but it’s outside both chips’ specifications: Wire.begin() pulls the bus toward 5v through the ATmega328P’s internal pull-ups, and the UNO needs 3.5v for a guaranteed HIGH. A BSS138 level shifter costs less than a dollar per channel and removes both problems, so every example here uses one.

Does a 3.3v Arduino need a level shifter?

No. A board that runs at 3.3v (an Arduino Pro Mini 3.3v, Arduino Due, or Nano 33) can connect pin to pin with a common ground, because its pull-ups and input thresholds are already at 3.3v.

Can an ESP32 be the I2C slave instead?

Yes, with the same onReceive and onRequest handlers, but the ESP32 Arduino core adds Wire.slaveWrite() to pre-fill the reply buffer (Espressif’s I2C documentation). It’s a 3.3v board, so no level shifter is needed.

Should I use I2C or SPI between a Raspberry Pi and an Arduino?

Use I2C when the Arduino shares the bus with other I2C devices or when you want two wires and sensor-style registers; use SPI when you need speed and fixed-size frames. For plain text commands, USB serial needs no wiring at all.


Raspberry Pi With Arduino Tutorial Series

This tutorial is the third part of our Raspberry Pi With Arduino tutorial series:

PartTutorialWhat You’ll Build
1Raspberry Pi With Arduino: USB Serial and ProgrammingProgram the UNO from the Pi (arduino-cli, Arduino IDE), USB serial in Python and C++
2Raspberry Pi With Arduino: UART CommunicationSerial over the GPIO pins with a level shifter, Python and C++
3Raspberry Pi With Arduino: I2C Communication (this tutorial)The UNO as an I2C target, register protocol, clock stretching
4Raspberry Pi With Arduino: SPI CommunicationThe UNO as an SPI slave, the one-byte lag, a framed protocol

Download Raspberry Pi With Arduino I2C Examples

The download contains the two Arduino sketches, every Raspberry Pi example in Python and C++, and the bench sketch and programs behind the measurements, with a README that lists each command.

Download Raspberry Pi With Arduino I2C Examples (Arduino, Python, C++)

Download all attachment files for this Article/Tutorial (project files, schematics, code, etc.) using the link below. Everything is tested and verified to work on real hardware, as you’ve seen in the demo videos & screenshots. Please consider supporting my work if it helped you.


Wrap Up

To conclude this tutorial, we’ve connected a Raspberry Pi 5 to an Arduino UNO over I2C through a BSS138 level shifter, explained why the shifter matters, made the UNO an I2C slave with the Wire library’s onReceive and onRequest handlers, and turned it into a register device that the Pi reads with a repeated START, in Python and C++.

This was Part 3 of the Raspberry Pi With Arduino series: Part 1 covers USB serial and uploading sketches, Part 2 the GPIO UART link, and Part 4 the faster SPI link. For two Arduinos talking over I2C, see my Arduino I2C slave tutorial; for the Raspberry Pi’s I2C pins, bus speed and scanning, the Raspberry Pi I2C tutorial; and for the UART alternative, the Raspberry Pi UART tutorial.

Share This Page With Your Network!
Join Our +25,000 Newsletter Subscribers!

Stay Updated With All New Content Releases. You Also Get Occasional FREE Coupon Codes For Courses & Other Stuff!

Photo of author
Author
Embedded systems engineer with several years of experience in embedded software and hardware design. I work as an embedded SW engineer in the Automotive & e-Mobility industry. However, I still do Hardware design and SW development for DSP, Control Systems, Robotics, AI/ML, and other fields I'm passionate about.
I love reading, writing, creating projects, and teaching. A reader by day and a writer by night, it's my lifestyle. I believe that the combination of brilliant minds, bold ideas, and a complete disregard for what is possible, can and will change the world! I will be there when it happens, will you?

Leave a Comment