Accelerometer Example - Esplora

Accelerometer Example

This example shows how to use the accelerometer integrated on the Arduino Esplora. 
In particular it is explained how to print on serial monitor the gotten values by accelerometer for each axis when the board is tilted.

Hardware Required

  • Arduino Esplora

Circuit

Accelerometer of Arduino Esplora 
In this sketch you need only your Arduino Esplora connected in your PC with USB cable.

Code

First it is included the "Eplora.h" library to use the accelerometer functions. 
In the setup function it is initialized the Serial comunication with the "Serial.begin(9600)" command. 
In the loop block it is used the "Esplora.readAccelerometer()" function to get the values from the accelerometer. 
This function needs an argument to indicate which axis it must read : X_AXIS, Y_AXIS, or Z_AXIS. 
Finally it is shown the results using "Serial.print()" function. 
NOTE: 
You have need the "Esplora.h" library to use this sketch.

 
 
/*
Esplora Accelerometer
 
This sketch shows you how to read the values from the accelerometer.
To see it in action, open the serial monitor and tilt the board. You'll see
the accelerometer values for each axis change when you tilt the board
on that axis.
 
Created on 22 Dec 2012
by Tom Igoe
 
This example is in the public domain.
*/
 
#include <Esplora.h>
 
void setup() {
Serial.begin(9600);       // initialize serial communications
}
 
void loop() {
int xAxis = Esplora.readAccelerometer(X_AXIS);   // read the X axis
int yAxis = Esplora.readAccelerometer(Y_AXIS);   // read the Y axis
int zAxis = Esplora.readAccelerometer(Z_AXIS);   // read the Z axis
 
Serial.print("x: ");     // print the label for X
Serial.print(xAxis);     // print the value for the X axis
Serial.print("\ty: ");   // print a tab character, then the label for Y
Serial.print(yAxis);     // print the value for the Y axis
Serial.print("\tz: ");   // print a tab character, then the label for Z
Serial.println(zAxis);   // print the value for the Z axis
 
delay(500);             // wait half a second (500 milliseconds)
}