Connecting the Raspberry Pi and the Arduino - dummies

Connecting the Raspberry Pi and the Arduino

By Mike Cook, Jonathan Evans, Brock Craft

Part of Raspberry Pi Projects For Dummies Cheat Sheet

Some people see the Arduino and Raspberry Pi as rival boards, but this isn’t the case at all. If anything, they’re complementary — the weakness of one is the strength of the other. Here are three ways of connecting an Arduino and a Raspberry Pi.


Simply connect the USB connector on the Raspberry Pi to the USB connector on the Arduino. That’s all you need to do. There is a slight curveball in that the Pi can potentially assign the Arduino one of two ports, so when opening up the serial port to the Arduino, use the following code snippet:

import serial
  ser = serial.Serial('/dev/ttyACM0',115200, timeout=2)
except :
  ser = serial.Serial('/dev/ttyACM1',115200, timeout=2)

This assumes that you’ve set up the code in the Arduino to use the serial port at the same 115200 baud rate with a begin command:


You can use any baud rate that the two systems can use, but they have to match. On the Pi side, use ser.write() to send whatever is in the brackets to the Arduino and back = to get one byte back. If there has been nothing received after the timeout period you set when opening the port, the call returns. You can get as many bytes back from this call as you put in the brackets.


This works in software much like USB, but here you connect the GPIO pins on the Raspberry Pi to the TX and RX pins on the Arduino. This method can be handy if you have an Arduino with more than one serial port, like the Arduino Mega. The only snag is that if you’re using a 5V Arduino, you need some level shifting circuits. For receiving a 5V signal into the Pi, a simple resistive divider will suffice, but in going from 3V3 up to 5V you need a transistor.


Any general-purpose NPN transistor can be used here. You communicate in the same way as the USB serial, except you always get the ttyACM0 port.

For both serial methods, you’re transferring bytes not numbers. If you have trouble with this, maybe some other software install has changed the default workings of your serial port on the Pi side. If so, search online for the symptoms of your specific problem.


The I2C (pronounced “I squared C” but often written I2C) can be used to connect the two together. The I2C system is a master/slave arrangement — only the master sends or requests data. The Raspberry Pi is not very well suited for being an I2C slave, so you have to make it the master. The bus requires pull-up resistors, which are already on GPIO pins 2 and 3 on the Raspberry Pi. Unfortunately, the 5V Arduino has its internal pull-up resistor enabled if you’re using the standard I2C library called “Wire”; because this is pulling the lines up to 5V, it could damage your Pi. So you’ll have to hack the wire library (the one used for I2C) or, even better, use a library that allows control of the internal pull-up resistors like the one found at Connection is then simple. Note that there is an example of using the Arduino as an I2C slave in the examples section of the Arduino IDE.


If you don’t want to change the software, you’ll have to use an I2C level shifting circuit.