Python code to read from the serial port of a Raspberry Pi

Share on facebook
Share on twitter
Share on linkedin
Share on pinterest
Share on email
Share on whatsapp

To develop your projects with Arduino or ESP8266 on Raspberry Pi basis, one may need to access the serial port for the update. This tutorial should work on all distributions of Linux and Raspbian in particular. I wrote this tutorial because I needed to access messages from my Gateway MySensors ESP8266 plugged into the USB port of my Raspberry Pi 3.

Install pyserial on Raspbian

Depending on the version of python, there are several commands to install the package pyserial. To know the version of python run

 python -version

If the pip command is available (run pip to see if return or not command not found bash) run (replace with pip2 and pip3 based on the python version used).

 sudo install pyserial pip3

Otherwise through apt-get

 sudo apt-get install python-serial python3-serial

Read on the serial port

Free serial port

Before going any further, check that the console is not listening on the serial port. To do this run

 sudo raspi-config

Then follow the images

raspi-config port serie




Find the ttyUSB port that corresponds to your device

There are several ways to determine the USB port to which the device is connected. For options go here. The fastest is to connect the unit to the USB port then to immediately run the command dmesg -s 1024.

You’ll get directly the tty port on which it is connected.


Python code to read the serial port

Open nano

sudo nano

And copy the following code

#!/usr/bin/env python
import time
import serial

ser = serial.Serial(
 baudrate = 9600,

while 1:
 print x

Register with the combination Ctrl + X then Y by giving a name to the file (for example

Now, launch the program to read messages sent on the serial port from your device (for example an Arduino).

sudo python

raspberry lire port serie code python esp8266 mysensors gateway wifi

Reading the messages of a Gateway MySensors on the serial port of a Raspberry Pi.

To exit the program, make the combinations of keys Ctrl + C.
Click to rate this post!
[Total: 0 Average: 0]

Are you having a problem with this topic?

Maybe someone has already found the solution, visit the forum before asking your question

Ask your question

Share on facebook
Share on twitter
Share on linkedin
Share on pinterest
Share on email
Share on whatsapp

Did you like this project ? Don't miss any more projects by subscribing to our weekly newsletter!

1 Comment
  1. Hi!
    This was really helpful (I think, if I get it to work in the end 🙂 ), but I don’t get the same output as you in order to identify the tty port!

    After disconnecting the USB device (I just tried with the mouse, since I don’t have access yet to the controller I am going to have serial communication with), and it only says “USB disconnect, device number 7″… (I attached a picture of it)

    So how to know what kind of port specification I should write in “port=’/dev/ttyUSB0′,”???

    Leave a Reply

    DIY Projects
    %d bloggers like this: