Use Arduino in the Jupiter Notebook for real-time representation of graphs

Asked 2 years ago, Updated 2 years ago, 95 views

I would like to communicate with Arduino using Python, but the graph is not displayed in real time even if I do the following:Specifically, it only appears after execution.
I'd like the image to be displayed like an animation, but what is the cause?
I use Windows.Please let me know if there is anything.

%matplotlib notebook
import numpy as np
import matplotlib.pyplot asplt
import serial
from serial.tools import list_ports
import time

ser=serial.Serial("COM6",9600)#COM port (Arduino connection) 
xlim = [0,100]
X,Y = [ ], [ ]
figure=plt.figure()
ax=figure.add_subplot(1,1,1)
start = time.time()

while True:
    plt.cla()# Clear previous graph
    bolt=ser.readline().rstrip()#Replace the information (string) received in the serial communication until a new line code arrives.obtain by byte type
    volt=volt.decode()
    bolt=float(volt)
    Y.append(volt)
    X.append(len(Y))
    
    iflen(X)>100:
        xlim[0]+=1
        xlim[1]+=1
    ax.plot(X,Y)
    plt.title("voltage(cds)")
    plt.ylim(0,5)
    plt.xlim(xlim[0], xlim[1])
    
    if time.time()-start>20:
            ser.close()
            break

python matplotlib jupyter-notebook arduino

2022-09-30 19:56

1 Answers

I answered the following questions first, but I will also be able to use real-time graphing by adding plt.pause(interval).

However, the impact of the Jupiter Notebook environment is unknown, so please try the original Python first.
Below are excerpts from the postscripts.

plt.ylim(0,5)
    plt.xlim(xlim[0], xlim[1])
    plt.pause(0.1)#### Add this line
    
    if time.time()-start>20:


2022-09-30 19:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.