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.
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.
sensors_event_t event;
accel.getEvent(&event);
Serial.println(event.acceleration.z);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.
// Tilt level: an ADXL345 accelerometer on I2C, read with the Adafruit
// unified-sensor library, as a spirit level that says which way to tip.
//
// ADXL345 (GY-291) SDA -> A4 SCL -> A5 VCC -> 5 V GND -> GND
// CS, SDO, INT1 and INT2 left as the board pulls them
//
// An accelerometer at rest measures gravity: 9.8 m/s^2 straight down. Lying
// flat that is all on Z; tipped, some of it moves onto X or Y, and the share
// says how far. The sketch prints the three axes in m/s^2 and the raw counts
// on X (256 a g at full resolution), and calls it level when X and Y are both
// under half a meter per second squared, about three degrees.
//
// The chip starts in standby and does not measure until it is told to:
// begin() does that, and setRange() picks 16 g at full resolution.
//
// Drag the board to tip it.
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345_U.h>
Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345);
void setup() {
Serial.begin(9600);
if (!accel.begin()) {
Serial.println("No ADXL345 detected");
while (1) delay(10);
}
accel.setRange(ADXL345_RANGE_16_G);
Serial.println("Tilt level: ADXL345 ready");
}
void loop() {
sensors_event_t event;
accel.getEvent(&event);
int16_t rawX = accel.getX();
Serial.print("X: ");
Serial.print(event.acceleration.x);
Serial.print(" Y: ");
Serial.print(event.acceleration.y);
Serial.print(" Z: ");
Serial.print(event.acceleration.z);
Serial.print(" m/s^2 raw X ");
Serial.print(rawX);
// Half a m/s^2 is 13 counts at 3.9 mg a count.
int16_t rawY = accel.getY();
if (rawX > 13) {
Serial.println(" tip it nose up");
} else if (rawX < -13) {
Serial.println(" tip it nose down");
} else if (rawY > 13 || rawY < -13) {
Serial.println(" roll it level");
} else {
Serial.println(" level");
}
delay(300);
}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
Try Adafruit ADXL345 without installing anything
Open the example, change the sketch, and keep your version in a free account.