Need a simple accelerometer visualization example!


I'm doing a project using the ADXL335 accelerometer from SparkFun. The actual project I'm using P5 and it's working great. I'm running workshops in parallel and want to demo the chip in real-time but am currently out of time to build something from scratch. I'm desperate for a simple processing (or something else) sketch that takes my x, y, z values being printed to the serial port from Arduino and visualizes them in some way. Can anyone point me to anything that won't need a lot of coding time and effort just now?

Thank you! Claudia


  • Answer ✓

    I use variations on this program:

     Strip Chart Slider
     Reads data from the serial port and draws it as a strip chart
    import processing.serial.*;     // import the Processing serial library
    int linefeed = 10;              // Linefeed in ASCII
    Serial myPort;                  // The serial port
    Sensor[] sensors;
    int count = 0;
    int x = 0;
    int xRect;
    void setup()
      // set up the usual stuff
      size(1000, 720);
      colorMode(RGB, 100);
      // List all the available serial ports, just FYI
      // Use the correct port in the serial list
      myPort = new Serial(this, Serial.list()[3], 9600);
      // set up sensor array, assigning colors
      sensors = new Sensor[6];
      sensors[0] = new Sensor(0, 0, color(100, 0, 0));    // red
      sensors[1] = new Sensor(0, 0, color(0, 100, 0));    // green
      sensors[2] = new Sensor(0, 0, color(0, 0, 100));    // blue
      sensors[3] = new Sensor(0, 0, color(0, 100, 100));  // cyan
      sensors[4] = new Sensor(0, 0, color(100, 0, 100));  // magenta
      sensors[5] = new Sensor(0, 0, color(100, 100, 0));  // yellow
    void draw() 
      // blank current slice
      line(x, 0, x, height);
      // draw current readings
      for (int i=0; i<count; i++ )
        point(x, 512 - sensors[i].value/2);
      // update x for next slice
      x = (x+1) % width;
      // erase rectangle area
      rect(0, 520, width, 200, 20);
      // set color from sensor values
      fill(sensors[0].value/10, sensors[1].value/10, sensors[2].value/10);
      // get position from sensor 0
      xRect = sensors[0].value/2;
      // and draw the rectangle
      rect(xRect, height-150, width/2, 100, 20);
    // serialEvent method is run automatically
    // whenever the buffer receives a linefeed byte 
    void serialEvent(Serial myPort) 
      // read the serial buffer:
      String myString = myPort.readStringUntil(linefeed);
      // if we get any bytes other than the linefeed:
      if (myString != null) 
        // remove the linefeed
        myString = trim(myString);
        // split the string at the tabs and convert the sections into integers:
        int mysensors[] = int(split(myString, '\t'));
        count = mysensors.length;
        for (int i=0; i<count; i++)
          // set sensor[i] value for use in draw()
          sensors[i].value = mysensors[i];
          // print out the values we got:
          print(i + ": " + sensors[i].value + "\t");


    class Sensor
      int value;
      int offset;
      color shade;
      Sensor( int val, int pos, color hue)
        value = val;
        offset = pos;
        shade = hue;

    My Arduino is sending tab-separated lines of data.

    Let me know if you have any questions.

  • edited April 2015

    I had made a joystick out of that for NFS mostwanted... Basically see which side it is tilted to and use keyPress(key) in robot class mimic the keyboard! You could always create your own game like DXballs that uses arduino as the console... Let me know if you need the code for reference for the NFS thing... it is somewhere deep down in my drive! :p

  • Answer ✓

    Cross questioned here . Just mention that in the question so that people from both forums can follow your progress...

  • Amazing srinag!! It worked almost out of the box - I am putting commas between my values in my arduino code but that was an easy switch. I'm going to play a bit with the visuals but this was an amazing help. One thing that I can probably fix on my own but I don't see the z value showing up - probably difficult to advise without seeing my setup / code, but if you have any thoughts on that let me know. THANK YOU for this - such a big help!

  • Hello, I use as well the processing code of the “simple accelerometer”. The code is running well and prints out single points of my Arduino MPU5060 acceleration data. The data is changing very quickly, so I need some change of visualization settings. The attached code runes on com7, data split with \t. How can I change the print out settings:

    a) Connect every measurement (point) with a line?

    b) Change the thickness of the lines or points?

    c) Change the range to, + 3000 -> -3000?

    Sorry for opening this question twice...

  • edited October 2015

    I have gone through your code and your problem. I had not worked so much but i have the some idea about the accelerometer co-ordinates display. For this you have to make changes in the program according tot he speed of your hardware. Change the speed and the thickness of the lines. Also make sure that the line must be connected with the every measurement.

    assembling pcb

Sign In or Register to comment.