Things You'll Need
Instructions
Connect the Accelerometer to the Arduino
Place the accelerometer on the breadboard with its pins on separate tracks.
Connect the accelerometer's axis pins to the Arduino's analog or digital inputs with wire. Use one input per pin. The accelerometer's datasheet tells you which pins provide the axis data and whether the data is analog or digital.
Connect the accelerometer's supply voltage pin to the appropriate voltage output on the Arduino with wire. The accelerometer's datasheet tells you how much power it needs.
Connect the accelerometer's ground pin to the ground terminal on the Arduino with wire.
Plug a USB cable into the Arduino's USB connector.
Plug the other end of the USB cable into a USB port on a computer.
Write the Arduino Code
Assign variables to the Arduino inputs connected to the accelerometer's outputs. Your code might look like this:
Initiate serial communication between the computer and the Arduino. Your code might look like this:
Define the Arduino's input pins under "setup." Your code might look like this:
Assign variables for storing incoming axis and acceleration data. Your code might look like this:
Read the data from the Arduino's inputs and store it in the appropriate variables. Your code might look like this if your accelerometer provides digital data:
Your code might look like this if your accelerometer provides analog data:
Convert the data stored in "pulseX" and "pulseY" into acceleration. The accelerometer's manufacturer should provide an equation or table of values to use for the conversion.
Print the acceleration data in the serial monitor, and send it to a compatible program for graphing. Your code might look like this:
Add a delay before the Arduino goes back to the first line of code. Your code might look like this:
Click the "Upload" button to upload the code to the Arduino.
Graph the Acceleration
Launch a programming environment such as Max/MSP, Processing or Pure Data, that accepts serial data.
Write code that tells the program to monitor and accept incoming serial data from the Arduino. The code you write depends on the programming environment you're using. Specify the baud rate you used in the Arduino code when setting up serial communication.
Write code that sets up a graph with an X and Y axis. Draw and label the axes with values appropriate for your accelerometer. You might want to use the maximum and minimum values provided by the accelerometer to define the length of each axis.
Write code that separates the data coming from the Arduino into the appropriate X-axis and Y-axis values. For example, you can assign the numerical value received after "X" to the X-axis on your graph and the value received after "Y" to the Y-axis.
Write code that plots points on the graph using the data provided by the Arduino.
Write code that draws lines between the plotted points.
int xPin = 2;
int yPin = 3;
The variables "xPin" and "yPin" are assigned to the pins connected to the accelerometer's X and Y outputs.
void setup() {
Serial.begin(9600);
The first line of code is required for all programs. The curly bracket marks the beginning of "setup." "Serial.begin(9600)" sets up communication at 9,600 bits per second.
pinMode(xPin, INPUT);
pinMode(yPin, INPUT);
}
Pins 2 and 3 on the Arduino are now set as inputs. The curly bracket marks the end of "setup."
void loop(){
int pulseX, pulseY;
int accelerationX, accelerationY;
The first line of code tells the Arduino that the following code consists of functions it must perform in a loop. The first set of integers store the data coming from the input pins, while the second set stores the results of calculations that convert the incoming data into values representing the accelerometer's acceleration.
pulseX = pulseIn(xPin,HIGH);
pulseY = pulseIn(yPin, HIGH);
The code "pulseIn(xPin,HIGH)" tells the Arduino to wait for the pin to read "HIGH" and start timing. When the pin reads "LOW," the Arduino stops timing and stores the value in "pulseX." The second line of code works in the same manner.
pulseX = analogRead(xPin);
pulseY = analogRead(yPin);
This code reads the values from the Arduino's inputs and stores them in "pulseX" and "pulseY."
Serial.print("X");
Serial.print(accelerationX);
Serial.println();
Serial.print("Y");
Serial.print(accelerationY);
Serial.println();
The lines beginning with "Serial.print" print the values stored in the assigned variables or the letters appearing in quotes. "Serial.println()" adds carriage returns between the printed variables, so they do not appear on the same line.
delay(50);
}
The first line of code sets the delay to 50 milliseconds. The curly bracket marks the end of "loop" and tells the Arduino to go back to the first line of code.