Programming and Calibration

Install the Library:

Download the ADXL345 library and install it. You will also need the Adafruit Sensor Library if you do not already have it installed.

This guide will help you with the install process.

Test:

Click "File->Examples->Adafruit_ADXL345->sensortest" to load the example sketch from the library.

Then click on the compile/upload button to compile and upload the sketch to the Arduino. You should see output similar to below. Watch the values change as you move the board around.

Calibrate:

The ADXL chips are calibrated at the factory to a level of precision sufficient for most purposes. For critical applications where a higher degree of accuracy is required, you may wish to re-calibrate the sensor yourself.

Calibration does not change the sensor outputs. But it tells you what the sensor output is for a known stable reference force in both directions on each axis. Knowing that, you can calculate the corrected output from a sensor reading.

Gravity as a Calibration Reference

Acceleration can be measured in units of gravitational force or "G", where 1G represents the gravitational pull at the surface of the earth. Gravity is a relatively stable force and makes a convenient and reliable calibration reference for surface-dwelling earthlings.

Calibration Method:

To calibrate the sensor to the gravitational reference, you need to determine the sensor output for each axis when it is precisely aligned with the axis of gravitational pull. Laboratory quality calibration uses precision positioning jigs. The method described here is simple and gives surprisingly good results with just a block of wood.

Mount the Sensor:

FIrst mount the sensor securely to a block or a box. The size is not important, as long as all the sides are at right angles. The material is not important as long as it is fairly rigid.

Load the Calibration Sketch:

Load and run the Calibration sketch below. Open the Serial Monitor and wait for the prompt.

Position the Block:

Place the block on a firm flat surface such as a sturdy table. Type a character in the Serial Monitor and hit return. The sketch will take a measurement on that axis and print the results.

Reposition the Block:

Turn the block so a different side is flat on the table and type another key to measure that axis.

Repeat:

Repeat for all six sides of the block to measure the positive and negative aspects of each axis.

(Hint:)

For the sides obstructed by the breakout board and/or wires, press the block up against the bottom of the table while taking the reading.

Calibration Results:

Once all six sides have been sampled, the values printed in the Serial Monitor will represent actual measurements for +/- 1G forces on each axis. These values can be used to re-scale readings for better accuracy.

Calibration Sketch:

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345_U.h>

/* Assign a unique ID to this sensor at the same time */
Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345);


float AccelMinX = 0;
float AccelMaxX = 0;
float AccelMinY = 0;
float AccelMaxY = 0;
float AccelMinZ = 0;
float AccelMaxZ = 0;


void setup(void) 
{
  Serial.begin(9600);
  Serial.println("ADXL345 Accelerometer Calibration"); 
  Serial.println("");
  
  /* Initialise the sensor */
  if(!accel.begin())
  {
    /* There was a problem detecting the ADXL345 ... check your connections */
    Serial.println("Ooops, no ADXL345 detected ... Check your wiring!");
    while(1);
  }
}

void loop(void)
{
    Serial.println("Type key when ready..."); 
    while (!Serial.available()){}  // wait for a character
    
    /* Get a new sensor event */ 
    sensors_event_t accelEvent;  
    accel.getEvent(&accelEvent);
    
    if (accelEvent.acceleration.x < AccelMinX) AccelMinX = accelEvent.acceleration.x;
    if (accelEvent.acceleration.x > AccelMaxX) AccelMaxX = accelEvent.acceleration.x;
    
    if (accelEvent.acceleration.y < AccelMinY) AccelMinY = accelEvent.acceleration.y;
    if (accelEvent.acceleration.y > AccelMaxY) AccelMaxY = accelEvent.acceleration.y;
  
    if (accelEvent.acceleration.z < AccelMinZ) AccelMinZ = accelEvent.acceleration.z;
    if (accelEvent.acceleration.z > AccelMaxZ) AccelMaxZ = accelEvent.acceleration.z;
  
    Serial.print("Accel Minimums: "); Serial.print(AccelMinX); Serial.print("  ");Serial.print(AccelMinY); Serial.print("  "); Serial.print(AccelMinZ); Serial.println();
    Serial.print("Accel Maximums: "); Serial.print(AccelMaxX); Serial.print("  ");Serial.print(AccelMaxY); Serial.print("  "); Serial.print(AccelMaxZ); Serial.println();

    while (Serial.available())
    {
      Serial.read();  // clear the input buffer
    }
}

Typical Calibration Output:

ADXL345 Accelerometer Calibration

Type key when ready...
Accel Minimums: 0.00  0.00  0.00
Accel Maximums: 0.12  0.20  1.14
Type key when ready...
Accel Minimums: 0.00  0.00  0.00
Accel Maximums: 0.12  0.20  1.14
Type key when ready...
Accel Minimums: 0.00  0.00  0.00
Accel Maximums: 0.12  0.20  1.14
Type key when ready...
Accel Minimums: 0.00  0.00  0.00
Accel Maximums: 0.12  0.20  1.14
Type key when ready...
Accel Minimums: 0.00  0.00  -0.24
Accel Maximums: 0.12  1.37  1.14
Type key when ready...
Accel Minimums: 0.00  0.00  -0.24
Accel Maximums: 0.12  1.37  1.14
Type key when ready...
Accel Minimums: 0.00  -1.22  -0.27
Accel Maximums: 0.12  1.37  1.14
Type key when ready...
Accel Minimums: 0.00  -1.22  -0.27
Accel Maximums: 0.12  1.37  1.14
Type key when ready...
Accel Minimums: -1.18  -1.22  -0.27
Accel Maximums: 0.12  1.37  1.14
Type key when ready...
Last updated on 2015-05-04 at 04.27.27 PM Published on 2013-03-26 at 04.51.15 PM