# Accelerometer

## Introduction

An accelerometer is a sensor that measures the acceleration of an object in a certain direction, from which the velocity and position can be calculated. Accelerometers are commonly used in aircraft, drones, rockets, to measure vibration, and even in mobile phones.

## Theory

All accelerometers are basically a mass and a spring, see figure 1 below: Figure 1: Basic accelerometer.

As the accelerometer moves, the spring stretches and compresses, and by measuring z, the length of the spring, the acceleration can be calculated.

## Technical Specifications

The accelerometer featured in this tutorial is the ADXL326 Triple-axis Accelerometer, it is actually 3 accelerometers combined into one chip so it can measure acceleration in all 3 directions. It has a measurement range of -16 to 16 g’s, and requires 2 inputs, Vin and GND, 5 and 0 volts respectively, and returns x, y, and z accelerations through the analog outputs, Xout, Yout, Zout. Figure 2: ADXL326 Triple Axis Accelerometer.

The analog outputs have a range of 0 – 3.3 volts, with 0 corresponding to -16g and 3.3 corresponding to 16g.

## Connections

The connections required are shown below in figure 3. Connect the 5v power port to Vin on the accelerometer. Figure 3: ADXL326 accelerometer to Arduino connections.

Connect the ground (GND) port on the Arduino to GND on the accelerometer, and connect analog in ports A0, A1, and A2 to the Xout, Yout, and Zout ports, respectively, on the accelerometer.

## Code

In order to use the accelerometer accurately it must first be calibrated. The calibration code is shown below.

`void setup(){  // put your setup code here, to run once:  Serial.begin(9600);     // neccessary to view data}int value;      // define variable to hold sensor readingvoid loop(){  // put your main code here, to run repeatedly:  value = analogRead(A0);   // read accelerometer  Serial.println(value);    // display value  delay(100);               // make easier to read}`

1)        First, connect the accelerometer as shown above, but remove the A1 and A2 input wires.

2)        Upload the code to the Arduino and open the serial monitor.

3)        Place the accelerometer flat on a stable table, it helps if the accelerometer is connected using a breadboard.

4)        Now rotate the accelerometer 90 degrees so that the +x direction is pointed vertical. Write down the value from the serial monitor.

5)        Turn the accelerometer so that the –x direction is vertical, write down the value from the serial monitor, the values from steps 4 and 5 should be between 300 – 400.

6)        Repeat steps 1, 3,4 and 5 two more times, but each time change the connection so that A0 will read y and then z outputs.

7)        The 6 values you have collected are all that is needed to calibrate the accelerometer, they correspond to x accelerations of +1g and -1g, y accelerations of +1g and -1g and z accelerations of 0g and -2g. Fitting a straight line between each of these two             points will give acceleration from the sensor output. This is done below.

The code to read the accelerations is shown below:

`void setup(){  // put your setup code here, to run once:  Serial.begin(9600);      // only neccesary to view data}// calibration constantsint x_a = 350;       // +1g xint x_b = 324;       // -1g xint y_a = 350;       // +1g yint y_b = 324;       // -1g yint z_a = 352;       // 0g zint z_b = 327;       // -2g z// variables to hold accelerationsfloat ax;float ay;float az; void loop(){  // put your main code here, to run repeatedly:   // analog inputs from accelerometers    ax = analogRead(A0);  ay = analogRead(A1);  az = analogRead(A2);  // convert acceleration value from analog input to -16g to 16g,  // using calibration constants  ax = 2*(ax - x_b)/(x_a - x_b) - 1;  ay = 2*(ay - y_b)/(y_a - y_b) - 1;  az = 2*(az - z_b)/(z_a - z_b) - 2;   delay(10);  // make easier to read  // Output Accelerations with 4 decimal places  Serial.print(ax,4);  Serial.print(" ");  Serial.print(ay,4);  Serial.print(" ");  Serial.println(az,4);}`

#### Tags:

Accelerometers

We ship to the GCC and MENA regions.

Reliable and fast shipping to your door, quality assured.