How to connect a Raspberry Pi to a serial USB port with Python from the terminal

Use these open source tools and simple commands to easily interface with other devices from your Raspberry Pi.
105 readers like this
105 readers like this
Coding the Raspberry Pi in a web emulator

Opensource.com

Raspberry Pi can be used to interface with the real world from its GPIO, for example, by controlling a stepper motor. You can also use Raspberry Pi to dialog with some devices—like Arduino—by using a serial USB port.

In this guide, I'll show you how to connect your Raspberry Pi to a serial USB port and read its values with Python from the terminal (without a desktop environment). For this purpose, we'll use Pyserial and its terminal tool.

What we need

For this project, I'm going to use a Raspberry Pi Zero W and an Arduino Uno R3. Steps should also work with newer Raspberry Pi boards.

  • Raspberry Pi Zero W (including proper power supply or a smartphone micro USB charger with at least a 3A) or newer board
  • micro SD card (at least 16 GB, at least class 10)
  • Arduino Uno R3 board or compatible board (you can also consider the Elegoo starter kit).

Step-by-step guide

We'll start by installing Raspbian Buster Lite in our Raspberry Pi Zero W. Then, we'll use Python to install proper libraries to read data from Arduino Uno R3.

Raspberry Pi environment preparation

Before you start, please refer to Install Raspbian Buster Lite in your Raspberry Pi article to install Raspberry PI OS.

Furthermore, Rasbpian Buster Lite comes with Python pre-installed.

Now we must configure Arduino Uno with its first sketch. To accomplish this, you have two options:

Install Pyserial

Before installing Pyserial, we need to get pip:

sudo apt install python-pip

Now we can go on to install Pyserial:

python -m pip install pyserial

Test installation and read console

To read our console, we need to connect the Raspberry Pi USB port to Arduino one. For testing purposes, I'll show you results from my weather system:

Raspberry Pi Arduino weather station

Arduino must be configured to send data via COM port with a Serial.print command inside its running sketch. Remember to append carriage return (text "\n") to each data sample in your Arduino sketch, in order to have different rows for each reading.

In this configuration, you can simply use the Pyserial miniterm tool to list available ports and their output. Use command python -m serial.tools.miniterm, then enter device port and receive port data on screen. Use CTRL+] to close connection:

pi@raspberrypi:~ $ python -m serial.tools.miniterm
--- Available ports:
---  1: /dev/ttyACM0         'ttyACM0'
---  2: /dev/ttyAMA0         'ttyAMA0'
--- Enter port index or full name: /dev/ttyACM0
--- Miniterm on /dev/ttyACM0  9600,8,N,1 ---
--- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
20.0;61.0
20.0;61.0
20.0;61.0
--- exit ---

You can also go directly on the port reading, if you already know its name, just by appending the port name. In my example, the correct port is /dev/ttyACM0 (please refer Connecting Raspberry Pi to Arduino only via terminal from Raspbian Buster Lite to know how to discover your port), so direct command will be:

pi@raspberrypi:~ $ python -m serial.tools.miniterm /dev/ttyACM0
--- Miniterm on /dev/ttyACM0  9600,8,N,1 ---
--- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
20.0;61.0
20.0;61.0
20.0;61.0
--- exit ---

Using Pyserial inside Python programs

Also, this operation is really simple. You need to import a serial library and call port opening. Create a file named "test.py":

nano test.py

and include the following code:

import serial
ser=serial.Serial(’/dev/ttyACM0’,9600)
readedText = ser.readline()
print(readedText)
ser.close()

Execute:

python test.py

Enjoy!

Article originally posted at peppe8o.com. Reposted with the permission.

What to read next
Lucky husband, open source passionate. Curious by nature. Proud of my roots, happy to mix and learn from different roots

5 Comments

Thanks , I needed this , cuz I also wanted to connect Raspberry Pi to Serial USB

3A to power the pi zero is kinda overkill. 1 amp is sufficient but 2 amps is preferable. ?

Rasperry PI Zero W recommended PSU is at least 1,2 A.Typical bare board current consumption goes down to 150mA (Ref. https://peppe8o.com/comparing-main-features-of-latest-rpi-models/). But for some projects (expecially if you want to power Arduino and external devices from Raspberry PI USB port) you may need more current capacity than the one RPI consumes alone. For this reason a bit higher power supply may be better. However also 2A is a good choise.
Thank you for your feedback!

In reply to by digidev (not verified)

well explained keep up the good work also check mine on

raspberry pinout
and
Serial communication

Awesome, I will benchmark with a post that I am creating but I am using Java instead of Python =P Let see the results!! Thanks for sharing

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.