ADXL345 3-Axis Digital MEMS Accelerometer & Tilt Sensor
Measure 3D linear acceleration (±2g to ±16g) over I2C to compute real-time pitch and roll inclination angles.
Give your Arduino spatial gravity awareness with the ADXL345 3-axis MEMS digital accelerometer! Read 10-bit X, Y, and Z acceleration registers directly over hardware I2C (address 0x53) without bloated dependencies, convert raw LSBs to g-forces, and calculate real-time pitch and roll tilt angles.
Key Learning Objectives & Robotics Concepts
By building and programming this project, you will master the following core engineering skills:
Power Architecture & Voltage Staging
5V / 3.3V Regulated PowerThe ultra-low-power MEMS ADXL345 draws just 140µA while measuring at 100Hz. Power from Arduino 3.3V (safe for both bare breakouts and GY-291 boards with LDOs). Power bare ADXL345 boards strictly from 3.3V (GY-291 modules include an onboard 3.3V LDO on VCC); tie CS HIGH (to VCC) to select I2C mode cleanly.
Bill of Materials (BOM) & Hardware Components
| Component | Specifications & Alternative | Qty | Approx Cost |
|---|---|---|---|
| Arduino Uno R3 | ATmega328P @ 16MHz | 1 | $8.00 |
| ADXL345 3-Axis Accelerometer (GY-291) | ±2g/±4g/±8g/±16g I2C/SPI MEMS sensor | 1 | $3.00 |
| Breadboard & 6× Jumper Wires | VCC, GND, SDA, SCL, CS, SDO leads | 1 | $2.00 |
Electrical & System Architecture
Connect ADXL345 VCC to 3.3V, GND to GND, SDA to A4, SCL to A5, CS to 3.3V (forces I2C mode), and SDO to GND (locks I2C address 0x53).
Autonomous Behavior & Finite State Machine (FSM)
Deterministic Arduino loop handling GY-291 ADXL345 3-Axis I2C Accelerometer acquisition, filtering, and real-time output updates.
Step-by-Step Physical Assembly & Wiring Tutorial
Follow each of the 5 sequential milestone stages with technical photos, wiring checks, and pre-flight tests.
Inspect Axis Markings & I2C Mode Pins
Look at the X and Y arrows printed on the ADXL345 PCB and verify CS/SDO configuration.
- Locate the 8-pin header: GND, VCC, CS, INT1, INT2, SDO, SDA, SCL.
- Note that tying CS to 3.3V guarantees I2C mode, and tying SDO to GND locks I2C address 0x53.
- Seat the module flat and level on the breadboard.
Wire ADXL345 I2C Bus to A4 & A5
Connect power, ground, mode-select ties, and hardware I2C lines.
- Wire VCC → 3.3V and CS → 3.3V.
- Wire GND → GND and SDO → GND.
- Wire SDA → A4 and SCL → A5.
Verify Hardware Device ID (0xE5)
Run the DEVID diagnostic to confirm Arduino communicates cleanly with the ADXL345 silicon.
- Upload the diagnostic snippet and open Serial Monitor.
- Confirm it prints "ADXL345 DEVID = 0xE5 (OK!)".
- If 0x00 or 0xFF prints, check CS→3.3V, SDO→GND, and SDA/SCL.
Upload 3-Axis Gravity & Pitch/Roll Firmware
Flash the direct Wire.h driver (no external libraries needed!) and test live tilt angles.
- Upload the production sketch below and open Serial Monitor at 115200 baud.
- Lay breadboard flat on desk: verify X ≈ 0.00g, Y ≈ 0.00g, Z ≈ +1.00g, Pitch ≈ 0°, Roll ≈ 0°.
- Tilt breadboard 45° forward/back and left/right to verify live Pitch and Roll tracking.
Test Dynamic Shake & Free-Fall Vector Magnitude
Observe total acceleration vector |a| = sqrt(x² + y² + z²) jump above 1.5g during a brisk shake.
- Confirm resting vector magnitude stays cleanly near 1.00g regardless of board orientation.
- Shake the breadboard gently and watch g-force spike live in Serial Plotter.
- Note why accelerometers measure tilt smoothly when stationary, while gyros track fast rotations.
Subsystem Benchtop Verification Scripts
Test each hardware subsystem independently before deploying the full autonomous firmware.
1Bench Diagnostic: GY-291 ADXL345 3-Axis I2C Accelerometer Hardware Check
Subsystem DiagnosticMinimal standalone sketch to verify pin wiring and signal response for GY-291 ADXL345 3-Axis I2C Accelerometer before running the full project firmware.
// Diagnostic: Read ADXL345 Silicon ID Register (0x00 -> 0xE5)
#include <Wire.h>
void setup() {
Wire.begin(); Serial.begin(115200);
Wire.beginTransmission(0x53); Wire.write(0x00); Wire.endTransmission(false);
Wire.requestFrom(0x53, 1);
uint8_t id = Wire.available() ? Wire.read() : 0;
Serial.print("ADXL345 DEVID = 0x"); Serial.println(id, HEX);
}
void loop() {}Complete Production Firmware
Full working C++ source code with pin mappings, non-blocking timers, and FSM avoidance routines.
/**
* Project 28: ADXL345 3-Axis Accelerometer & Pitch/Roll Inclinometer
* Board: Arduino Uno R3 | I2C: SDA=A4, SCL=A5, CS=3.3V, SDO=GND (Addr 0x53)
*/
#include <Wire.h>
#include <math.h>
const uint8_t ADXL_ADDR = 0x53;
void writeReg(uint8_t reg, uint8_t val) {
Wire.beginTransmission(ADXL_ADDR);
Wire.write(reg);
Wire.write(val);
Wire.endTransmission();
}
void setup() {
Serial.begin(115200);
Wire.begin();
writeReg(0x2D, 0x08); // POWER_CTL: Enable measurement mode
writeReg(0x31, 0x00); // DATA_FORMAT: ±2g (256 LSB/g)
Serial.println(F("ADXL345 3-Axis Inclinometer Ready"));
}
void loop() {
Wire.beginTransmission(ADXL_ADDR);
Wire.write(0x32); // DATAX0 start register
Wire.endTransmission(false);
Wire.requestFrom(ADXL_ADDR, (uint8_t)6, (uint8_t)true);
if (Wire.available() == 6) {
int16_t rawX = (int16_t)(Wire.read() | (Wire.read() << 8));
int16_t rawY = (int16_t)(Wire.read() | (Wire.read() << 8));
int16_t rawZ = (int16_t)(Wire.read() | (Wire.read() << 8));
float gx = rawX / 256.0f;
float gy = rawY / 256.0f;
float gz = rawZ / 256.0f;
float roll = atan2(gy, gz) * 180.0f / PI;
float pitch = atan2(-gx, sqrt(gy * gy + gz * gz)) * 180.0f / PI;
Serial.print(F("X:")); Serial.print(gx, 2);
Serial.print(F("g Y:")); Serial.print(gy, 2);
Serial.print(F("g Z:")); Serial.print(gz, 2);
Serial.print(F("g | Roll: ")); Serial.print(roll, 1);
Serial.print(F(" deg | Pitch: ")); Serial.print(pitch, 1);
Serial.println(F(" deg"));
}
delay(150);
}Firmware Architecture Explanation:
- Communicates directly with ADXL345 registers via built-in Wire.h with zero external libraries.
- Reads all 6 bytes (X0..Z1) in a single multi-byte I2C burst so axes always belong to the exact same instant.
- Computes singularity-safe Pitch and Roll tilt angles using atan2() in degrees.
Operational Verification & Pin State Matrix
Input Signal vs. System Output Truth Table:
| Input / State | Pin / Signal | Detected Condition | Expected Output |
|---|---|---|---|
| Flat Horizontal | Z ≈ +1.00g | Level Surface | Pitch ≈ 0°, Roll ≈ 0° |
| Tilt +45° Right | Y ≈ +0.71g, Z ≈ 0.71g | Roll Inclination | Roll ≈ +45.0° |
| Upside Down | Z ≈ -1.00g | Inverted Board | Roll ≈ ±180.0° |
Engineering Troubleshooting Matrix
| Problem | Likely Cause | Diagnostic Check | Engineering Solution |
|---|---|---|---|
| Prints all 0.00g continuously | CS pin floating or POWER_CTL measure bit not set | Run DEVID 0xE5 diagnostic | Tie CS firmly to 3.3V and SDO to GND. |
| DEVID returns 0x00 or hangs | SDA/SCL swapped or address is 0x1D (SDO tied HIGH) | Run I2C scanner from Project 4 | Check A4/A5 wiring and confirm SDO→GND for 0x53. |
| Values read ±10g while sitting still | Unsigned byte shift bug or wrong range scale | Verify int16_t cast on rawX/rawY/rawZ | Use signed int16_t and 256.0f divisor for ±2g mode. |
Engineering Challenges & Upgrades
Take your build to the next level with these progressive engineering upgrades:
Draw a moving target bubble on Project 4 OLED that centers when Pitch & Roll equal 0°.
Drive Project 8 SG90 servo to stay mechanically horizontal as you tilt the breadboard!
Detect walking strides by counting peaks where total g-magnitude exceeds 1.35g.