Contact Sales & After-Sales Service

Contact & Quotation

  • Inquire: Call 0086-755-23203480, or reach out via the form below/your sales contact to discuss our design, manufacturing, and assembly capabilities.
  • Quote: Email your PCB files to Sales@pcbsync.com (Preferred for large files) or submit online. We will contact you promptly. Please ensure your email is correct.
Drag & Drop Files, Choose Files to Upload You can upload up to 3 files.

Notes:
For PCB fabrication, we require PCB design file in Gerber RS-274X format (most preferred), *.PCB/DDB (Protel, inform your program version) format or *.BRD (Eagle) format. For PCB assembly, we require PCB design file in above mentioned format, drilling file and BOM. Click to download BOM template To avoid file missing, please include all files into one folder and compress it into .zip or .rar format.

ADXL345 Arduino: Complete Guide to 3-Axis Accelerometer Projects

Working with motion sensors is one of those skills that separates hobbyists from serious embedded developers. The ADXL345 Arduino combination has earned its reputation as the workhorse of acceleration sensing projects, and for good reason. This 3-axis digital accelerometer from Analog Devices delivers professional-grade measurements at a price point that won’t hurt your prototyping budget.

I’ve specified this sensor in dozens of PCB designs over the years, from industrial vibration monitors to consumer fitness trackers. Its digital interface, configurable measurement ranges, and built-in detection features make it far more versatile than simple analog accelerometers. Let me walk you through everything you need to know to integrate the ADXL345 into your Arduino projects successfully.

What Makes the ADXL345 Accelerometer Special?

The ADXL345 is a 3-axis MEMS (Micro-Electro-Mechanical Systems) accelerometer that measures acceleration along the X, Y, and Z axes. Unlike older analog accelerometers that output voltage proportional to acceleration, the ADXL345 provides digital output through I2C or SPI interfaces. This digital approach eliminates analog-to-digital conversion errors and simplifies your circuit design.

What sets this sensor apart is its combination of high resolution (13-bit measurement capability), selectable measurement ranges, and intelligent motion detection features. The chip can detect single taps, double taps, free-fall conditions, and activity/inactivity states without any processing load on your microcontroller. These hardware interrupts make the ADXL345 ideal for battery-powered applications where you want the main processor sleeping most of the time.

The sensor measures both static acceleration (gravity) and dynamic acceleration (motion, vibration, shock). By measuring gravitational acceleration, you can determine the sensor’s orientation relative to Earth. Dynamic measurements let you detect movement, impacts, and vibrations.

ADXL345 Technical Specifications

Understanding the specifications helps you configure the sensor correctly for your application:

ParameterSpecification
Supply Voltage2.0V – 3.6V (chip), 3.3V – 5V (module)
CommunicationI2C (up to 400kHz) or SPI (up to 5MHz)
Measurement Range±2g, ±4g, ±8g, ±16g (selectable)
Resolution13-bit (full resolution mode)
Output Data Rate0.1Hz to 3200Hz
Current Consumption40µA (measurement), 0.1µA (standby)
I2C Address0x53 (SDO low) or 0x1D (SDO high)
Operating Temperature-40°C to +85°C
Package Size3mm x 5mm x 1mm (LGA)

The selectable measurement ranges deserve special attention. For applications requiring precise tilt sensing (like a digital level), use the ±2g range for maximum resolution. For detecting impacts or high-speed motion, the ±16g range prevents signal clipping.

ADXL345 Pinout and Module Overview

Most makers use the ADXL345 on a breakout module like the GY-291, which includes voltage regulation and pull-up resistors. Here’s what each pin does:

PinFunctionDescription
VCCPower Supply3.3V or 5V (module dependent)
GNDGroundCommon ground reference
CSChip SelectTie HIGH for I2C, drive LOW for SPI
INT1Interrupt 1Configurable interrupt output
INT2Interrupt 2Secondary interrupt output
SDOSerial Data OutSPI data output / I2C address select
SDASerial DataI2C data line / SPI data input
SCLSerial ClockI2C/SPI clock line

