Serial Connection
You can connect an Arduino to a Raspberry Pi in two ways: USB or via the GPIO pins. USB is the simplest and most reliable method, but if the Pi's USB ports are occupied or inaccessible, you can fall back on the GPIO pins with a bit of extra hardware and setup.
Serial Over USB (The Easy Way)
The easiest method is to connect the Arduino directly to the Raspberry Pi using a standard USB cable.
Serial Over RPi GPIO Pins
On the side opposite the display ports, the Raspberry Pi features a 40-pin header. Of these, 27 pins serve as general-purpose input/output (GPIO) pins, which can be configured for various functions, including serial communication.
Voltage Differences
The Arduino and Raspberry Pi use different logic voltages: the Arduino runs at 5V, while the Raspberry Pi runs at 3.3V. Connecting them directly through the GPIO pins can permanently damage the Pi. To safely bridge the two, you’ll need voltage regulation. While there are several ways to achieve this, this guide focuses on using a logic level converter, the safest and simplest method.
What is a Logic Level Converter?
A logic level converter merely steps voltage signals up or down between two independent circuits.
In addition to the information provided in this guide, you may also find SparkFun's detailed Hookup Guide helpful.
Components
You'll need the following or similar:
- Logic Level Converter ($4 Sparkfun link)
- Male-to-Female Dupont Jumper Wires ($7 Amazon link)
- Male-to-Male Jumper Wires ($7 Amazon link)
- A small breadboard or PCB ($7 Amazon link)
Enable Serial Over GPIO Pins
By default, the Raspberry Pi GPIO pins are not enabled and must be activated using the Raspberry Pi Configuration Tool.
Using raspi-config
When the tool opens select Interface Options.
Select Serial Port.
Select No to login over serial.
Select Yes to enable serial GPIO hardware.
Click OK and then Finish.
Now, restart the Raspberry Pi.
After restarting, you should be able to see the GPIO port ttyS0 using the ls and grep commands.
If ttyS0 is listed, the GPIO pins are configured and ready to be wired to the Arduino.
Wiring the Logic Level Converter
Identifying TX/RX Pins
Do not connect pins TX to TX and RX to RX. Instead, cross them between the two devices. Connect the transmit pin of the Arduino to the receive pin of the RPi and the transmit pin of the RPi to the receive pin of the Arduino.
Build the Circuit
Reset Line
In the diagram above, notice the purple connection between the Arduino RESET and RPi GPIO 4 pins.cThis circuit is not part of the serial data connection but is instead used to reset the Arduino, and covered in the Electrically Resetting the Arduino from the RPi section later.
LLC Pins
Ensure the Arduino is connected to the high voltage (HV) side and the RPi is connected to the low voltage (LV) side.
There's nothing special about any of the four channels of the logic level converter. Any channel can be used for any signal.
| Channel | Signal |
|---|---|
| HV4 to LV4 | (any signal) |
| HV3 to LV3 | (any signal) |
| HV2 to LV2 | (any signal) |
| HV1 to LV1 | (any signal) |
Testing Serial Connectivity
Once connected by USB or GPIO, run the board list command to determine if the RPi can detect your Arduino.
An Arduino connected via USB should show under port /dev/ttyACM0.
An Arduino connected via GPIO pins should show under port /dev/ttyS0.
The Arduino is listed as Unknown in this example
The board showing as unknown doesn't necessarily mean there is a problem with the connection. Currently boards connected over GPIO may not be correctly indentified.