Light, distance and motion

ADXL345 accelerometer with ESP32

The ADXL345 has one job an MPU6050 does not do well: built-in tap and free-fall detection, with interrupts. It can wake a screen on a knock or flag a dropped device without you writing the maths. It measures up to ±16 g with 13-bit resolution and keeps about 4 mg per step at every range.

Set the address deliberately. With SDO low it is 0x53, and with SDO high it is 0x1D. Adafruit's board pulls SDO down, but on some boards the pin floats, and a floating pin gives an address that may change, so tie it. CS must be high for I2C, because low selects SPI. Many cheap boards handle that themselves, while some do not. If the scan finds nothing, check CS first.

Tilt angle is easy to work out because gravity shows up on the axes. Vibration measurements need more care: mount the sensor rigidly, because a loose board measures its own wobble. It gives acceleration only and has no gyro, so a fast-spinning or moving object cannot be tracked. For that, use an MPU6050. Read the device ID at start-up: register 0 returns 0xE5, a quick test that wiring and address are right. The example reads it, sets full resolution at ±16 g, and prints three axes in g. An Arduino user suggests 0.3 to 0.6 g and 0.1 s as a starting point for free fall.

ADXL345 accelerometer wiring diagram for ESP32
Wiring diagram drawn by LCD Ultra Pro.

Specifications

RangeUp to ±16 g
Resolution13-bit, about 4 mg per step
InterfaceI2C or SPI
I2C address0x53 (SDO low), 0x1D (SDO high)
Built-in featuresTap, double tap, free fall
Device ID0xE5

Power and logic levels

3.3 V: safe to connect directly

SupplyChip 2–3.6 V, some modules have a regulator
Signal level3.3 V
On an ESP32Run it from 3V3, so the I2C lines stay at 3.3 V.

ESP32 pins, including those on a display board's connectors, work at 3.3 V and are not 5 V tolerant. Modules differ, so if in doubt, measure the signal before you connect it.

Wiring

ADXL345 accelerometerConnect toNote
VIN / VCC3V3
GNDGND
SDAGPIO 8Any free pin, set in Wire.begin()
SCLGPIO 9Any free pin, set in Wire.begin()
CS3V3High selects I2C
SDOGNDAddress 0x53

Arduino code

#include <Wire.h>

const int SDA_PIN = 8, SCL_PIN = 9;
const uint8_t ADDR = 0x53;

void writeReg(uint8_t reg, uint8_t value) {
  Wire.beginTransmission(ADDR);
  Wire.write(reg); Wire.write(value);
  Wire.endTransmission();
}

void setup() {
  Serial.begin(115200);
  Wire.begin(SDA_PIN, SCL_PIN);
  Wire.beginTransmission(ADDR);
  Wire.write(0x00);
  Wire.endTransmission();
  Wire.requestFrom(ADDR, (uint8_t)1);
  Serial.printf("Device ID 0x%02X\n", Wire.read());   // expect 0xE5
  writeReg(0x31, 0x0B);               // full resolution, +/-16 g
  writeReg(0x2D, 0x08);               // measure
}

void loop() {
  Wire.beginTransmission(ADDR);
  Wire.write(0x32);
  Wire.endTransmission();
  Wire.requestFrom(ADDR, (uint8_t)6);
  int16_t v[3];
  for (int i = 0; i < 3; i++) {       // low byte first
    uint8_t lo = Wire.read();
    v[i] = lo | (Wire.read() << 8);
  }
  Serial.printf("x %.2f  y %.2f  z %.2f g\n", v[0] * 0.004, v[1] * 0.004, v[2] * 0.004);
  delay(200);
}

No extra library needed. Compiled for ESP32-S3 with arduino-esp32 3.x. Not yet tested by us on a physical sensor. Change the pins to match your board.

On a display

Read the value in your sketch and pass it to a label, or send it over MQTT and show it with an MQTT value in the editor.

Design a display for ADXL345 accelerometer

Boards with a ready connector

Only boards whose connectors we have surveyed. Others may still work on free GPIOs.

