The other day I picked up a Parallax Memsic 2125 dual-axis accelerometer from Radio Shack to use with my Arduino. There was a great write up on the Arduino website about how to use the accelerometer, but after getting it working I wanted to take it a step further and see if I could get it to work with Processing (a simple language for creating interactive programs quickly). Here is a quick write up of how I used processing to create a simple application similar to a demo I saw on this YouTube video. There are much more elegant ways of doing this, but its a quick way to get started. I put the code up on GitHub so feel free to fork it, and whatnot. Here is a screenshot of the final application:

The circuit:
The Arduino website has a great write up on how to wire the accelerometer. I’m not going to go into detail on how to wire it up, because it is very simple. The only thing to remember is to pay attention to the triangle on the Memsic - its pointing up.
The Arduino Code:
That Arduino Memsic 2125 page has some sample code to output over a serial connection every 100 milliseconds. The only real modifications from that code is that we need to map the X and Y inputs from the Arduino to a value between 0-500 (This will make more sense a bit later when we get to the Processing sketch). Here is the code (also available on GitHub):
/*
Sample code to read inputs from the Memsic 2125 accelerometer.
This is based off of the code by David A. Mellis and Tom Igoe
http://arduino.cc/en/Tutorial/Memsic2125?from=Tutorial.AccelerometerMemsic2125
*/
// these constants won't change:
const int xPin = 2; // X output of the accelerometer
const int yPin = 3; // Y output of the accelerometer
void setup() {
// initialize serial communications:
Serial.begin(9600);
// initialize the pins connected to the accelerometer
pinMode(xPin, INPUT);
pinMode(yPin, INPUT);
}
void loop() {
// variables to read the pulse widths
int pulseX, pulseY;
// variables to contain the resulting accelerations
int accelerationX, accelerationY;
// read the pulses from the accelerometer
pulseX = pulseIn(xPin,HIGH);
pulseY = pulseIn(yPin,HIGH);
// map the pulse to a value between 0-500, to be used in Processing
accelerationX = map(pulseX, 3700, 6350, 0, 500);
accelerationY = map(pulseY, 3700, 6350, 0, 500);
// send the accelerometer data to Processing through serial connection
Serial.print(accelerationX);
Serial.print(",");
Serial.print(accelerationY);
Serial.print('\n');
// delay the loop so we're sending a manageable amount of data
delay(100);
}
Lets go through the code line by line:
- Line 10 and 11 create constants for the X and Y input pins from the Arduino.
- In our setup() function on line 16, we initialize a serial connection. Then we set the xPin and yPin to INPUT (because we want to read the data coming in from the accelerometer).
- Then in our loop() function on line 25 we create 2 variables, pulseX and pulseY, and then on line 28 we create 2 more variables accelerationX & accelerationY.
- On line 31 and 32 we use the pulseIN function to get the time between the pin going from HIGH to LOW. You can read more about pulseIN at the Arduino website. Once we have read the pulses from the accelerometer, we map that data to a value between 0-500. We do this because the window of our Processing application is set to 500. By mapping the values we get in, we can create coordinates to use in the Processing application.
- Lines 39-42 print our mapped data into a serial connection. These 4 lines print the data so it is on one line line so: accelerationX, accelerationY .
- Finally, on like 45 we delay the loop for 100 milliseconds.
Here screen shot of the Arduino environment and the serial output you should see:

