Connecting to Microcontroller With Pyserial Library - Python

by drselim in Circuits > Microcontrollers

4697 Views, 13 Favorites, 0 Comments

Connecting to Microcontroller With Pyserial Library - Python

Python Serial Library & Connecting to Microcontroller

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.

Circuit Setup

msp430_dht11_computer.png

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:

https://www.instructables.com/MSP430-DHT11-Tempera...

https://www.instructables.com/MSP430-Serial-Monito...

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.

Installing Pyserial Library

install_pyserial.png

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

python -m pip install pyserial


Python Code Using Serial Library

code1.JPG
temp_humid.JPG
cmd_prompt_temp_humid.png

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.

Code:

import serial

from time import localtime, strftime

ser = serial.Serial()

ser.port = 'COM3'

ser.baudrate = 19200

ser.open()

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

while(True):

line = ser.readline()

print(line)

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

temp_file.write(line.decode())

Plotting Temperature and Humidity Change

humidity.JPG
temperature.JPG

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)

Code:

import numpy as np

import sys

import matplotlib.pyplot as plt

np.set_printoptions(threshold=sys.maxsize)

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]

plt.plot(x,y)

plt.show()

If you like the content please subscribe to my YouTube channel for similar tutorials and projects.

https://www.youtube.com/c/drselim

Thank you for your time..