Python Serial Port Communication Between PC and Arduino Using PySerial Library
by xanthium-enterprises in Circuits > Arduino
25179 Views, 1 Favorites, 0 Comments
Python Serial Port Communication Between PC and Arduino Using PySerial Library
In this tutorial,
- we will learn How to communicate with an Arduino UNO from a PC using Python and pySerial Library.
- The Tutorial will talk about both the Arduino side code and Python Side code.
- All Code is written in Python 3.x.x
- Original Article on Python Serial Port Programming using pySerial Library can be found here.
- Source codes can be downloaded from our Website
Supplies
Things needed/Software to be Downloaded
- Python Interpreter (You can Download CPython from here )
- pySerial Library
- Arduino
- PC
- USB cable
Download and Install Python and PySerial Library
Install Python
In Windows, Python is not installed by default, you can find python binary installers from either python.org or from ActiveState software.
Install pySerial
You can install PySerial Package using the pip installer.
PIP is included with the standard Python interpreter (CPython )
On your Windows Command prompt and type the following
C:\ python -m pip install pyserial
Writing Data to Serial Port Using Python and PySerial
Here we will send a character 'A' to Arduino from PC using a Python Script ,
On receiving the character A ,Arduino will blink the LED connected to PIN12 of Arduino UNO
Open a text editor and type the following lines of code into it .Save the file with a ” .py” extension.
The Below code writes character 'A' to the serial port.
# Python code transmits a byte to Arduino /Microcontroller import serial import time SerialObj = serial.Serial('COM24') # COMxx format on Windows # ttyUSBx format on Linux SerialObj.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 = 1 time.sleep(3) SerialObj.write(b'A') #transmit 'A' (8bit) to micro/Arduino SerialObj.close() # Close the port
Arduino Code
The partial code is shown below.
if (Serial.available()) { RxedByte = Serial.read(); switch(RxedByte) { case 'A': digitalWrite(12,HIGH); delay(1000); digitalWrite(12,LOW); break; case 'B': //your code break; default: break; }//end of switch() }//endof if
Reading Data From Serial Port Using Python and Pyserial
Here,
we will use the pyserial Library to read a string send from the Arduino and display it on the console.
Arduino transmits the the string "Hello from Arduino" and PC receives it
PySerial provides two functions to read data from the serialport
- 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() Arduino Code void loop() { char TextToSend[] = " Hello From Arduino Uno"; Serial.println(TextToSend); // sends a \n with text delay(1000); }
The Arduino sends a string which is received by the PC side Python code and displayed on terminal.
We also have a full detailed step by step tutorial on our Youtube Channel