A critical point many tutorials miss: the CS pin determines whether the ADXL345 operates in I2C or SPI mode. For I2C operation, CS must be tied HIGH. If you leave it floating, the sensor behavior becomes unpredictable. Similarly, the SDO pin sets the I2C address when using I2C mode. Connect SDO to GND for address 0x53, or to VCC for address 0x1D.

Wiring ADXL345 to Arduino

The most common configuration uses I2C communication, which requires only four wires. Here’s the connection table for popular Arduino boards:

ADXL345 PinArduino Uno/NanoArduino MegaArduino Leonardo
VCC3.3V or 5V3.3V or 5V3.3V or 5V
GNDGNDGNDGND
SDAA4202
SCLA5213
CS3.3V3.3V3.3V
SDOGNDGNDGND

Important considerations from real-world experience:

Always check whether your breakout module includes a voltage regulator. Most GY-291 modules do, allowing 5V power input. However, some bare breakout boards require 3.3V only. Supplying 5V to a 3.3V-only board will damage the sensor permanently.

For SPI communication, you’ll need additional connections to the MOSI, MISO, and SS pins of your Arduino. SPI offers faster data rates (up to 5MHz versus 400kHz for I2C) but requires more wiring.

Installing ADXL345 Arduino Libraries

Several libraries simplify ADXL345 programming. Here are the most reliable options:

LibraryBest ForInstallation
Adafruit ADXL345Beginners, unified sensor interfaceArduino Library Manager
SparkFun ADXL345Advanced features, interruptsGitHub / Library Manager
ADXL345_WEComprehensive functionalityArduino Library Manager

For most projects, the Adafruit library works well. Open your Arduino IDE, navigate to Sketch → Include Library → Manage Libraries, search for “Adafruit ADXL345,” and install it. You’ll also need the “Adafruit Unified Sensor” library as a dependency.

Basic ADXL345 Arduino Code Example

Here’s a complete sketch that reads acceleration data and displays it on the Serial Monitor:

#include <Wire.h>

#include <Adafruit_Sensor.h>

#include <Adafruit_ADXL345_U.h>

// Create accelerometer object with unique ID

Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345);

void setup() {

  Serial.begin(9600);

  // Initialize sensor

  if (!accel.begin()) {

    Serial.println(“No ADXL345 detected. Check wiring!”);

    while (1);

  }

  // Set measurement range to ±4g

  accel.setRange(ADXL345_RANGE_4_G);

  Serial.println(“ADXL345 initialized successfully”);

}

void loop() {

  sensors_event_t event;

  accel.getEvent(&event);

  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.println(” m/s^2″);

  delay(100);

}

Upload this code, open the Serial Monitor at 9600 baud, and you should see acceleration values updating continuously. With the sensor flat on your desk, X and Y should read near zero while Z shows approximately 9.8 m/s² (1g from gravity).

Calculating Tilt Angles from ADXL345 Data

Raw acceleration values are useful, but many projects need orientation angles. You can calculate roll and pitch using basic trigonometry:

#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);

  accel.begin();

  accel.setRange(ADXL345_RANGE_2_G);

}

void loop() {

  sensors_event_t event;

  accel.getEvent(&event);

  float ax = event.acceleration.x;

  float ay = event.acceleration.y;

  float az = event.acceleration.z;

  // Calculate roll and pitch in degrees

  float roll = atan2(ay, az) * 180.0 / PI;

  float pitch = atan2(-ax, sqrt(ay * ay + az * az)) * 180.0 / PI;

  Serial.print(“Roll: “); Serial.print(roll);

  Serial.print(” Pitch: “); Serial.println(pitch);

  delay(100);

}

One limitation: accelerometers cannot measure yaw (rotation around the vertical axis) when stationary. The gravitational vector doesn’t change during yaw rotation, so there’s no reference for measurement. For complete orientation tracking, combine the ADXL345 with a magnetometer or gyroscope.

ADXL345 Calibration for Accurate Measurements

Factory calibration gets you close, but environmental factors and mounting orientation can introduce offsets. Here’s a simple calibration approach:

  1. Place the sensor on a known flat, level surface
  2. Record the average readings for each axis over several seconds
  3. Calculate offset values (X and Y should be 0g, Z should be 1g when flat)
  4. Subtract these offsets in your measurement code

