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.






May 31st, 2010 at 8:47 pm
how to export serial data to Excel file?, i need that to analysis serial output. can you help me, because i’m newbies in python language. thank’s
June 29th, 2010 at 1:51 am
If you want to put data into excel, just open the serial monitor, and run your sketch, then when its done copy and pase the data from serial monitor to excel?
July 1st, 2010 at 8:41 pm
Thanks for posting this. I had a few issues. In Ubuntu 9.10 I got the following error when trying to run
>> ImportError: Matplotlib backend_wx and backend_wxagg require wxPython >=2.8
I fixed it by adding the following two lines to wx_mpl_dynamic_graph.py
import wxversion
wxversion.select(’2.8′)
right before import wx.
Second, when testing with the sketch above and a pot attached to the board I notice the signal was wrapping around, changing the val variable to int type instead of byte and removing the DEC from the write command fixed the problem. Thanks again!