Reading a serial USB port from Rasperry PI OS Lite with python (from terminal)

2.3
(3)

Last Updated on 2nd September 2023 by peppe8o

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 terminal (without Desktop environment). For this purpose, we’ll use Pyserial and its terminal tool.

Raspberry PI can be used to interface real world from its GPIO as, for example, by Controlling a stepper motor. Furthermore, you can also use Raspberry PI to dialog with some devices (like Arduino) by using serial USB port.

What We Need

For this post I’m going to use a Raspberry PI Zero W and an Arduino Uno R3. Steps should work also with newer Raspberry PI boards.

Raspberry PI Zero W unpopulated

As usual, I suggest adding from now to your favourite e-commerce shopping cart all the needed hardware, so that at the end you will be able to evaluate overall costs and decide if continue with the project or remove them from the shopping cart. So, hardware will be only:

Check hardware prices with following links:

Amazon raspberry pi boards box
Amazon Raspberry PI 3 Model A+ box
Amazon raspberry pi Zero W box
amazon raspberry pi pico box
Amazon Micro SD box
Amazon Raspberry PI Power Supply box
Amazon Resistors box
Amazon Breadboard box
Amazon Dupont Wiring box

Step-by-Step Guide

We’ll start from installing Raspberry PI OS 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 all, please refer to Install Raspberry PI OS Lite article to install Raspberry PI OS. Furthermore, Raspberry PI OS Lite comes with Python pre-installed.

Now we must configure Arduino Uno with its first sketch. For this purpose, you have 2 option:

Install Pyserial

Before installing Pyserial, we need to get pip:

sudo apt install python3-pip

Now we can go on to install Pyserial:

pip3 install pyserial

Test Installation And Read Console

To read our console, we need now to connect Raspberry PI USB port to Arduino one. For testing purposes I’ll show you results from my weather system (procedure will be soon available on peppe8o.com, stay tuned!):

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 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:~ $ python3 -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 port reading, if you already know its name, just by appending port name. In my example correct port is /dev/ttyACM0 (please refer Connecting Raspberry PI to Arduino only via terminal to know how to discover your port), so direct command will be:

pi@raspberrypi:~ $ python3 -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 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:

python3 test.py

Enjoy!

How useful was this post?

Click on a star to rate it anonymously!

Average rating 2.3 / 5. Vote count: 3

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?