The ADXL345 also has built-in offset registers (OFSX, OFSY, OFSZ) that can store calibration values directly in the chip. Writing to these registers automatically adjusts all future readings.

// Simple software calibration example

float offsetX = 0, offsetY = 0, offsetZ = 0;

void calibrate() {

  float sumX = 0, sumY = 0, sumZ = 0;

  int samples = 100;

  Serial.println(“Calibrating… Keep sensor flat and still”);

  for (int i = 0; i < samples; i++) {

    sensors_event_t event;

    accel.getEvent(&event);

    sumX += event.acceleration.x;

    sumY += event.acceleration.y;

    sumZ += event.acceleration.z;

    delay(10);

  }

  offsetX = sumX / samples;

  offsetY = sumY / samples;

  offsetZ = (sumZ / samples) – 9.81; // Subtract 1g for gravity

  Serial.println(“Calibration complete”);

}

Practical ADXL345 Arduino Projects

The ADXL345’s versatility enables numerous applications. Here are proven project ideas I’ve implemented or helped others build:

Motion-Activated Security System

Use the activity/inactivity detection feature to trigger alarms when someone moves a protected object. The ADXL345 can generate hardware interrupts, allowing your Arduino to sleep until motion occurs. This dramatically extends battery life in portable security devices.

Digital Inclinometer

Build a precise tilt meter for construction or automotive applications. The ±2g range provides approximately 4mg resolution, enabling tilt measurements with better than 0.5-degree accuracy. Add an OLED display for a complete handheld tool.

Vibration Analyzer

Monitor machinery health by analyzing vibration patterns. The ADXL345’s high output data rate (up to 3200Hz) captures vibration frequencies that indicate bearing wear, imbalance, or mechanical looseness. Log data to an SD card for trend analysis.

Gaming Controller

Create motion-controlled game inputs by mapping accelerometer data to keyboard or mouse commands. The ADXL345’s fast response and low latency make it suitable for action games requiring quick reactions.

Earthquake Detector

Build a sensitive seismograph that detects ground motion. The ADXL345’s high resolution can detect accelerations as small as 4mg, making it capable of sensing minor seismic activity that humans can’t feel.

Troubleshooting ADXL345 Arduino Problems

From years of fielding questions on electronics forums, these are the most common issues and solutions:

ProblemLikely CauseSolution
“No ADXL345 detected”Wrong I2C address or wiring errorCheck SDO pin connection, verify I2C address
Readings stuck at 0Sensor in standby modeEnsure proper initialization sequence
Noisy or erratic dataElectrical interference or loose connectionsAdd decoupling capacitor, secure wiring
Incorrect axis valuesSensor orientation mismatchVerify axis orientation from datasheet
I2C communication failsMissing pull-up resistorsAdd 4.7kΩ pull-ups if not on module
Sensor works intermittentlyVoltage supply issuesCheck regulator output, add bulk capacitor

One subtle issue: some cheap ADXL345 modules have inadequate voltage regulation. If your readings become unstable when adding other I2C devices, try adding a 10µF capacitor across the module’s power pins.

Using ADXL345 Interrupt Features

The ADXL345’s built-in motion detection features reduce processing overhead and enable power-efficient designs. Here’s how to configure tap detection:

#include <Wire.h>

#include <SparkFun_ADXL345.h>

ADXL345 adxl = ADXL345();

void setup() {

  Serial.begin(9600);

  adxl.powerOn();

  // Configure tap detection

  adxl.setTapThreshold(50);      // 62.5mg per LSB

  adxl.setTapDuration(15);       // 625µs per LSB

  adxl.setDoubleTapLatency(80);  // 1.25ms per LSB

  adxl.setDoubleTapWindow(200);  // 1.25ms per LSB

  // Enable tap detection on all axes

  adxl.setTapDetectionOnXYZ(1, 1, 1);

  // Map interrupts to INT1 pin

  adxl.setImportantInterruptMapping(1, 1, 1, 1, 1);

  // Enable single and double tap interrupts

  adxl.singleTapINT(1);

  adxl.doubleTapINT(1);

  Serial.println(“Tap the sensor!”);

}

