Realtime Plot of Arduino Serial Data Using Python

So I got an Arduino a few weeks ago, and just made my second little project:

Arduino Knock Sensor Circuit

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:

arduino_plot_screenshot.PNG

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.

del.icio.us |  Digg |  FURL |  Yahoo! My Web 2.0 |  Reddit

25 Responses to “Realtime Plot of Arduino Serial Data Using Python”

  1. wwn Says:

    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

  2. someone Says:

    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?

  3. flashbox Says:

    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!

  4. Colin Heics Says:

    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.

  5. NAJK Says:

    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

  6. Kevin Says:

    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?

  7. gen Says:

    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/

  8. WaZK Says:

    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

  9. Graham Says:

    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.

  10. theepdinker Says:

    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

  11. Janneman Says:

    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)

  12. Arduino Test Runs (part 1) | The Scrambled Bit Says:

    […] So now I have 2 pots on my board, and I have to get serial data to the computer. This time, I wanted to work… so I Googled a while, until I found that pySerial can read lines of the serial port (n delimited). Awesome, I have packets. I try this… amazing… I can draw for 3 seconds than it hangs. A bit more searching, and I found the answer to my problem: read continuously off the serial port in a thread and get the last result whenever I ask for it. A nice class which does that can be found at the answer. I can now send more numbers to the computer, awesome! This was my fifth attempt at drawing my logo using the 2 potentiometers etch-a-sketch style. […]

  13. [g12] » Blog Archive » PDP (HRV) monitorius logeris Says:

    […] Toliau: skaitau šį output’Ä… su python programyte: […]

  14. kooXKristenSuera Says:

    This is the proper weblog for anyone who wants to find out about this topic. You realize so much its virtually laborious to argue with you (not that I truly would want…HaHa). You definitely put a new spin on a topic thats been written about for years. Great stuff, simply great!

  15. TideMan Says:

    Running on Ubuntu, I inserted:
    import wxversion
    wxversion.select(’2.8′)
    before import wx.
    Also, I had to remove:
    interCharTimeout=None
    and change the port to /dev/ttyACM1.

    Now it’s working but straightlining at 100, yet the Arduino Serial Monitor shows changing data. What can be wrong?

  16. website Says:

    Have you thought about adding some relevant links to the article? I think it will really enhance viewers understanding.

  17. chronic mono Says:

    I have got one idea for your web site. It looks like there are a handful of cascading stylesheet problems when launching a number of web pages in google chrome and firefox. It is working alright in internet explorer. Perhaps you can double check that.

  18. roweboat Says:

    Great work! We have been working on some software to try and do this as well. Will be interested to know what you think. Check it out at megunolink.com

  19. mausgang Says:

    Cool work. Could the python be reconfigured to talk with another executable, I’m trying to get an Arduino to talk with a GC’s computer software.

  20. Felipe Martins Says:

    Great post! Very helpful!
    The only thing I had to do was to change my COM port number. It is working perfectly! I am running Windows 7 and Python 2.7.3.
    I had also changed the type of the variable “val” on your Arduino code. I am using a long int so that I can read the full ADC range (10 bits = 0-1023).
    Thanks a lot for the post!!

  21. Tim Pitman Says:

    Great work! I’ve spent a lot of time researching the best realtime Python plotting libraries. I initially used matplotlib, but eventually switched to Chaco, which is one of Enthought’s libraries. I found it much faster for realtime type stuff. It’s not quite as easy to use and not as good for live plotting, but there are other features (ie Traits) which make it really awesome in other ways. Highly recommended for anyone wanting higher refresh rates and/or lower CPU usage.

  22. steff Says:

    I am wondering if there is anyway I can export my count directly from two sensors (serial data)from Arduino connected to a serial bluetooth module to excel or any other software that can plot graphs in real time.

    Thanks for any help.

    Steff

  23. Jose Says:

    I really like reading your blog. There is only one problem. I cannot find your rss feed button.

  24. Sex anonse Says:

    www.blendedtechnologies.com gives me so much pleasure

    Sex za darmo

  25. http://newespressomachine.com/ Says:

    I’m not that much of a online reader to be honest but your sites really
    nice, keep it up! I’ll go ahead and bookmark your website to come back down the road. Cheers

Leave a Reply