Nikitha Narendra
Posts: 2
Joined: Sun Dec 06, 2020 6:45 am

Arduino - Raspberry pi connection

Fri Jun 11, 2021 3:57 am

I have a raspberry pi 3B+ and an arduino leonardo. Ive written a an arduino code to control dc motors and servo motors and print a char to serial port (eg, forward print 'f') raspberry pi reads this character and prints it.
The issue is, when i shutdown the raspberry pi and run the script, nothing gets printed. But if i reupload the arduino script/rst it, the script continues as normal.
On doing a bit of digging, ive found out that arduino and raspberry pi are writing/reading to /dev/ttyACM0, but after shuutdown and then power on, both arduino and raspberry pi can connnect to this file (using piserial) but cannot read/write to this file. When i tried to cat this file, it says resource is busy.
P.S. No error is generated , neither on pi, not on arduino. The serial connection is established without error, the first character is written, then eveything gets stuck on an infinite loop. But once i refresh arduino everything resumes as normal.
Im not sure why this is happening.

User avatar
Posts: 3250
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near The National Museum of Computing

Re: Arduino - Raspberry pi connection

Fri Jun 11, 2021 8:21 am

Are you using pySerial and readline() ?
You may want to set a timeout e.g.

Code: Select all

with serial.Serial('/dev/ttyS1', 19200, timeout=1) as ser:

Posts: 4244
Joined: Fri Jun 08, 2012 6:03 pm

Re: Arduino - Raspberry pi connection

Fri Jun 11, 2021 8:47 am

Just to keep things clear, the Arduino doesn't write to /dev/ttyACM0, that is done by the serial port driver in Linux. As your Arduino outputs data over the USB-Serial connection, data is read by the serial port driver and is placed in the buffer which you can read by accessing /dev/ttyACM0 (or another serial port device).

Only one application can use a serial port at any one moment. So if you have python code running, you probably won't be able to use cat too.

Does pressing the reset button on your Arduino fix your issue? Or do you have to re-upload the software?

It is possible to reset an Arduino using the DTR pin of the USB Serial port (or certainly used to be on the older Arduinos). This might allow you to reset the Arduino from the Pi, without having to physically press a button.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

Return to “Other projects”