void loop() {

  byte interrupts = adxl.getInterruptSource();

  if (adxl.triggered(interrupts, ADXL345_SINGLE_TAP)) {

    Serial.println(“Single tap detected!”);

  }

  if (adxl.triggered(interrupts, ADXL345_DOUBLE_TAP)) {

    Serial.println(“Double tap detected!”);

  }

  delay(50);

}

Useful ADXL345 Resources and Downloads

Here are essential resources for ADXL345 development:

ResourceDescriptionLink
ADXL345 DatasheetOfficial Analog Devices documentationanalog.com
Adafruit LibraryBeginner-friendly Arduino librarygithub.com/adafruit/Adafruit_ADXL345
SparkFun LibraryAdvanced features and examplesgithub.com/sparkfun/SparkFun_ADXL345_Arduino_Library
ADXL345_WE LibraryComprehensive functionalityArduino Library Manager
SparkFun Hookup GuideDetailed wiring and exampleslearn.sparkfun.com

Frequently Asked Questions

What is the difference between ADXL345 and ADXL335?

The ADXL345 is a digital accelerometer with I2C/SPI output, while the ADXL335 is an analog accelerometer with voltage outputs. The ADXL345 offers selectable measurement ranges (±2g to ±16g), built-in motion detection features, and higher resolution. The ADXL335 is simpler with a fixed ±3g range and requires analog-to-digital conversion. For most Arduino projects, the ADXL345 is the better choice due to its digital interface and advanced features.

Can I use multiple ADXL345 sensors with one Arduino?

Yes, you can connect two ADXL345 sensors to the same I2C bus by configuring different addresses. Connect one sensor’s SDO pin to GND (address 0x53) and the other’s SDO to VCC (address 0x1D). For more than two sensors, use SPI communication with separate chip select pins, or add an I2C multiplexer like the TCA9548A.

Why does my ADXL345 show wrong values when tilted?

This usually indicates incorrect axis orientation or calibration issues. First, verify which physical direction corresponds to each axis using the datasheet diagrams. Then perform a calibration routine with the sensor in a known orientation. Also ensure you’re using the correct sensitivity value when converting raw readings to g-units.

How accurate is the ADXL345 for tilt measurement?

The ADXL345 achieves approximately 0.5 to 1 degree accuracy for tilt measurement in the ±2g range under ideal conditions. Accuracy depends on proper calibration, temperature stability, and filtering. For applications requiring better than 0.1-degree accuracy, consider a more expensive industrial-grade sensor or implement sensor fusion with a gyroscope.

Does the ADXL345 work with 5V Arduino boards?

Most ADXL345 breakout modules include a 3.3V voltage regulator and logic level shifting, allowing direct connection to 5V Arduino boards. However, always verify your specific module’s specifications before connecting. Some bare breakout boards without voltage regulation require 3.3V power and may be damaged by 5V signals.

Final Thoughts

The ADXL345 Arduino combination provides an excellent platform for learning accelerometer fundamentals and building practical motion-sensing projects. Its digital interface eliminates many headaches associated with analog sensors, while the built-in detection features enable sophisticated applications without complex software.

Start with basic acceleration readings, progress to tilt angle calculations, then explore the interrupt-driven features for power-efficient designs. The skills you develop working with the ADXL345 transfer directly to other digital sensors and more advanced IMU modules.

Whether you’re building a simple tilt indicator or a sophisticated vibration analysis system, the ADXL345 delivers reliable performance at a price that makes experimentation affordable. Get your sensor wired up and start measuring the world around you.

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Sales & After-Sales Service

Contact & Quotation

  • Inquire: Call 0086-755-23203480, or reach out via the form below/your sales contact to discuss our design, manufacturing, and assembly capabilities.

  • Quote: Email your PCB files to Sales@pcbsync.com (Preferred for large files) or submit online. We will contact you promptly. Please ensure your email is correct.

Drag & Drop Files, Choose Files to Upload You can upload up to 3 files.

Notes:
For PCB fabrication, we require PCB design file in Gerber RS-274X format (most preferred), *.PCB/DDB (Protel, inform your program version) format or *.BRD (Eagle) format. For PCB assembly, we require PCB design file in above mentioned format, drilling file and BOM. Click to download BOM template To avoid file missing, please include all files into one folder and compress it into .zip or .rar format.