Hi,
I have trouble using Python to graph Arduino's output data from sensor. I was able to display the data in Arduino's Serial Monitor. However, when I try to use python to graph/visualize those data, I ran into few errors.
Arduino Sketch's Output Successfully:
Pycharm Python Code below:
import serial
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
ser = serial.Serial('COM7', 9600)
fig, (ax1, ax2, ax3) = plt.subplots(3, 1)
x_data = []
y_gyro_data = []
x_gyro_data = []
z_gyro_data = []
def update(frame):
line = ser.readline().decode('utf-8').strip()
if "X Gyro:" in line:
x_gyro = float(line.split(': ')[1])
x_data.append(frame)
x_gyro_data.append(x_gyro)
elif "Y Gyro:" in line:
y_gyro = float(line.split(': ')[1])
y_gyro_data.append(y_gyro)
elif "Z Gyro:" in line:
z_gyro = float(line.split(': ')[1])
z_gyro_data.append(z_gyro)
if len(x_data) > 50:
del x_data[0]
del x_gyro_data[0]
del y_gyro_data[0]
del z_gyro_data[0]
ax1.clear()
ax2.clear()
ax3.clear()
ax1.plot(x_data, x_gyro_data, label='X Gyro')
ax2.plot(x_data, y_gyro_data, label='Y Gyro')
ax3.plot(x_data, z_gyro_data, label='Z Gyro')
ax1.legend(loc="upper right")
ax2.legend(loc="upper right")
ax3.legend(loc="upper right")
ani = FuncAnimation(plt.gcf(), update, interval=200) # Update every 200ms
plt.tight_layout()
plt.show()
Traceback (most recent call last):
Error:
line 4, in
arduino = serial.Serial(port='COM7', baudrate=9600)
line 244, in init
self.open()
line 64, in open
raise SerialException("could not open port {!r}: {!r}".format(self.portstr, ctypes.WinError()))
serial.serialutil.SerialException: could not open port 'COM7': PermissionError(13, 'Access is denied.', None, 5)