Linux Serial Port Communication With Arduino Using Python and Pyserial

by xanthium-enterprises in Circuits > Arduino

7626 Views, 5 Favorites, 0 Comments

Linux Serial Port Communication With Arduino Using Python and Pyserial

Serial Port Communication between Linux (ubuntu) and Arduino using Python and PySerial Module Pt-2

Learn how to setup a serial port communication link between a Linux PC/Laptop and a AVR/PIC/MSP430 Microcontroller or Arduino using PySerial and Python.


Here we will Learn the following

  • Setting up permissions to read and write to a Linux serial port,
  • How to solve the /dev/ttyUSB0 or /dev/ttyACM0 access denied error.
  • How to add a user to tty and dialout groups for serial communication on Linux
  • How to solve dmesg :read kernel buffer failed :Operation not permitted error


Supplies

python-serial-programming-tutorial-banner.jpg
  1. Python Interpretor
  2. Arduino
  3. Ubuntu/Linux system

How to Install Pyserial on Ubuntu Linux for Serial Port Programming

install-pyserial-pip-ubuntu.png

How To Install PIP Package Manager On Linux

Check whether PIP is installed on the System,

if not, install with the following command, You can also check the link above to the video.

sudo apt install python3-pip

After installing PIP ,use it to install the latest version of Pyserial


How to install PySerial on Linux (Ubuntu 22-04-LTS) using PIP Package Manager

use the below command or check the above video link ,if you get into trouble while installing Pyserial

python3 -m pip install pyserial 



How to Identify the Serial Port Name / Number of Your Device in Linux (ubuntu)

Id-USB-to-Serial-linux.jpeg

In Linux there is no concept of COM number, instead the

  1. hardware serial ports are numbered as ttyS0,ttyS1 etc ,
  2. USB to Serial Converters as ttyUSB0,ttyUSB1 etc.
  3. Arduino's as ttyACM0


Connect your Arduino to the USB port and issue a


 sudo dmesg | tail 


command at the terminal.


You have to use sudo in front of dmesg otherwise you will get a dmesg :read kernel buffer failed :Operation not permitted error.

So in Linux change the second line to

SerialObj = serial.Serial('/dev/ttyACM0')

 for Arduino

More details on serial port programming on Linux can be found here

Making Python Serial Port Code Executable

Before running the python code ,you have to make them executable by using the chmod command

chmod +x yourpythonfile.py

Adding the User to Tty and Dialout Groups for Serial Port Access in Linux (Ubuntu)

ubuntu-linux-serial-permissions.jpg

On Linux to access the serial port the user must be part of 2 groups

  1. tty
  2. dialout

You should add yourself to these two groups using the usermod command.

Please note that you should have permission to run the sudo command (part of the Sudo group in Ubuntu) or part of the wheel group in Centos/RHEL/Rocky Linux.

 


Adding a user to tty and dialout groups for serial port access.

sudo usermod -a -G tty [username]
sudo usermod -a -G dialout [username]

replace [username]  with your username Eg rahul or molly.

The -a makes sure that you are appending the user to the groups .if -a  is missing you will get unsubscribed from all other groups except tty and dialout.

After the commands are run,

Logoff from your account and then log back in so that changes are saved.


Running Python Serial Port Communication Code on Linux

Serial Port Communication between Linux (ubuntu) and Arduino using Python and PySerial Module Pt-2

After this you can run the code files on the linux system