Skip to content
Permalink
Browse files

added some simple python scripts for interacting with an arduino

  • Loading branch information...
Peter Sand
Peter Sand committed Jul 17, 2014
1 parent c2f07bb commit 1573e4d8f7bf97a7b73deaf6b7e94a0adbb05da9
Showing with 79 additions and 0 deletions.
  1. +34 −0 python/simpleLogger.py
  2. +45 −0 python/simplePlotter.py
@@ -0,0 +1,34 @@
# This is a simple sample script for logging data from an Arduino.
# It assumes that your Arduino sends comma-separated sensor values.
# (It will blindly log all serial messages, but you'll only have a valid
# .csv file if the messages are comma-separated or a single number per line.)
import serial # http://pyserial.sourceforge.net/


# set this to your serial port (e.g. COM3 on windows or /tty.usb/... on mac/linux)
SERIAL_PORT = "COM3"


# log data from serial port into a text file
def logSensors():

# open the serial port
serialConnection = serial.Serial( SERIAL_PORT, timeout=2.0 )

# open the output file (for appending)
outputFile = open( "sensorLog.csv", "a" )

# loop until user breaks
while True:
data = serialConnection.readline().strip()
if data:
print data
outputFile.write( data + "\n" )


# if run as top-level script
if __name__ == "__main__":
try:
logSensors()
except KeyboardInterrupt:
pass
@@ -0,0 +1,45 @@
# This is a simple sample script for plotting data from an Arduino.
# It assumes that your Arduino sends comma-separated sensor values.
import datetime
import serial # http://pyserial.sourceforge.net/
from matplotlib import pyplot


# set this to your serial port (e.g. COM3 on windows or /tty.usb/... on other mac/linux)
SERIAL_PORT = "COM3"


# maximum data points to show
MAX_LEN = 100


# plot data on a window
def runPlotting():

# open the serial port
serialConnection = serial.Serial( SERIAL_PORT, timeout=2.0 )

# prep plotting
pyplot.ion()
y = [0] * MAX_LEN
plot, = pyplot.plot( y )

# do until user interrupts
while True:
data = serialConnection.readline().strip()
if data:
print data
values = [float( v ) for v in data.split( "," )]
y = y[ 1: ]
y.append( values[ 0 ] )
plot.set_ydata( y )
pyplot.ylim( [min( y ), max( y )] )
pyplot.draw()


# if run as top-level script
if __name__ == "__main__":
try:
runPlotting()
except KeyboardInterrupt:
pass

0 comments on commit 1573e4d

Please sign in to comment.
You can’t perform that action at this time.