Realtime Plot of Arduino Serial Data Using Python
So I got an Arduino a few weeks ago, and just made my second little project:

It simply has a piezo element connected to an analog input pin. The Arduino polls the value every 100ms and prints it to the serial port. Here is the sketch:
/* Knock Poller
* ----------------
* We listen to an analog pin, sample the
* signal and write it to the serial port.
*/
int knockSensor = 5;
byte val = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
val = analogRead(knockSensor);
Serial.println(val,DEC);
delay(100); // we have to make a delay to avoid overloading the serial port
}
The problem was that I found it was hard to tell what was happening just by reading the values printed to the serial monitor in the Arduino IDE’s serial monitor.
So I researched realtime plots/graphs/charts in Python, and sadly didn’t find too much. Finally I came across
Eli Bendersky’s live graph demo using wxPython and matplotlib which was exactly what I was looking for.
I repurposed it to listen to the Arduino and set it listening:
You can see how my pushing on the knock sensor makes the voltage go up and down.
You can get the code here. (I only tested it on Windows XP.)
It should work for any Arduino sketch that sends numeric data to the serial port at least a few times per second. It’s smart enough to ignore non-numeric lines.
I got a lot of help from the folks at Stack Overflow on how to use pySerial.
Update:
It turns out the proper search term would have been “arduino oscilloscope” which does bring back some useful projects:
http://code.google.com/p/arduinoscope/
http://accrochages.drone.ws/en/node/90
But I still like mine since you don’t have to compile anything to run it, and it doesn’t dictate what Arduino sketch you use.





