Source: https://mokxi.com/libraries/adafruit-adxl345
Updated: 2026-11-17

Libraries / Sensors

Arduino library · Sensors

# Adafruit ADXL345: a three-axis accelerometer on I2C

Yes. You can use the Adafruit ADXL345 library online in Mokxi with nothing to install, and the sketch runs in your browser against the simulated part (Accelerometer, ADXL345). The example below, "Tilt level", runs on the Arduino Uno R3.

Run the example in Mokxi See the calls

Built in on Mokxi: #include <Adafruit_ADXL345_U.h> and it builds, with nothing to install. Free, and no account needed.

## What Adafruit ADXL345 does

The ADXL345 is Analog Devices’ three-axis accelerometer, the chip on the GY-291 board. It measures up to ±16 g and has no gyroscope, so it suits tilt, shake and orientation rather than tracking rotation. Adafruit’s driver is a Unified Sensor library: readings come back in a sensors_event_t, in meters per second squared.

The chip powers up in standby and measures nothing until it is told to. begin() checks the device ID, 0xE5, and switches measuring on. setRange() chooses ±2, 4, 8 or 16 g, and this driver turns on full resolution at the same time, so one count stays at about 3.9 mg on every range. setDataRate() sets how often it samples, from 0.10 Hz up to 3200 Hz.

At rest the sensor feels only gravity, about 9.8 m/s^2. Lying flat that is all on Z; tipped, part of it moves onto X or Y, and the share tells how far. The tilt level below uses the raw counts on X and Y to say which way to lean the board. In Mokxi the adxl345 part is read over I2C at 0x53 and you drag it to tip it. This driver has no SPI constructor and no tap or free-fall calls.

Library Manager name | Adafruit ADXL345

Author | Adafruit

Include | #include <Adafruit_ADXL345_U.h>

Boards | every board Mokxi compiles for but ATtiny85

In the simulator | Simulated: it runs against the part the way it does on the bench.

Parts it drives | Accelerometer, ADXL345

## Install it

It builds on every board Mokxi compiles for but ATtiny85.

In Mokxi it is built in: #include <Adafruit_ADXL345_U.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "Adafruit ADXL345" by Adafruit from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add Adafruit ADXL345 to lib_deps in PlatformIO. Mokxi follows the calls of release 1.3.4, so a sketch written here builds there too.

- ESP32-C3
- Arduino Uno
- Arduino Nano
- Arduino Mega 2560
- Arduino Leonardo
- SparkFun Pro Micro
- Arduino Micro
- ESP32-C6
- WCH CH32V003
- Raspberry Pi Pico
- Raspberry Pi Pico W
- Seeed XIAO SAMD21
- STM32
- STM32 Nucleo-F411RE
- STM32 Blue Pill
- micro:bit V2
- Arduino Uno R4
- ESP32 DevKit
- ESP32-S3-DevKitC-1
- ESP8266 NodeMCU
- Arduino Pro Mini 3.3 V
- Seeed XIAO ESP32-C3
- ESP32-C3 SuperMini
- Seeed XIAO ESP32-C6
- ESP32-DevKitC V4
- LOLIN32 Lite
- Wemos D1 R32
- Cheap Yellow Display
- TTGO T-Display
- Seeed XIAO ESP32-S3
- Arduino Nano ESP32
- Arduino Nano RP2040 Connect
- Seeed XIAO RP2040
- Waveshare RP2040-Zero
- Arduino Uno R4 WiFi

## The calls you will use

In the order a sketch meets them.

### Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345)

Creates the driver. The number is a sensor ID copied into each event, only useful when logging several sensors.

### accel.begin()

Looks for an ADXL345 at 0x53 and starts it measuring. Returns false when the ID is wrong or nothing answers; begin(0x1D) is for a board with SDO high.

### accel.setRange(ADXL345_RANGE_16_G)

Sets ±16 g. ADXL345_RANGE_2_G is enough for a level; getRange() reads the setting back.

### accel.setDataRate(ADXL345_DATARATE_100_HZ)

Sets the chip’s sampling rate. getDataRate() returns the current one.

### accel.getEvent(&event)

All three axes into event.acceleration.x, y and z in m/s^2, read in one transfer. Returns false if the read failed.

### accel.getX()

The raw signed count on X, 256 to a g at full resolution. getY() and getZ() give the other axes; whole-number counts are cheaper than floats on an Uno.

### accel.getSensor(&sensor)

Fills a sensor_t: the name ADXL345, a range of -156.9 to 156.9 m/s^2 and a step of 0.0392 m/s^2.

## Example: Tilt level

On the Arduino Uno R3. It runs in the browser exactly as printed.

sketch.ino · Arduino Uno R3

Open it in the editor, wired and ready to run · Tilt level: the project page

## Common errors and how to fix them

### No ADXL345 detected

begin() did not read 0xE5 from the ID register. Check the bus wires; on a GY-291, CS must sit high for I2C and SDO low for 0x53. With SDO high, call begin(0x1D) instead.

### An angle worked out from getX() is far too large

getX() returns raw counts, not m/s^2 or g. Divide by 256 to get g first, or use getEvent(), which already scales every axis to m/s^2.

### Every axis reads 0

The chip was never switched from standby to measuring, usually because begin() was skipped or its false result ignored. Call begin() once in setup() and stop the sketch when it fails.

## Questions and answers

### How does the tilt level decide what is level?

It calls it level when the raw X and Y counts are both within 13, which is half a meter per second squared at 3.9 mg a count, about three degrees of tilt.

### Should I pick the ADXL345 or an MPU6050?

For a level or an orientation check the ADXL345 is enough. Angles that must stay right while the board moves need a gyroscope, which the MPU6050 adds.

## Keep going

### Learn

- Arduino ADXL345: Library, Example Code and Tilt
- I2C Scanner for Arduino and ESP32, and Fixes

### Parts

- Accelerometer, ADXL345

### More sensors libraries

- DHT sensor library
- DHT20
- VL53L0X
- Adafruit_VL53L0X
- HX711 Arduino Library
- MPU6050
- Adafruit MPU6050
- BME280
- MD_TCS230
- SparkFun BME280
- MCP4725
- DHT sensor library for ESPx
- SparkFun BMP180 (SFE_BMP180)
- ADS1115_WE

Every built-in library · Open the editor

## Try Adafruit ADXL345 without installing anything

Open the example, change the sketch, and keep your version in a free account.

Start building free

Open the editor
