Complete Tutorial: Motion Detection, Tilt Sensing & Tap Detection
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!
| 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 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!
| 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) |
Figure 1: ADXL345 I2C wiring with ESP32
Connect the CS pin to VCC to enable I2C mode. Leaving it floating may cause communication issues.
Install Adafruit ADXL345 and Adafruit Unified Sensor via Arduino Library Manager.
#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);
}
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);
}
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 |