Connecting to Microcontroller With Pyserial Library - Python

Introduction: Connecting to Microcontroller With Pyserial Library - Python

About: Hi! I'm a Ph.D Electronics & Telecommunications engineer. I'm interested in microcontrollers, programming (C, Python), retro computers, embedded systems...

Hi, In this instructable, we'll see how to use the Pyserial library in Python in order to make a serial connection via a COM Port (in our example COM3) and receive the temperature and humidity values from an MSP430 Microcontroller which is programmed to obtain data from a DHT11 sensor.

In addition to this, we'll see how to write these serial output to a text file and then we'll use matplotlib and numpy libraries to process the data from the text file and plot temperature & humidity change by time.

Step 1: Circuit Setup

In order to see how to obtain the value from the MSP430 microcontroller and send them to the serial port you can use the following instructables:

You can also use other microcontroller boards such as Arduino, in that case the only thing you'll need to pay attention is the COM port that you're using and the speed (baudrate) of the connection.

Step 2: Installing Pyserial Library

We'll use the following command in the command prompt (Windows) for installing Pyserial Library:

python -m pip install pyserial

Step 3: Python Code Using Serial Library

In this step, we'll write the code that opens a serial connection, prints the received messages to the terminal (command prompt) and at the same time to a text file that we specify. You can change the port and baudrate settings with (ser.port and ser.baudrate)

While writing to the text file, the code also puts timestamps in the beginning of the line in order to log when the measurement was received.


import serial

from time import localtime, strftime

ser = serial.Serial()

ser.port = 'COM3'

ser.baudrate = 19200

temp_file = open('temp_humid.txt', 'a', encoding = 'utf-8')


line = ser.readline()


temp_file.write(strftime("%d %b %Y %H%M%S ", localtime()))


Step 4: Plotting Temperature and Humidity Change

In this final step, using the log file that we created in the previous step, we'll plot the temperature and humidity change over time. In order to plot temperature, you need to set the line y = matrix_data[:,6] . In order to plot humidity, you need to set the line y = matrix_data[:,-2] (6 and -2 correspond to the colums representing temperature and humidity values respectively, you can check for the text file, first column having the indice 0)


import numpy as np

import sys

import matplotlib.pyplot as plt


read_file=input('Please enter file name to read: ')

test2_file=open(read_file, 'r', encoding = 'utf-8')

matrix_data = np.genfromtxt(test2_file)

x = matrix_data[:,3]

y = matrix_data[:,-2]


Thank you for your time..

Be the First to Share


    • Furniture Contest

      Furniture Contest
    • Make it Real Student Design Challenge #3

      Make it Real Student Design Challenge #3
    • Home Decor Challenge

      Home Decor Challenge