How to Communicate Between Raspberry Pi and Arduino via Serial
To communicate between a Raspberry Pi and an Arduino via
serial, connect their TX and RX pins with a common ground, then use the Raspberry Pi's serial port in Python and Arduino's Serial library to send and receive data. Use a USB cable or direct GPIO serial pins with proper voltage levels for communication.Syntax
On the Arduino side, use the Serial.begin(baud_rate) to start serial communication and Serial.print() or Serial.read() to send or receive data.
On the Raspberry Pi side, use Python's serial.Serial(port, baudrate) to open the serial port and write() or read() methods to communicate.
arduino / python
/* Arduino Syntax */ void setup() { Serial.begin(9600); // Start serial at 9600 baud } void loop() { if (Serial.available() > 0) { int data = Serial.read(); // Read incoming byte Serial.print("Received: "); Serial.println(data); } Serial.println("Hello from Arduino"); delay(1000); } # Raspberry Pi Python Syntax import serial ser = serial.Serial('/dev/serial0', 9600) # Open serial port ser.write(b'Hello from Pi\n') # Send bytes if ser.in_waiting > 0: line = ser.readline() # Read a line print(line.decode('utf-8'))
Example
This example shows how the Arduino sends a message every second and the Raspberry Pi reads and prints it. The Pi also sends a message to Arduino.
arduino / python
/* Arduino Code */ void setup() { Serial.begin(9600); } void loop() { if (Serial.available() > 0) { String msg = Serial.readStringUntil('\n'); Serial.print("Pi says: "); Serial.println(msg); } Serial.println("Hello from Arduino"); delay(1000); } # Raspberry Pi Python Code import serial import time ser = serial.Serial('/dev/serial0', 9600, timeout=1) time.sleep(2) # Wait for connection while True: ser.write(b'Hello from Pi\n') if ser.in_waiting > 0: line = ser.readline().decode('utf-8').strip() print(f'Arduino: {line}') time.sleep(1)
Output
Arduino: Hello from Arduino
Arduino: Hello from Arduino
Arduino: Hello from Arduino
...
Common Pitfalls
- Wrong wiring: Connect Raspberry Pi TX to Arduino RX and Pi RX to Arduino TX with a common ground.
- Voltage mismatch: Raspberry Pi uses 3.3V logic; Arduino Uno uses 5V. Use a level shifter or voltage divider to avoid damage.
- Wrong serial port: On Raspberry Pi, use
/dev/serial0or/dev/ttyAMA0depending on Pi model and config. - Baud rate mismatch: Both devices must use the same baud rate (e.g., 9600).
- Not flushing buffers: Sometimes you need to flush serial buffers to avoid stale data.
iot_protocols
/* Wrong wiring example (do NOT do this): Raspberry Pi TX to Arduino TX */ /* Correct wiring example: Raspberry Pi TX -> Arduino RX, Raspberry Pi RX -> Arduino TX, common GND */
Quick Reference
Raspberry Pi Serial Ports: Use /dev/serial0 for GPIO serial pins or /dev/ttyUSB0 for USB serial.
Arduino Serial Commands: Serial.begin(baud_rate), Serial.print(), Serial.read().
Python Serial Library: import serial, serial.Serial(port, baudrate), write(), readline().
Key Takeaways
Connect Raspberry Pi TX to Arduino RX and Pi RX to Arduino TX with a shared ground for serial communication.
Use matching baud rates on both Raspberry Pi and Arduino to ensure data is correctly sent and received.
Use a voltage level converter if connecting Pi GPIO (3.3V) to Arduino (5V) to protect devices.
On Raspberry Pi, use the correct serial port like /dev/serial0 and Python's serial library for communication.
Test communication with simple send and receive code on both devices to verify setup.