MyRoboPathOpen Robotics Lab
Arduino Uno #28 of 35
Est. 0.75 Hours
Budget: $3 - $6
Intermediate

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.

Arduino Uno R3ADXL345 MEMSI2C Bus (0x53)Pitch & Roll Math3-Axis Gravity
ADXL345 3-Axis Digital MEMS Accelerometer & Tilt Sensor Blueprint
ADXL345 3-Axis Digital MEMS Accelerometer & Tilt SensorCAD Blueprint

Key Learning Objectives & Robotics Concepts

By building and programming this project, you will master the following core engineering skills:

1Understand MEMS capacitive acceleration sensing and why a stationary flat sensor reads +1.00g on Z.
2Configure ADXL345 POWER_CTL (0x2D) and DATA_FORMAT (0x31) registers directly via Wire.h.
3Read 6 consecutive little-endian bytes (0x32–0x37) atomically to avoid torn X/Y/Z frames.
4Compute Pitch and Roll angles cleanly using atan2() trigonometry.

Power Architecture & Voltage Staging

5V / 3.3V Regulated Power

The 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.

ADXL345 3-Axis Digital MEMS Accelerometer & Tilt Sensor Power Architecture Schematic
Figure: Regulated Arduino Uno supply rails, current budget, and component protection.Power Schematic

Bill of Materials (BOM) & Hardware Components

Bill of Materials for ADXL345 3-Axis Digital MEMS Accelerometer & Tilt Sensor
ComponentSpecifications & AlternativeQtyApprox Cost
Arduino Uno R3ATmega328P @ 16MHz1$8.00
ADXL345 3-Axis Accelerometer (GY-291)±2g/±4g/±8g/±16g I2C/SPI MEMS sensor1$3.00
Breadboard & 6× Jumper WiresVCC, GND, SDA, SCL, CS, SDO leads1$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).

Figure: Complete Arduino Uno R3 pin-to-pin wiring schematic for ADXL345 3-Axis Digital MEMS Accelerometer & Tilt Sensor.
Figure: Complete Arduino Uno R3 pin-to-pin wiring schematic for ADXL345 3-Axis Digital MEMS Accelerometer & Tilt Sensor.Wiring Schematic

Autonomous Behavior & Finite State Machine (FSM)

Deterministic Arduino loop handling GY-291 ADXL345 3-Axis I2C Accelerometer acquisition, filtering, and real-time output updates.

Figure: Firmware execution flow and state transitions for ADXL345 3-Axis Digital MEMS Accelerometer & Tilt Sensor.
Figure: Firmware execution flow and state transitions for ADXL345 3-Axis Digital MEMS Accelerometer & Tilt Sensor.Control Algorithm

Step-by-Step Physical Assembly & Wiring Tutorial

Follow each of the 5 sequential milestone stages with technical photos, wiring checks, and pre-flight tests.

1

Inspect Axis Markings & I2C Mode Pins

Duration: 5 Mins

Look at the X and Y arrows printed on the ADXL345 PCB and verify CS/SDO configuration.

ADXL345 3-Axis Digital MEMS Accelerometer & Tilt Sensor hardware layout diagram
Stage 1: GY-291 ADXL345 3-Axis I2C Accelerometer hardware inspection & pinout overview.Stage 1 Diagram
Step Action Checklist:
  • 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.
Engineering & Tuning Tip: Using 3.3V on VCC is safe for every ADXL345 breakout variant (both bare 3.3V and 5V-LDO GY-291 boards).
2

Wire ADXL345 I2C Bus to A4 & A5

Duration: 5 Mins

Connect power, ground, mode-select ties, and hardware I2C lines.

ADXL345 3-Axis Digital MEMS Accelerometer & Tilt Sensor breadboard wiring check diagram
Stage 2: Breadboard layout & Arduino Uno pin-to-pin wiring audit.Stage 2 Diagram
Step Action Checklist:
  • Wire VCC → 3.3V and CS → 3.3V.
  • Wire GND → GND and SDO → GND.
  • Wire SDA → A4 and SCL → A5.
Engineering & Tuning Tip: Leave INT1 and INT2 unconnected for polling mode (connect to D2 later for hardware tap/freefall interrupts).
3

Verify Hardware Device ID (0xE5)

Duration: 5 Mins

