Python Serial Communication

Updated: Apr 16

Learn how to use the open source and free PySerial library to send and receive data from your Python program via Serial communication.

[Disclaimer: As an Amazon Associate I earn from qualifying purchases from paid links in this article]

Introduction

Python is a vey powerful language because of its ease of use, wide adoption, support community, and increased use in data science and scientific endeavors. So it is a tremendous advantage to be able to unleash the power of Python on our physical devices. In order to do this we need way of communication from our Python programs to our microcontrollers -- so we use Serial Communication. For a walkthrough on how to communicate via Serial Line from the language Processing, see Serial Communication with Processing Tutorial.


pySerial Library

This tutorial will show examples of how to send and receive data from Python over the serial port. we will be sending actual string literals, as well as variables (more useful).


Parts List

Arduino UNO (1)

Anaconda Installation with Python 3.8

LED (1)

330 Ohm Resistor (1)

Jumper Cables (assorted)


The Project

We are going to run Python on our computer and send numerical data to our Arduino to control the brightness of an LED.


Physical Interface


The Arduino circuit is shown to the left .



This circuit is very simple because the purpose of this tutorial is just to demonstrate basic Serial communication with the use of an LED.







Below is the fritzing breadboard diagram. Overall it's pretty self-explanatory, except that the single resistor used was 330 ohms.

This is the code to execute in the Arduino IDE:

const int analogOutPin = 9; 
int ledBrightness = 0;

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
}

void loop() {
  // Update brightness of LED   
  analogWrite(analogOutPin, ledBrightness); 
  delay(2);

 // Read data from Serial line
  if (Serial.available() > 0) {
    ledBrightness = Serial.parseInt();      
  }      
}

Python Interface


1. First we need to install and import the pySerial library:

conda install pyserial
import serial

2. Initialize the port for serial communication:

ComPort = serial.Serial('COM4') # open COM4
ComPort.baudrate = 9600 # set Baud rate to 9600

The value of the port may be different for you, it must be changed to the name of the port you are using in your Arduino sketch (i.e. 'COM3', 'COM5', etc.)


Here is how to figure out the port for you Arduino:

AFTER the Arduino is already connected to you computer, go to the Tools menu and see which port is specified. In the image to the left we can see "COM4 (Arduino Uno)", which means we need to communicate with "COM4".





3. Here is an example of sending a string literal via Serial line:

ComPort.write(b'100')

In the above we are specifying that the value is being sent as a byte, as denoted by the prefix b.


After executing this line of code above along with your Arduino circuit hooked up, you should now see that the LED has turned on, and it has been set to a brightness of 100 out of 255.


Before After




















4. Here is an an example of sending a value by variable to the Serial monitor. This will cause the LED connected to the Arduino to be set to a random brightness, which is stored in the variable s and then used in ComPort.write(s).

import random
s = str(random.randrange(50,100)).encode()
ComPort.write(s)

In the above code we are doing the following:

  • Encode the random value as a String, s.

  • Write the value of s via Serial line.

  • After executing these lines of code above along with your Arduino circuit hooked up, you should see a different brightness each time you run the command.

Below is a screenshot of the complete Jupyter notebook with all of the commands in order to follow through with the examples: