Skip to main content

Arduino to Raspberry Pi over USB serial (Lab 5c)

In Lab 5c the Arduino reads the sensors and prints lines like Dist=42 and Temp=25.31, and a Python program on the Pi reads those lines through the USB cable. The program is on your lab sheet. This page explains the pieces so you can fix them.

How the USB cable becomes a "serial port"​

The Arduino Uno has a USB-to-serial chip. When you plug it into the Pi, Linux creates a device file for it:

BoardAppears as
Genuine Arduino Uno/dev/ttyACM0
Clone with a CH340 chip/dev/ttyUSB0
A second board...ACM1 / ...USB1

Find it:

ls /dev/ttyACM* /dev/ttyUSB* # what exists now
sudo dmesg | tail # what Linux saw when you plugged it in

Unplug and plug back in while running sudo dmesg -w to see the board appear live (Ctrl+C to stop). Nothing appearing = a charge-only USB cable, a bad port, or the board is not powered.

Who may open the port​

/dev/ttyACM0 belongs to the group dialout:

ls -l /dev/ttyACM0 # crw-rw---- 1 root dialout ...
groups # is "dialout" in your list?

If not: sudo usermod -aG dialout pi, then log out and back in (group changes only apply to new logins). See users and groups.

Only one program at a time​

A serial port can only be opened by one program. These all fight over it:

  • the Arduino IDE Serial Monitor or Serial Plotter,
  • an earlier run of your Python script that is still running,
  • a second Thonny window.

Symptom: could not open port /dev/ttyACM0: [Errno 16] Device or resource busy. Close the others. Find the one holding it:

sudo fuser -v /dev/ttyACM0

What the important lines do​

CodeWhat it doesIf it is wrong
serial.Serial('/dev/ttyACM0', 9600)Opens the port at 9600 baudWrong name → No such file. Wrong speed → garbage characters. Must match Serial.begin(9600) in the sketch.
ser.readline()Waits for one full line (ending in \n)Blocks forever if the Arduino sends nothing. Add timeout=2 to Serial(...) to stop waiting after 2 s.
.decode('utf-8', errors='ignore')Bytes to textWithout errors='ignore', a half-received first line crashes with UnicodeDecodeError.
.rstrip()Removes \r\nWithout it, float() may fail on the line ending.
[5:]Drops the first 5 characters (Dist= / Temp=)Wrong if the label is not exactly 5 characters.

Opening the port resets the Arduino. The sketch restarts, so the first line can be cut off or empty. That is normal; skip it.

A safer way to read​

The lab program assumes the lines always come in the order Dist, Temp. If the Pi starts reading halfway, the values swap and float() crashes. Checking the label is more robust:

import serial

ser = serial.Serial('/dev/ttyACM0', 9600, timeout=2)
ser.reset_input_buffer() # throw away old, half-read data

while True:
line = ser.readline().decode('utf-8', errors='ignore').strip()
if not line:
continue # timeout or empty line
label, _, value = line.partition('=') # "Dist=42" -> "Dist", "=", "42"
try:
number = float(value)
except ValueError:
continue # damaged line, skip it
print(label, number)

Testing without Python​

See the raw data from the Pi's terminal:

stty -F /dev/ttyACM0 9600 raw -echo
cat /dev/ttyACM0 # Ctrl+C to stop

If cat shows clean Dist= / Temp= lines, the Arduino side is fine and the problem is in your Python.

Common problems​

ProblemCause and fix
could not open port /dev/ttyACM0: No such file or directoryArduino not plugged into the Pi (still in the PC?), or it is /dev/ttyUSB0. ls /dev/tty*
[Errno 13] Permission denied: '/dev/ttyACM0'Not in the dialout group. Add, then log out and in.
[Errno 16] Device or resource busyAnother program has the port. Close the Serial Monitor; stop old runs.
ModuleNotFoundError: No module named 'serial'Wrong interpreter. See venv and Thonny.
AttributeError: module 'serial' has no attribute 'Serial'Wrong serial package, or your file is called serial.py
Garbage characters (�x�)Baud rate mismatch: both sides must be 9600.
could not convert string to floatLine cut off, empty, or out of order. Use the safer version.
Nothing printed at allWrong sketch on the Arduino (re-upload from the PC), or readline() waiting forever. Test with cat.
Port name changes to ttyACM1The old port was still open when the board was replugged. Close everything and replug.

Next​

Save the readings in MariaDB: Storing sensor data (Lab 5d).