How to Graph an Accelerometer With Arduino

Found This Helpful

Accelerometers are electronic components that measure the acceleration, tilt or vibration of an object when connected to another device. You can interpret the data provided by an accelerometer by connecting it to an Arduino microcontroller. The Arduino reads the values from the accelerometer's output pins and transmits them to a computer. The computer displays the data in numerical form. You can also graph the data by transmitting it to a programming application, such as Max/MSP or Processing, where it can be plotted in real time.

Things You'll Need

  • Breadboard
  • Wire
  • USB cable

Connect the Accelerometer to the Arduino

  1. Place the accelerometer on the breadboard with its pins on separate tracks.

  2. 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.

  3. 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.

  4. Connect the accelerometer's ground pin to the ground terminal on the Arduino with wire.

  5. Plug a USB cable into the Arduino's USB connector.

  6. Plug the other end of the USB cable into a USB port on a computer.

Write the Arduino Code

  1. Assign variables to the Arduino inputs connected to the accelerometer's outputs. Your code might look like this:
    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.

  2. Initiate serial communication between the computer and the Arduino. Your code might look like this:
    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.

  3. Define the Arduino's input pins under "setup." Your code might look like this:
    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."

  4. Assign variables for storing incoming axis and acceleration data. Your code might look like this:
    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.

  5. 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:
    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.

    Your code might look like this if your accelerometer provides analog data:
    pulseX = analogRead(xPin);
    pulseY = analogRead(yPin);
    This code reads the values from the Arduino's inputs and stores them in "pulseX" and "pulseY."

  6. 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.

  7. Print the acceleration data in the serial monitor, and send it to a compatible program for graphing. Your code might look like this:
    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.

  8. Add a delay before the Arduino goes back to the first line of code. Your code might look like this:
    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.

  9. Click the "Upload" button to upload the code to the Arduino.

Graph the Acceleration

  1. Launch a programming environment such as Max/MSP, Processing or Pure Data, that accepts serial data.

  2. 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.

  3. 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.

  4. 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.

  5. Write code that plots points on the graph using the data provided by the Arduino.

  6. Write code that draws lines between the plotted points.

Related Searches

References

Comments

  • Photo Credit Ryan McVay/Photodisc/Getty Images

Featured

Related Ads

Related Searches
Watch Video

#eHowHacks: Open a Wine Bottle With a Shoe