Run the DEVID diagnostic to confirm Arduino communicates cleanly with the ADXL345 silicon.

ADXL345 3-Axis Digital MEMS Accelerometer & Tilt Sensor power rail protection diagram
Stage 3: Supply rail check (3.3V Regulated Rail (5V tolerant on GY-291 LDO)) and circuit protection verification.Stage 3 Diagram
Step Action Checklist:
  • 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.
Engineering & Tuning Tip: Reading register 0x00 always returns 0xE5 on genuine ADXL345 chips—an instant hardware health proof!
4

Upload 3-Axis Gravity & Pitch/Roll Firmware

Duration: 10 Mins

Flash the direct Wire.h driver (no external libraries needed!) and test live tilt angles.

ADXL345 3-Axis Digital MEMS Accelerometer & Tilt Sensor serial telemetry verification diagram
Stage 4: Firmware upload & 115200 baud Serial Monitor verification.Stage 4 Diagram
Step Action Checklist:
  • 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.
Engineering & Tuning Tip: In ±2g range, sensitivity is 256 LSB/g (~3.9 mg per bit), giving crisp 0.25° static tilt resolution!
5

Test Dynamic Shake & Free-Fall Vector Magnitude

Duration: 5 Mins

Observe total acceleration vector |a| = sqrt(x² + y² + z²) jump above 1.5g during a brisk shake.

ADXL345 3-Axis Digital MEMS Accelerometer & Tilt Sensor live operational logic diagram
Stage 5: Live operational logic flow & calibration verification.Stage 5 Diagram
Step Action Checklist:
  • 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.
Engineering & Tuning Tip: Combining an accelerometer with a gyroscope via a Complementary or Kalman filter is the foundation of self-balancing robots and drones!

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 Diagnostic

Minimal standalone sketch to verify pin wiring and signal response for GY-291 ADXL345 3-Axis I2C Accelerometer before running the full project firmware.

Arduino IDE Diagnostic ScriptC++ / Arduino
// 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() {}
Expected Output: ADXL345 DEVID = 0xE5

Complete Production Firmware

Full working C++ source code with pin mappings, non-blocking timers, and FSM avoidance routines.

arduino_uno_adxl345_3axis_accelerometer.ino
arduino_uno_adxl345_3axis_accelerometer.inoArduino C++
/**
 * 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

1Double-check all 4 Arduino Uno pin connections against the wiring schematic before plugging in USB.
2Upload the Bench Diagnostic sketch first and confirm Serial Monitor at 115200 baud matches: ADXL345 DEVID = 0xE5
3Upload the complete production sketch and verify every condition in the operational truth table below.

Input Signal vs. System Output Truth Table:

Operational truth table for ADXL345 3-Axis Digital MEMS Accelerometer & Tilt Sensor
Input / StatePin / SignalDetected ConditionExpected Output
Flat HorizontalZ ≈ +1.00gLevel SurfacePitch ≈ 0°, Roll ≈ 0°
Tilt +45° RightY ≈ +0.71g, Z ≈ 0.71gRoll InclinationRoll ≈ +45.0°
Upside DownZ ≈ -1.00gInverted BoardRoll ≈ ±180.0°

Engineering Troubleshooting Matrix

Engineering troubleshooting matrix for ADXL345 3-Axis Digital MEMS Accelerometer & Tilt Sensor
ProblemLikely CauseDiagnostic CheckEngineering Solution
Prints all 0.00g continuouslyCS pin floating or POWER_CTL measure bit not setRun DEVID 0xE5 diagnosticTie CS firmly to 3.3V and SDO to GND.
DEVID returns 0x00 or hangsSDA/SCL swapped or address is 0x1D (SDO tied HIGH)Run I2C scanner from Project 4Check A4/A5 wiring and confirm SDO→GND for 0x53.
Values read ±10g while sitting stillUnsigned byte shift bug or wrong range scaleVerify int16_t cast on rawX/rawY/rawZUse 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:

Level 1Digital Bubble Level on OLED

Draw a moving target bubble on Project 4 OLED that centers when Pitch & Roll equal 0°.

Level 2Tilt-Controlled Servo Horizon

Drive Project 8 SG90 servo to stay mechanically horizontal as you tilt the breadboard!

Level 3Pedometer Step Counter

Detect walking strides by counting peaks where total g-magnitude exceeds 1.35g.