Elecrow CrowPanel ESP32 HMI 2.4"I2C port: SDA 22, SCL 21Not shared with the touch controller (it is on SPI) — free for sensors
Elecrow CrowPanel ESP32 HMI 2.8"I2C port: SDA 22, SCL 21Not shared with the touch controller (it is on SPI) — free for sensors
Elecrow CrowPanel ESP32 HMI 3.5"I2C port: SDA 22, SCL 21Not shared with the touch controller (it is on SPI) — free for sensors
Elecrow CrowPanel ESP32-S3 HMI 5.0"I2C port: SDA 19, SCL 20Same I2C bus as the GT911 touch (0x14 / 0x5D) and, on V3.0 boards, the PCA9557 touch-reset expander (0x18)
Elecrow CrowPanel ESP32-S3 HMI 7.0"I2C port: SDA 19, SCL 20Same I2C bus as the GT911 touch (0x14 / 0x5D) and, on V3.0 boards, the PCA9557 touch-reset expander (0x18)
LilyGO T-Display-S3-Pro 2.33"QWIIC RIGHT: SDA 5, SCL 6I2C only — same bus as the touch, the PMU (SY6970) and the light sensor
M5Stack AtomS3 0.85"Bottom I2C pins: SDA 38, SCL 39Same I2C bus as the built-in IMU (MPU6886, 0x68)
Makerfabs MaTouch ESP32-S3 Rotary IPS 2.1 RoundMabee I2C (J1): SDA 17, SCL 18Same I2C bus as the touch controller (CST826)
Makerfabs MaTouch ESP32-S3 Parallel TFT 4.3"Mabee I2C: SDA 17, SCL 18Same I2C bus as the touch controller (GT911)
Makerfabs MaTouch ESP32-S3 Parallel TFT with Touch 5.0"Mabee I2C: SDA 17, SCL 18Same I2C bus as the touch controller (GT911)
Makerfabs MaTouch ESP32-S3 Parallel TFT with Touch 7.0"Mabee I2C: SDA 17, SCL 18Same I2C bus as the touch controller (GT911)
Sunton ESP32-4827S043R/C 4.3"I2C header (P3): SDA 19, SCL 20Same I2C bus as the GT911 touch controller. P3 also carries GPIO 17 / 18.
Sunton ESP32-8048S043C 4.3"I2C header (P3): SDA 19, SCL 20Same I2C bus as the GT911 touch controller. P3 also carries GPIO 17 / 18.
Sunton ESP32-8048S050C 5.0"I2C header (P3): SDA 19, SCL 20Same I2C bus as the GT911 touch controller. P3 also carries GPIO 17 / 18.
Sunton ESP32-8048S070C 7.0"I2C header (P3): SDA 19, SCL 20Same I2C bus as the GT911 touch controller. P3 also carries GPIO 17 / 18.
Waveshare ESP32-S3 Touch LCD 2.1" roundI2C connector: SDA 15, SCL 7Same I2C bus as the touch controller, the IO expander, the RTC and the IMU (addresses 0x15/0x5D, 0x20, 0x51, 0x6B, 0x7E taken)
Waveshare ESP32-S3 Touch LCD 2.8"I2C connector: SDA 11, SCL 10Same I2C bus as the RTC and the IMU (0x51, 0x6B, 0x7E taken) — the touch has its own bus
Waveshare ESP32-S3 Touch LCD 2.8" Type BI2C connector: SDA 15, SCL 7Same I2C bus as the touch controller, the IO expander, the RTC and the IMU (addresses 0x15/0x5D, 0x20, 0x51, 0x6B, 0x7E taken)
Waveshare ESP32-S3-Touch-LCD-2.8CI2C connector: SDA 15, SCL 7Same I2C bus as the touch controller, the IO expander, the RTC and the IMU (addresses 0x15/0x5D, 0x20, 0x51, 0x6B, 0x7E taken)
Waveshare ESP32-S3-LCD-7BI2C connector (H1): SDA 8, SCL 9Same I2C bus as the touch controller (GT911) and the IO expander (CH422G)

Good to know

  • Do not leave SDO floating.
  • CS must be high for I2C.
  • It has no gyro.
  • Mount it rigidly for vibration work.

What it is good for

  • Wake on tap
  • Tilt displays
  • Drop and shock detection

FAQ

Why does my I2C scan not find it?

Check that CS is high and SDO is tied to a level.

ADXL345 or MPU6050?

The ADXL345 has built-in tap and free-fall detection. The MPU6050 adds a gyro.

How do I check that it works?

Read register 0. It should return 0xE5.

Sources

Related parts

Use this information at your own risk. It is for general guidance only: the example code compiles, but it has not been tested on every part, and modules differ between sellers. Check the manufacturer's data before you build. Product names belong to their owners, and LCD Ultra Pro is not affiliated with them. See our Terms.