Skip to content

Instantly share code, notes, and snippets.

@eddieliberato
Created June 10, 2016 18:55
Show Gist options
  • Save eddieliberato/7abfedb4990b2c5c58d97cd701a1d094 to your computer and use it in GitHub Desktop.
Save eddieliberato/7abfedb4990b2c5c58d97cd701a1d094 to your computer and use it in GitHub Desktop.
Live Data Plot with Arduino and Python
import serial
import matplotlib.pyplot as plt
from time import sleep
from drawnow import *
arduino = serial.Serial('/dev/ttyUSB0')
with arduino:
arduino.setDTR(False)
sleep(1)
arduino.flushInput()
arduino.setDTR(True)
arduino = serial.Serial('/dev/ttyUSB0', baudrate=57600)
plt.ion ()
count = 0
AccYarray = []
AccZarray = []
tempoarray = []
def fig ():
plt.plot (AccYarray)
plt.plot (AccZarray)
plt.title ('AcelY & AcelZ')
plt.grid (True)
plt.plot (AccYarray, 'ro-' , label = 'AcelY')
plt.plot (AccZarray, 'bo-' , label = 'AcelZ')
plt.legend (loc= 'upper left')
with arduino:
while True:
dadosstring = arduino.readline ()
dadosarray = dadosstring.split (",")
AccY = float (dadosarray [3])
AccYarray.append (AccY)
AccZ = float (dadosarray [4])
AccZarray.append (AccZ)
plt.pause (0.00001)
count = count + 1
if (count > 50):
AccYarray.pop (0)
AccZarray.pop (0)
tempo = float (dadosarray [0])
tempoarray.append (tempo)
drawnow (fig)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment