Extra 5% OFF Use Code: OL05
Free Shipping over ₹999

Project 25: ADXL345

Introduction

The ADXL345 is a 3-axis digital accelerometer that measures acceleration in the X, Y, and Z directions.
It communicates with Arduino via I2C or SPI, most commonly using I2C for simplicity.
This sensor is ideal for motion detection, tilt sensing, and vibration monitoring projects.
In this tutorial, we’ll connect the ADXL345 to an Arduino and read acceleration data over I2C.

Required Components

  • Arduino UNO
  • ADXL345
  • Jumper wires
  • Breadboard (optional)

Pinout

Circuit Diagram / Wiring

  • ADXL345 VCC → 5V (Arduino)
  • ADXL345 GND → GND (Arduino)
  • ADXL345 SDA → A4 (Arduino)
  • ADXL345 SCL → A5 (Arduino)

Arduino Code / Programming

#include <Wire.h>
 
#define ADXL345_ADDRESS (0x53) // ADXL345 I2C address
 
void setup() {
  Serial.begin(9600);
  Wire.begin(); // Initialize I2C communication
 
  // Configure ADXL345
  writeToADXL345(0x2D, 0x08); // Power on and enable measurement
}
 
void loop() {
  // Read accelerometer data
  int x = readFromADXL345(0x32); // X-axis data (8 bits)
  int y = readFromADXL345(0x34); // Y-axis data (8 bits)
  int z = readFromADXL345(0x36); // Z-axis data (8 bits)
 
  // Print accelerometer data
  Serial.print("X = ");
  Serial.print(x);
  Serial.print(", Y = ");
  Serial.print(y);
  Serial.print(", Z = ");
  Serial.println(z);
 
  delay(100); // Delay between readings
}
 
// Function to write to ADXL345 register
void writeToADXL345(byte regAddress, byte value) {
  Wire.beginTransmission(ADXL345_ADDRESS);
  Wire.write(regAddress);
  Wire.write(value);
  Wire.endTransmission();
}
 
// Function to read from ADXL345 register
int readFromADXL345(byte regAddress) {
  Wire.beginTransmission(ADXL345_ADDRESS);
  Wire.write(regAddress);
  Wire.endTransmission(false); // Restart transmission for reading
  Wire.requestFrom(ADXL345_ADDRESS, 1); // Request 1 byte of data
  if (Wire.available()) {
    return Wire.read();
  }
  return -1; // Return error if no data received
}

Explanation

  • The ADXL345 is initialized over I2C and configured to start measuring by writing 0x08 to register 0x2D.
  • The loop() function reads 8-bit acceleration data from X, Y, and Z axis registers (0x32, 0x34, 0x36).
  • The values are printed to the Serial Monitor every 100 ms for real-time observation.

Testing and Troubleshooting

  • Ensure proper I2C wiring between the ADXL345 and Arduino (VCC, GND, SDA, SCL).
  • If no data is showing, check if the ADXL345 address (0x53) is correct or adjust your I2C scanner.
  • If readings seem off, ensure the sensor is powered correctly and check your code for any register misconfigurations.

    Leave a Reply

    Your email address will not be published.

    Need Help?