ADXL345 3-Axis Accelerometer

Complete Tutorial: Motion Detection, Tilt Sensing & Tap Detection

📖 12 min read 🔧 Beginner Friendly 💻 Arduino / ESP32

1. Introduction

The ADXL345 is a small, thin, ultra-low power 3-axis accelerometer made by Analog Devices. It measures static acceleration (like gravity for tilt sensing) and dynamic acceleration (from motion, shock, or vibration).

What makes it special is its built-in motion detection features: single tap, double tap, free-fall detection, and activity/inactivity monitoring—all handled by the sensor itself!

📦 Key Features

  • High resolution: 13-bit, up to ±16g range
  • Ultra-low power: 23µA in measurement mode
  • Built-in tap and free-fall detection
  • Dual I2C and SPI interface

2. Technical Specifications

Parameter Specification
Operating Voltage 2.0V – 3.6V (module: 3.3V – 5V)
Interface I2C (up to 400kHz) / SPI (up to 5MHz)
Measurement Range ±2g, ±4g, ±8g, ±16g (selectable)
Resolution 13-bit (full resolution mode)
I2C Address 0x53 (SDO/ALT low) or 0x1D (SDO/ALT high)
Output Data Rate 0.1Hz to 3200Hz
Current (Measurement) 23 µA typical

💡 ADXL345 vs MPU6050

ADXL345 is accelerometer-only (no gyroscope) but has better built-in event detection (tap, free-fall). MPU6050 adds a gyroscope but lacks these hardware features. Choose based on your needs!

3. Wiring Guide

ADXL345 Pin Description ESP32 Arduino Uno
VCC Power (3.3V-5V) 3.3V 5V
GND Ground GND GND
SCL I2C Clock GPIO 22 A5
SDA I2C Data GPIO 21 A4
CS SPI Chip Select VCC (for I2C) VCC (for I2C)
SDO Address Select GND (for 0x53) GND (for 0x53)
ADXL345 wiring with ESP32

Figure 1: ADXL345 I2C wiring with ESP32

⚠️ Important for I2C Mode

Connect the CS pin to VCC to enable I2C mode. Leaving it floating may cause communication issues.

4. Project: Acceleration & Tilt Monitor

Required Library

Install Adafruit ADXL345 and Adafruit Unified Sensor via Arduino Library Manager.

Basic Acceleration Reading

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

Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345);

void setup() {
    Serial.begin(115200);
    Serial.println("ADXL345 Accelerometer");

    if (!accel.begin()) {
        Serial.println("ERROR: ADXL345 not found!");
        while (1);
    }

    // Set measurement range
    accel.setRange(ADXL345_RANGE_16_G);

    Serial.println("ADXL345 Ready!");
}

void loop() {
    sensors_event_t event;
    accel.getEvent(&event);

    Serial.print("X: "); Serial.print(event.acceleration.x, 2);
    Serial.print(" Y: "); Serial.print(event.acceleration.y, 2);
    Serial.print(" Z: "); Serial.print(event.acceleration.z, 2);
    Serial.println(" m/s^2");

    delay(100);
}

Tilt Angle Calculation

void loop() {
    sensors_event_t event;
    accel.getEvent(&event);

    // Calculate tilt angles
    float pitch = atan2(event.acceleration.y, event.acceleration.z) * 180.0 / PI;
    float roll = atan2(-event.acceleration.x, 
                  sqrt(event.acceleration.y * event.acceleration.y + 
                       event.acceleration.z * event.acceleration.z)) * 180.0 / PI;

    Serial.print("Pitch: "); Serial.print(pitch, 1);
    Serial.print("° | Roll: "); Serial.print(roll, 1);
    Serial.println("°");

    delay(100);
}

5. Built-in Event Detection

The ADXL345 can detect these events in hardware:

Feature Description
Single Tap Detects a quick tap on any axis
Double Tap Detects two taps in quick succession
Free-Fall Detects when acceleration on all axes is near zero
Activity Detects movement above a threshold
Inactivity Detects lack of movement for a set time

6. Applications

  • Tilt sensing: Level indicators, inclinometers
  • Vibration monitoring: Machine health, earthquake detection
  • Drop detection: Package handling, device protection
  • Gesture control: Shake-to-activate features
  • Pedometers: Step counting in wearables

7. Troubleshooting

Sensor not detected

  • Check CS pin is tied to VCC for I2C mode
  • Verify SDO pin for correct address (GND=0x53, VCC=0x1D)

Z-axis always reads ~9.8

  • This is correct! Gravity causes ~1g on Z when horizontal

Readings are offset

  • Use calibration registers or apply software offset correction

8. Summary

  • 3-axis accelerometer with ±2g to ±16g range
  • Ultra-low power (23µA)
  • Built-in tap, double-tap, free-fall detection
  • I2C address: 0x53 (default) or 0x1D