Skip to content

Instantly share code, notes, and snippets.

@bbevan
Last active January 4, 2020 02:32
Show Gist options
  • Save bbevan/ee629f5e0967d62f8a3b55cc9edbc378 to your computer and use it in GitHub Desktop.
Save bbevan/ee629f5e0967d62f8a3b55cc9edbc378 to your computer and use it in GitHub Desktop.
Accessing the Arduino via Ubuntu

Arduino on Ubuntu

When I moved over to Ubuntu a few months ago, I was in the middle of an Arduino project involving the LCD display of strings via Python's serial module. On my windows device, the board was accessed via Window's Comport 3 (COM3), however Linux does not use that nomenclature as the serial device software is different.

According to Building Embedded Linux Systems: Concepts, Techniques, Tricks, and Traps by Yaghmour et al., serial devices are "uniformly accessed as terminal devices". Those devices are found under /dev/ttyS0 all the way through /dev/ttyS191 (pg. 73).

The problem is that my Python script bugged out an error upon executing the following code.

import serial
ser=serial.Serial("/dev/ttyS0", 9600) #9600 baud connection to the board via the serial port on /dev/ttyS0
ser.write("My string.")

The error stated that I had no user rights to access /dev/ttyS0.

The previous edition of Ubuntu kept serial device access under /dev/serial/by-id/. My code then looked something like this.

import serial
ser=serial.Serial("/dev/serial/by-id/arduinosomethingsomethingusb", 9600) #9600 baud connection to the board via the serial port on /dev/serial/by-id/arduinosomethingsomethingusb
ser.write("My string.")

The exact code I used for my bootleg Arduino Nano looks like this under a sudo python shell.

>>> import serial
>>> ser=serial.Serial("/dev/serial/by-id/usb-1a86_USB2.0-Serial-if00-port0", 9600)
>>> ser.write("test")
4

Indicating that Python had indeed accessed the board under root conditions.

Access

Some users report problems accessing serial devices on Ubuntu 18. My experience on Ubuntu 19.10 was different once I gave programs root permission through sudo. Perhaps the issue is fixed in the new release. While searching the release notes, I couldn't find any mention of serial devices, although there is new support for Raspberry Pi, which may have repaired the issue for the Arduino.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment