Realtime Graphing of Accelerometer/Gyroscope Data

During my naive days as a university student the only time I graphed data was when some professor required a graph in the lab report. Back in those days I always just looked at the numbers to see what what was happening. It wasn’t until I worked on much more complex real world problems that I admitted graphing the data can help. It’s ironic that as I’ve become a better engineer I’ve realized how important it is to take advantage of every crutch I can fine. These days I graph just about every kind of data I see. Want to know when all the office birthdays are? Let me graph that out for you…

Graphing data lets my mind understand what is happening much faster/better the raw numbers. I know there are some less experienced programmers/engineers that need to learn this. The holy grail for software is real time graphing of the data your application is using. So often people add this sort of feature in at the end, but I think there are benefits to doing it first since it reduces debugging time. At my job we have a whole team dedicated to real time graphing of the data processed by our 3D graphics processors. I am always extra friendly to the people on this team because I fully realize how much easier they make my job. In short I’m a graphing fan boy so it should be no surprise that I wrote some graphing software for my hobby projects. I did this as the first step in a much larger autonomous flying vehicle project that I’m working on.

Since my ultimate goal is a flying vehicle I knew I’d need an inertia measurement unit (IMU). I wanted something with accelerometers to measure acceleration and gyroscopes to measure rotation. After some research I found the ADXL330 is a good accelerometer and the IDG300 is a good gyroscope. I use this Sparkfun 5 degrees of freedom (DOF) device that has both of these chips integrated onto a single small board. These chips measure acceleration along all three axises (x,y,z) and the gryroscopes measures pitch and roll. This means I am missing yaw, but I decided I can add an electronic compass in the future to get this data. I have an Arduino so that is the microcontroller I used.

With all the hardware figured out I already knew that taking this many inputs into a project and expecting it to do what I want was not going to happen on my first try. So to make the debugging less frustrating I decided to graph out all these signals. I will probably add some derived data to these graphs in the future as I get further on this project, but I think it makes sense to share this simple version of the generic graphing software before things get overly complicated.

I used the open source Arduino SDK to compile the code for my microcontroller. Here is the code I wrote for the Arduino. This code reads the sensors and then writes this data onto the serial bus. This software lets you figure out the frequency you want to refresh this data and then will sample the sensors as many times as it can while hitting this refresh frequency. Multisampling helps reduce sensor noise. The reason I didn’t want to send every sensor value over the serial bus is because that it too much data. For my uses I limited the refresh frequency to 100 times per second.

The graphing was done with the open source Processing language. I had never used Processing before, but I found it very easy to use and in a few hundred lines of code got some pretty nice graphing functionality. In a nutshell Processing is a java based language with a bunch of drawing and IO helper functions. Here is the graphing code I wrote for Processing. Besides graphing the data in real time, this software also supports logging all the data to a file and applying a smoothing filter to the data. I wrote this code pretty cleanly so it should be easy to add and remove more data to the graphs.

Here’s a movie of me shaking the sensors in a few different directions and you can see the graphs react accordingly. I’ll let you derive your own conclusions, but it looks like I have a good deal of work ahead of me.

