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!
March 14th, 2011 at 1:31 am
Hey. IS Any Explanation About ziare
March 21st, 2011 at 10:09 pm
I recently used this to visualize some data coming off a Bluetooth DAC I have. Did you manage to be ok with the low update rate of this graph in comparison to the realtime graph using random numbers?
I am examining some of the buffering of the serial device, and I will post a link to it later this week if I figure out how to handle it.
March 30th, 2011 at 12:12 pm
Hi.
I’m trying to read data from a serial port using this code, and haven’t modified it further than changing the port information. The only output I get is 500 “100″’s in a row, and can’t figure out where it comes from. I’m a beginner at this stuff, so I’d be glad for some help. How can I solve this?
With best regards,
Najk
April 3rd, 2011 at 2:38 pm
Thanks for this post! Is there any way i can obtain the raw data and time and save it to a text file?
Best regards,
Q. K
April 3rd, 2011 at 2:40 pm
Thanks for this post! Do you know how to obtain the raw data with the time and save it into a text file? But not just copy paste from the serial monitor. Your help are greatly appreciated!
Best regards,
Q. K
April 3rd, 2011 at 2:41 pm
Hello,
Thanks for this post! Do you know how to obtain the raw data with the time and save it into a text file? But not just copy paste from the serial monitor. Your help are greatly appreciated!
Best regards,
Q. K
April 3rd, 2011 at 2:42 pm
Hello,
Thanks for this post! Do you know how to obtain the raw data with the time and save it into a text file? But not just copy paste from the serial monitor. Your help are greatly appreciated!
July 19th, 2011 at 9:27 am
Brilliant! This was a tremendous help. I got it rolling with Python2.5 on Vista in about 20 minutes, and most of that time was downloading libraries.
Thank you!
PS: For those of you looking to save the raw data into Excel, why not modify the Python to write to a file in Arduino_Monitor.py? Maybe write the buffer right after it’s read?
July 22nd, 2011 at 7:36 pm
Thank you very much!! This is exactly what I needed. It’s awesome. I won’t have to adapt Eli Bendersky’s code to add serial…
I’ll customize it a bit, to fit my needs. (add a logger and some UI elements, for COM port selection etc.)
For those who are interested he also did this :
http://eli.thegreenplace.net/2009/08/07/a-live-data-monitor-with-python-pyqt-and-pyserial/
September 10th, 2011 at 3:11 pm
[…] Plotting My search for a plotting solution lead me to look into Python matplotlib and pySerial to plot the temperature and see some internal variable such as the PID in action. I found this via the Arduino Play ground. I’ll be looking into this to see if I can fit it to my needs. It would be a handy interface for future projects as well. blendedtechnologies 25A/250V Sold State Relay […]
September 13th, 2011 at 8:36 pm
Thank you very much^^
I just copy your code and I got it!!!
Anyway, do you know how to plot multiply data in one graph?? Data was sent by single serial port
October 22nd, 2011 at 5:10 am
Thank you.
I could not get Processing to run because of incompatible java versions or something.
I have been playing with python for a while. I have the analog data comming at one byte every 100 us on a serial of 115 k and written to a csv file in python for analysis and graphing in a spreadsheet.
I couldn’t get control of the data flow into the array for display directly in python.
October 23rd, 2011 at 8:08 pm
Great write up!
I got it working in Python 2.7 on Ubuntu 11.04 with two changes:
1. port=’/dev/ttyACM0′
2. select a specific version of wx:
import wxversion
wxversion.select(”2.8″)
import wx, wx.html
December 14th, 2011 at 4:29 pm
Sound and simple program. Nice work!
I’ve got it working on debian squeeze, but had to disable the interCharTimeout=None option on line 37 of Arduino_Monitor.py. (Squeeze comes with pyserial version 1.35, which doesn’t support this option)