Python Serial Port Communication Between PC and Arduino Using PySerial Library

Introduction: Python Serial Port Communication Between PC and Arduino Using PySerial Library

About: https://www.youtube.com/c/XanthiumIndustries/videos

In this tutorial,

Supplies

Things needed/Software to be Downloaded

  1. Python Interpreter (You can Download CPython from here )
  2. pySerial Library
  3. Arduino
  4. PC 
  5. USB cable

Step 1: 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



Step 2: 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 


Full code can be downloaded from our Website

Step 3: 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

  1. readline()
  2. 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


Be the First to Share

    Recommendations

    • On a Budget Challenge

      On a Budget Challenge
    • Fandom Contest

      Fandom Contest
    • Fabric Challenge

      Fabric Challenge

    Comments