26 Comments

  1. Maurice Ribble said,

    June 30, 2009 @ 9:08 pm

    This thread adapted this project for use with the gyro included in the wii plus controller. The benefits of using that gyro are that it’s cheaper and it uses a two wire bus instead of the analog ports (faster and uses less pins on Arduino).

    http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1245723011/15#15

  2. Les Garwood said,

    August 18, 2009 @ 10:30 pm

    Thanks VERY much for your helpful code. I’m a weekend coder wannabe and find it extremely slow going. I’ve been looking for graphing code for arduino and processing for months, and yours is far and away the best for my use so far. Basically, I’m working on an IR remote temperature sensor that can graph. I’ve cobbled together a linear amp and a thermopile, then hooked the output to one of my boarduino’s analog inputs. I’m running the output into a serial/USB converter cable. That’s were my useful data stream ended. Your code may help change that. Real time, or simulated, is essential to my project. Eventually, I want to add audio to the graph so that a musical tone changes pitch with the rise and fall of the temp. I also want my graph to register highest and lowest temp placekeeper marks. Actually, a bar graph version might be ideal for this.

    Regards,

    Les

  3. Nataraj.S said,

    March 26, 2010 @ 10:10 am

    Hi,
    When I run ur code I get the following error
    Error inside Serial.()
    at processing.serial.Serial.errorMessage(Unknown Source)
    at processing.serial.Serial.(Unknown Source)
    at imu.setup(imu.java:55)
    at processing.core.PApplet.handleDraw(Unknown Source)
    at processing.core.PApplet.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:619)

    what could be the reason,
    Thanks
    Nataraj

  4. Nataraj.S said,

    March 26, 2010 @ 10:58 am

    Hi,
    I figured the problem,your code works great.
    Thank you once again.
    Nataraj

  5. sadiq al sadi said,

    March 28, 2010 @ 11:27 am

    hi….
    i have the same problem that is written above but i couldn’t figure it out…..
    can you help me plzzz…
    thanks alot…

  6. Kev Lewis said,

    May 14, 2010 @ 3:55 pm

    Very useful code block for the rtc, it let me quickly test the rtc was operating correctly. AFTER I realised the arduino mega has the I2C separated out onto their own pins lol

    Your site is a gold mine of arduino code which I find fascinating and extremely useful. Thank you

    Kev Lewis aka PhotosbyKev

  7. Bryan Cochran said,

    June 25, 2010 @ 12:58 pm

    So I got the code compile and uploaded to my Arduino but it won’t output a graph. I’m pretty new to this whole thing so what am I missing?

  8. Marco said,

    July 3, 2010 @ 11:54 am

    Hi,
    thanks so much for the code, it works great!!
    I had one minor problem and it’s that my output file is empty and I can’t work with the collected data.
    I guess I should change something in the code so that the output file doesn’t get constantly cleared of all data but I am not sure what. Any idea?
    Thanks again,

  9. Oscar said,

    July 29, 2010 @ 11:30 pm

    Nice tool. It helped me get going rather quickly. It was the best one I’ve seen for real-time graphing in Processing. I’ve been extending this to be a more generic plotting tool so I can adapt it easily enough to capture any number of signals, and configure how I want signals plotted, etc. I was going to try and get the settings all in one place in the global definitions or maybe in a text file to read in at the start. Maybe some buttons to do some tweaking like auto-scaling.

    Would you mind if I post my code, which builds on yours, on the arduino site when I’ve got something that might be useful to others?

  10. Maurice Ribble said,

    July 30, 2010 @ 4:50 am

    Oscar, I’m glad you found this useful! You can post your code which is based on mine as long as you give me the appropriate credit.

  11. Pete Townson said,

    October 28, 2010 @ 8:51 pm

    Mr. Ribble,
    having a some problems using your Processor code,
    I have never used Processor before this and i only just learn’t the very basics of Arduino at uni ( in a design class mind you)

    after modifying your processor code so it reads the correct port and the baud rate co-insides with the baud of my arduino, i get no errors and my IMU pops up with the graph legend – but the graph is black.

    When i press (play) on arduino and then press (play) on the Processor, the only error i get is

    RXTX Warning: Removing stale lock file. /var/lock/LK.008.011.029

    Mate, would love to get this to work. This is only 1 small part of my assignment which is due in a few days, and it isn’t necessary for me to get this to work, as my project was involving the use of RFID’s – but they haven’t arrived yet from Sparkfun (an in Australia, we don’t have too many electronic stores to supply that sort of gear) so i am working on this so i have something to show for my presentation.

    does anybody have any thoughts as to why my graph isn’t working ????

    Ta

  12. Joe Rosenblum said,

    February 1, 2011 @ 11:54 am

    Thanks for posting this. I used the code almost out of the box with my ADXL335 (not on a 5dof chip) to graph X, Y and Z. Very helpful.

  13. Eapen said,

    February 4, 2011 @ 12:45 pm

    does processing accept data from the arduino board in byte format only??..

  14. Rohan said,

    March 14, 2011 @ 4:23 am

    I tried to use your code. But i am getting the following error.
    If you could explain me as to where i am going wrong or is it something i have missed.
    I tried to assess the problem myself, and stumbled here. See a few others have also faced a similar issue.

    If some one can post or mail me what is going wrong.

    Below is the message that i get on the processing GUI Message window

    Stable Library
    =========================================
    Native lib Version = RXTX-2.1-7
    Java lib Version = RXTX-2.1-7
    [0] “COM46″
    processing.app.debug.RunnerException: ArrayIndexOutOfBoundsException: 1
    at processing.app.Sketch.placeException(Sketch.java:1543)
    at processing.app.debug.Runner.findException(Runner.java:582)
    at processing.app.debug.Runner.reportException(Runner.java:558)
    at processing.app.debug.Runner.exception(Runner.java:498)
    at processing.app.debug.EventThread.exceptionEvent(EventThread.java:367)
    at processing.app.debug.EventThread.handleEvent(EventThread.java:255)
    at processing.app.debug.EventThread.run(EventThread.java:89)
    Exception in thread “Animation Thread” java.lang.ArrayIndexOutOfBoundsException: 1
    at imu.setup(imu.java:55)
    at processing.core.PApplet.handleDraw(Unknown Source)
    at processing.core.PApplet.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:619)

  15. Balazs said,

    March 15, 2011 @ 2:49 am

    @Rohan
    You have to set the COM port in the processing program to match the one where the arduino is connecting to your computer. In the code it says [1] which is not the first connection, but the second. I had to modify mine to [0] to get it work.
    –> g_serial = new Serial(this, Serial.list()[0], 115200, ‘N’, 8, 1.0);

    @Pete Townson
    On the arduino side you have to press play to compile the code, but after it is done, you have to upload it on your board (there is an upload button). After the code is up and running, you don’t need the arduino program, the board will send data to your computer through the USB port. Now you can start the processing code.

  16. Li Zhou said,

    May 6, 2011 @ 8:14 pm

    Hi, Thank you so much for sharing the code with us. It’s so helpful.

    While when I run the processing code. The graph will come out, but it is blank in the graph. There is no curve at all. Could you please tell me the reasons?

    Many Many Many Thanks!!!

    Li Zhou

  17. Li Zhou said,

    May 6, 2011 @ 8:17 pm

    Oh, I figured it out already. I am using the wrong COM port number.

  18. david izy said,

    July 11, 2011 @ 8:07 pm

    hey if you need more pins for u project and you are using the arduino u might wanna buy the mux shield for your project that might help out they sell them at sparkfun here is the link http://www.sparkfun.com/products/9832

  19. Brian said,

    May 26, 2012 @ 7:12 pm

    Hello my friend, i hava questions with the pin conexion, i have IMU Analog Combo Board – 5 Degrees of Freedom IDG500/ADXL335 ( http://www.sparkfun.com/products/9268 ) , I need to know the use for
    ST
    VRef
    RAW
    PTAT
    AZ
    X 4.5
    Y4.5…

    I have fear of damaging the device..

    Thank you for your attention, waiting for a prompt response…

    Brian Lara

  20. roweboat said,

    June 28, 2012 @ 6:42 am

    Another great tool for plotting arduino data is MegunoLink. It’s a windows application that lets you monitor, log, and plot data sent from a serial port.

    Check it out at MegunoLink.com

  21. Green_tie said,

    July 22, 2012 @ 2:12 am

    Help me.. when i run the processing.. no activity or movement from my acclerometer MMA7361.. i want to monitor the sketch graph from mine accelerometer..

    [0] “COM1″
    [1] “COM3″
    error saving bytes to c:\output.txt
    java.io.IOException: Access is denied
    at java.io.WinNTFileSystem.createFileExclusively(Native Method)
    at java.io.File.checkAndCreate(File.java:1704)
    at java.io.File.createTempFile(File.java:1792)
    at processing.core.PApplet.saveBytes(Unknown Source)
    at processing.core.PApplet.saveBytes(Unknown Source)
    at imu.setup(imu.java:79)
    at processing.core.PApplet.handleDraw(Unknown Source)
    at processing.core.PApplet.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:662)

  22. Stefano said,

    August 12, 2012 @ 2:07 pm

    Hello my friends,
    I have a question about the code to read the output of IMU IDG500/ADXL335. I saw the wonderful program in this page. Are there other similar code to download on the web?

    Thank you

    Stefano

  23. Joel said,

    October 27, 2012 @ 6:07 pm

    Hi, I’m working with a college project were I need to plot the serial data from an accelerometer. I gotta say, I’m kind of new with programming and specially with Java. I’ve been using your code to plot x, y, z data from the accelerometer of a Wii nunchuck. I’ve been using this Arduino class http://arduino.cc/playground/Main/WiiChuckClass from the website and I’ve been trying to integrate it to your code. I know I can just use a normal accelerometer like the ADXL ones from sparkfun but I already have the nunchuck and I want to cut costs from my project. The problem that I’m having is in interfacing with the individual x,y,z from the nunchuck and displaying the correct curves on the graph.

    As you may already know, the nunchuck uses de I2C protocol and I’m setting 4 pins: PWR, SCL, SDA and GND. So, I’m not able to interface the x, y, and z pins individually like normal accelerometers do. The nunchuck returns its data using six bytes. Byte 3, 4, and 5 are for the x,y,z, respectively. The WiiChuckClass library uses chuck.readAccelX(), chuck.readAccelY() and chuck.readAccelZ() to read from these bytes. So I’m substituting these functions in your Arduino code as parameters of analogRead:

    xAccel += analogRead(chuck.readAccelX());
    yAccel += analogRead(chuck.readAccelY());
    zAccel += analogRead(chuck.readAccelZ());

    In conclusion, after I upload this modified code into the Arduino and click play on Processing I’m getting the right response and sensitivity from the nunchuck’s accelerometer but the curves in the graph are exactly the same for x, y and z. It’s like I’m reading the same data for each of the dimensions. I would really appreciate some kind of feedback from you as, I’ve been really stuck on this part. If you have any questions feel free to ask. Thank you and have a good day.

  24. Steph said,

    October 30, 2012 @ 10:19 am

    Hi,

    I’m making similar project: http://www.jouin.eu/phpBB/viewtopic.php?f=15&t=24
    (standalone sismic detector using Wiichuck+gps+gsm).

    I have not see that the curves in the graphs given same data.

  25. Andrea said,

    December 6, 2012 @ 4:14 am

    i have a question… are you running both programs at the same time? arduino and processing?

  26. Lenny swkp said,

    May 22, 2013 @ 11:58 am

    |

RSS feed for comments on this post