The Processing Code:
This was my first attempt at using Processing and I was surprised how simple it was to get things up and running. The Arduino environment is based off of Processing so it feels very familiar. The code is set up similarly to Arduino code, but instead of a loop() function, we have a draw() function. Here is the code, and here it is on GitHub:
/*
Sample code to receive serial data from an Arduino hooked up to a
Memsic 2125 accelerometer.
*/
// import and set up serial connection to the Arduino
import processing.serial.*;
Serial port;
// variables for the coordinates
int coordinateX = 0;
int coordinateY = 0;
// index of the comma between values (when reading from Arduino)
int index = 0;
void setup(){
// create window
size (500, 500);
// begin serial connection
// you will need to change the second argument for your setup
port = new Serial(this, "/dev/tty.usbmodem1d11", 9600);
port.bufferUntil('\n');
}
void draw(){
// draw the background
background(0);
// draw the grid
stroke(255);
line(250,0,250,500);
line(0,250,500,250);
// draw the ellipse
noStroke();
fill(255,0,0);
ellipse(coordinateY, coordinateX, 50, 50);
// draw the text
fill(255);
text("X: ", 10, 480);
text(coordinateX, 25, 480);
text("Y: ", 10, 500);
text(coordinateY, 25, 500);
}
// called whenever we receive data from the arduino
void serialEvent(Serial port){
// read the input
String buffer = port.readStringUntil('\n');
// if input is not null
if(buffer != null){
// find the index of the comma
index = buffer.indexOf(",");
// check that the index is greater than zero
if(index > 0){
// get the coordinates from the original string
String stringX = buffer.substring(0, index);
String stringY = buffer.substring(index+1, buffer.length());
// trim coordinate strings so we don't get errors
String trimX = trim(stringX);
String trimY = trim(stringY);
// set the ellipse's coordinates to the trimmed strings
coordinateX = int(trimX);
coordinateY = int(trimY);
}
}
}
Again lets go through the code line by line:
- We start on line 10 by importing the processing serial library. (The Arduino environment imports it by default).
- Next on line 11 we create a Serial variable called port, which we will use to get the serial data from our Arduino.
- On lines 14 and 15 we create variables for our X and Y coordinantes, and then on line 18 we create a variable called “index” which we will use when reading our serial strings.
- In the setup function on line 22, we start by creating a window that is 500px by 500px.
- Then on line 26 we create a new Serial connection. The Serial Library constructor is in the form: Serial(parent, name, rate). In our case the parent is “this”, and our rate is the default 9600. The only thing you need to change is the name — it will be different for you depending on your machine.
- The last part of the setup() function on line 27 tells the Serial connection to buffer until we get a new line - ‘\n’.
- The draw() function is called whenever we need to update our screen. On line 32 we set the background to be black. Lines 35-37 create our grid. Line 35 sets the stroke color to white. Lines are created by using the format: line(x1, y1, x2, y2) where x1 & y1 are the beginning coordinates and x2&y2 are the ending coordinates.
- On lines 40-42 we draw our red ellipse. We start by making sure we don’t have a stroke color set on line 40, and then on line 41 we set the fill color to be red. On line 42 we create our ellipse at coordinateY & coordinateX with a width and height of 50px.
- The last part of the draw() function draws our variables on the bottom of the screen. I did this to debug while I was getting the application going. On line 45 we set the fill color to white. Then on 46 we print “X: “, and on line 47 we print the coordinateX. 48 and 49 do the same thing but for the Y coordinate.
- The last function in the sketch is our serialEvent(Serial port) function. It is called whenever Processing receives serial data from our Arduino.
- On line 56 we create a string which reads the serial input until we come to a ‘\n’ character.
- Then on line 59 we check to make sure our string is not null. If it is not, we set our index variable that we created on line 18 to be the index of the comma in the string (remember, our serial data comes in the format of X, Y). We then check to make sure the index is greater than zero (if it were zero, we wouldn’t have an X).
- On line 66-67 we create two strings from the serial data, one with the X data and one with the Y data. We then trim the strings to make sure there are no extra spaces at the beginning or ends.
- The last step is to turn our X and Y strings into integers, so we can use them to draw our ellipse in the proper location in the window.
Here is another screenshot of the final application:

I hope this helps you get started using your Arduino to make some cool things in Processing! Hit me up on Twitter (@parkerboundy) if you have any questions, or to show me what else you’ve built from this sample code (or if you’ve found a bug in my code…)