Python Serial Port Programming Tutorial for Beginners using PySerial Module
Tutorial on connecting an Arduino with a Windows PC using serial port (VCP). and communicating with it using Python and PySerial library.
pySerial Module
pySerial is an easy to use opensource elibrary that simplifies the access to the SerialPort using Python.
The Library is cross platform and can be used on Linux, Windows, Mac and BSD
you can use the PIP installer to install pySerial on your Python installation.
or by giving the below command
. python -m pip install pyserial
PC Arduino Serial Communication Block Diagram
PC and Arduino are connected as shown above using a single USB cable.
A more detailed explanation of how to connect the Arduino/Atmega328P with Pc can be found here
Writing data to Serial Port using Python and pySerial
The below code transmits a byte A to the Arduino and Arduino and Arduino blinks a LED.
# Python code transmits a byte to Arduino /Microcontroller
import serial
import time
SerialObj = serial.Serial(‘COM24’) # COMxx format on Windows
# ttyUSBx format on LinuxSerialObj.baudrate = 9600 # set Baud rate to 9600
SerialObj.bytesize = 8 # Number of data bits = 8
SerialObj.parity =’N’ # No parity
SerialObj.stopbits = 1 # Number of Stop bits = 1time.sleep(3)
SerialObj.write(b’A’) #transmit ‘A’ (8bit) to micro/Arduino
SerialObj.close() # Close the port
The first line import serial imports the pySerial module so that your programcan use it.
ComPort = serial.Serial(‘COM24’) opens the serial port named COM24.
Serial communication occurs in bytes (8 bits) while Python3+ handles strings in unicode format which may consume upto 4 bytes.
In the above Example,
we are sending a byte ‘A’. Here A is defined as a byte by using b prefix. You can also use the bytearray() function.
SerialObj.write(b’A’) #transmit ‘A’ (8bit) to micro/Arduino
Reading Data from Serial port using Python and Pyserial
PySerial provides two functions to read data from the serial port
- readline()
- read()
readline() reads till it encounters a newline character ‘\n’ and returns the bytes it has read.
If \n character is not encountered it will wait forever or until the read timeout expires.
Partial Python Code (PC side)
ReceivedString = SerialObj.readline()
print(ReceivedString)
SerialObj.close()
The Arduino sends a string which is received by the PC side Python code and displayed on terminal.
Arduino Side Code
- All the Source codes used on the Arduino side for transmitting and receiving from Python Serial Port can be found here
- Detailed explanation of Arduino side Code for Python Serial Port Programming can be found here
USB to Serial Converter
In the above tutorial we learned how to connect an Arduino board with a PC and communicate with it (Receive/Transmit data ) using a standard USB cable.
What if instead of an Arduino we just want to connect a bare microcontroller like ATmega328P,ATmega16,MSP430 or PIC18F4550 to your PC.
When you are using a microcontroller which do not have any built in USB controller like ATmega16 or MSP430G2553 ,you have to use a USB to Serial Converter chip like FT232RL or buy a standard USB to serial converter board like USB2SERIAL
to convert serial signals to the USB ones.
Original Article
- The Original Article Cross Platform serial port programming using Python (PySerial) and Arduino can be found here
- reproduced with permission