Interfacing ADXL345 Digital Accelerometer Module with Arduino

ADXL345 Accelerometer Module Features

ADXL345 accelerometer sensor can measure angle orientation and object path. The sensor can do that in three X, Y and Z axes. It can also measure dynamic and static acceleration forces. Dynamic acceleration force is due to motion and vibration and static acceleration is caused by gravity. The sensitivity range of the ADXL345 accelerometer sensor is from +-2g to +-16g. (g is approximately 9.8) For example, when this sensor is placed on a flat surface, its value is +1g in the direction of the z axis and zero in the direction of the x and z axes.

This sensor uses both I2C and SPI communicaton protocols. It can be used in robotics and flying robots, navigation devices, game consoles, 3D remote controllers, etc.

ADXL345 Accelerometer Module

You can download the datasheet of this module here.  

ADXL345 Accelerometer Module Pinout

This sensor has 8 pins:

  • VCC: Module power supply – 3 to 6 V
  • GND: Ground
  • Cs: Chip Select
  • INT1: Interrupt 1 Out
  • INT2: Interrupt 2 Out
  • SDO: Serial Data Out
  • SDA: Serial Data
  • SCL: Serial Clock

You can see the pinout of this module in the image below. In this tutorial the two I2C pins have been used.

Required Materials

Hardware Components

Arduino UNO R3×1
ADXL345 Accelerometer Module×1
Male to Male jumper wire×1

Software Apps

Interfacing ADXL345 Accelerometer Module with Arduino

Step 1: Circuit

The following circuit shows how you should connect Arduino to ADXL345 sensor. Connect wires accordingly.

Step 2: Code

Upload the following code to Arduino.

/*
  ADXL345-Acceleration-Modole
  modified on 26 oct 2020
  by Amir Mohammad Shojaee @ Electropeak
  Home

  Based on howtomechatronics.com Example
*/

#include <Wire.h>  // Wire library - used for I2C communication
int ADXL345 = 0x53; // The ADXL345 sensor I2C address
float X_out, Y_out, Z_out;  // Outputs
void setup() {
  Serial.begin(9600); 
  Wire.begin(); // Initiate the Wire library
  Wire.beginTransmission(ADXL345); // Start communicating with the device 
  Wire.write(0x2D); // Access/ talk to POWER_CTL Register - 0x2D
  Wire.write(8); // (8dec -> 0000 1000 binary) Bit D3 High for measuring enable 
  Wire.endTransmission();
  delay(10);
}
void loop() {
  // === Read acceleromter data === //
  Wire.beginTransmission(ADXL345);
  Wire.write(0x32); // Start with register 0x32 (ACCEL_XOUT_H)
  Wire.endTransmission(false);
  Wire.requestFrom(ADXL345, 6, true); // Read 6 registers total, each axis value is stored in 2 registers
  X_out = ( Wire.read()| Wire.read() << 8); // X-axis value
  X_out = X_out/256; //For a range of +-2g, we need to divide the raw values by 256, according to the datasheet
  Y_out = ( Wire.read()| Wire.read() << 8); // Y-axis value
  Y_out = Y_out/256;
  Z_out = ( Wire.read()| Wire.read() << 8); // Z-axis value
  Z_out = Z_out/256;
  Serial.print("Xa= ");
  Serial.print(X_out);
  delay(500);
  Serial.print("   Ya= ");
  Serial.print(Y_out);
  delay(500);
  Serial.print("   Za= ");
  Serial.println(Z_out);
  delay(500);

}
Arduino

The library needed for I2C connection is called at the beginning of the code. In the next step, the I2C address is written. This address is listed in the datasheet. Next, we put it in the measurement position. This is done by addressing the registry and the desired bit. Each axis contains 2 registers. The operation of reading the registers starts from the x-axis and this process must be repeated 6 times. Then, according to the +-2g sensitivity mentioned in the datasheet, we divide it by 256. Finally, the output value of each axis is displayed in Serial Monitor.
By moving the sensor in different directions, the following output in Serial Monitor changes as shown below.