Filling out Adafruit's accelerometer offerings, we now have the really lovely digital ADXL345 from Analog Devices, a triple-axis accelerometer with digital I2C and SPI interface breakout. We added an on-board 3.3V regulator and logic-level shifting circuitry, making it a perfect choice for interfacing with any 3V or 5V microcontroller such as the Arduino.

The sensor has three axes of measurements, X Y Z, and pins that can be used either as I2C or SPI digital interfacing. You can set the sensitivity level to either +-2g, +-4g, +-8g or +-16g. The lower range gives more resolution for slow movements, the higher range is good for high speed tracking. The ADXL345 is the latest and greatest from Analog Devices, known for their exceptional quality MEMS devices.

We added an on-board 3.3V regulator and logic-level shifting circuitry, making it a perfect choice for interfacing with any 3V or 5V microcontroller or computer, such as Arduino or Raspberry Pi.

There are both Arduino (C/C++) and CircuitPython (Python 3) libraries available, so you can use it with any microcontroller like Arduino, ESP8266, Metro, etc. or with Raspberry Pi or other Linux computers thanks to Blinka (our CircuitPython library support helper).

As if that weren't enough, we've also added SparkFun qwiic compatible STEMMA QT connectors for the I2C bus so you don't even need to solder. Just wire up to your favorite micro with a plug-and-play cable to get accelerometer data ASAP. For a no-solder experience, just wire up to your favorite micro using a STEMMA QT adapter cable. 

The Stemma QT connectors also mean the ADXL can be used with our various associated accessories.QT cable is not included, but we have a variety in the shop

Each order comes with a fully tested and assembled breakout and some header for soldering to a PCB or breadboard. Comes with 0.1" standard header in case you want to use it with a breadboard or perfboard. Four 2.5mm (0.1") mounting holes for easy attachment. You'll be up and running in under 5 minutes

The ADXL345 is a low-power, 3-axis MEMS accelerometer modules with both I2C and SPI interfaces. The Adafruit Breakout boards for these modules feature on-board 3.3v voltage regulation and level shifting which makes them simple to interface with 5v microcontrollers such as the Arduino.

The ADXL345 features 4 sensitivity ranges from +/- 2G to +/- 16G. And it supports output data rates ranging from 10Hz to 3200Hz.

ADXL345 datasheet

How it Works

MEMS - Micro Electro-Mechanical Systems

The sensor consists of a micro-machined structure on a silicon wafer. The structure is suspended by polysilicon springs which allow it to deflect smoothly in any direction when subject to acceleration in the X, Y and/or Z axis.

Deflection causes a change in capacitance between fixed plates and plates attached to the suspended structure. This change in capacitance on each axis is converted to an output voltage proportional to the acceleration on that axis.

There are two versions of this board - the STEMMA QT version shown above, and the original header-only version shown below. The code works the same on both!

The ADXL345 breakout has the following pinout:

The default I2C address for this board is 0x53.

Power Pins

This breakout board can be run on 3.3V and 5V systems. We added an on-board 3.3V regulator and logic-level shifting circuitry, making it a perfect choice for interfacing with any 3V or 5V microcontroller such as the Arduino.

  • VIN - This is the input to the 3.3V voltage regulator, which makes it possible to use the 3.3V sensor on 5V systems. It also determines the logic level of the SCL and SDA pins. Connect this to 3.3V on the MCU for 3.3V boards (Adafruit Feathers), or 5.0V for 5V Arduinos (Arduino Uno, etc.).
  • 3Vo - This is the OUTPUT of the 3.3V regulator, and can be used to provide 3.3V power to other parts of your project if required (< 100mA).
  • GND - Connect this to the GND pin on your development board to make sure they are sharing a common GND connection, or the electrons won't have anywhere to flow!

I2C Pins

  • SCL - The clock line on the I2C bus. This pin has an internal pullup resistor on the PCB, which is required as part of the I2C spec, meaning you don't need to add one externally yourself. This also functions as SCK in SPI mode.
  • SDA - The data line on the I2C bus. This pin has an internal pullup resistor on the PCB, which is required as part of the I2C spec, meaning you don't need to add one externally yourself. This also functions as MOSI in SPI mode.
  • STEMMA QT - These connectors allow you to connect to development boards with STEMMA QT connectors, or to other things, with various associated accessories.
The available pins are identical for both versions of the boards. However, on the STEMMA QT version, pins I2 (INT2), SDO, and CS are located at the top of the board.

Other Pins

  • SDO/ALT ADDR - This pin can be used as MISO in SPI mode, but is more commonly used as an optional bit in the I2C bus address. By default this pin is pulled down, meaning it has a value of 0 at startup, which will results in an I2C address of 0x53. If you set this pin high (to 3.3V), and reset, the I2C address will be updated to 0x1D.
  • CS: This dual purpose pin can be used as the chip select line in SPI mode, but also determines whether the board will boot up into I2C or SPI mode. The default of logic high sets the board up for I2C, and manually setting this pin low and resetting will cause the device to enter SPI mode. Please note that SPI mode is not actively support and the SPI pins are not all 5V safe and level shifted, so care will be required when using it!
  • INT1 and INT2: There are two optional interrupt output pins on this sensor, which can be configured to change their state when one or more 'events' occur. For details on how to use these interrupts, see the Arduino/HW Interrupts page later in this guide.

LED Jumper

  • LED jumper - This jumper is located on the back of the board. Cut the trace on this jumper to cut power to the "on" LED.

The board comes with all surface-mount components pre-soldered. The included header strip can be soldered on for convenient use on a breadboard or with 0.1" connectors. However, for applications subject to extreme accelerations, shock or vibration, locking connectors or direct soldering is advised.

Assembly

Position the Header

Cut the header to size if necessary. Then plug the header - long pins down - into a breadboard to stabilize it for soldering.

Add the Breakout

Align the breakout board and place it over the header pins on the breadboard.

And Solder

Be sure to solder all pins to assure good electrical contact.

I2C Wiring

The ADXL345 Breakout has an I2C address of 0x53. It can share the I2C bus with other I2C devices as long as each device has a unique address. Only 4 connections are required for I2C communication:

  • GND -> GND
  • VIN -> +5v
  • SDA -> SDA (Analog 4 on "Classic Arduinos")
  • SCL -> SCL (Analog 5 on "Classic Arduinos")

The Adafruit breakout has level shifting and regulation circuitry so you can power it from 3-5V and use 3V or 5V logic levels for I2C.

  • Connect SCL on the Metro to SCL (yellow wire) on the ADXL343
  • Connect SDA on the Metro to SDA (blue wire) in the ADXL343
  • Connect GND on the Metro to GND (black wire) on the ADXL343
  • For 3.3V LOGIC boards: connect 3.3V on the Arduino/Metro to VIN (red wire) on the ADXL343
  • For 5.0V LOGIC boards: Connect 5V on the Arduino/Metro to VIN (red wire) on the ADXL343

Install the Library:

Download the ADXL345 library and install it. You will also need the Adafruit Sensor Library if you do not already have it installed.

This guide will help you with the install process.

Test:

Click "File->Examples->Adafruit_ADXL345->sensortest" to load the example sketch from the library.

Then click on the compile/upload button to compile and upload the sketch to the Arduino. You should see output similar to below. Watch the values change as you move the board around.

Calibrate:

The ADXL chips are calibrated at the factory to a level of precision sufficient for most purposes. For critical applications where a higher degree of accuracy is required, you may wish to re-calibrate the sensor yourself.

Calibration does not change the sensor outputs. But it tells you what the sensor output is for a known stable reference force in both directions on each axis. Knowing that, you can calculate the corrected output from a sensor reading.

Gravity as a Calibration Reference

Acceleration can be measured in units of gravitational force or "G", where 1G represents the gravitational pull at the surface of the earth. Gravity is a relatively stable force and makes a convenient and reliable calibration reference for surface-dwelling earthlings.

Calibration Method:

To calibrate the sensor to the gravitational reference, you need to determine the sensor output for each axis when it is precisely aligned with the axis of gravitational pull. Laboratory quality calibration uses precision positioning jigs. The method described here is simple and gives surprisingly good results with just a block of wood.

Mount the Sensor:

FIrst mount the sensor securely to a block or a box. The size is not important, as long as all the sides are at right angles. The material is not important as long as it is fairly rigid.

Load the Calibration Sketch:

Load and run the Calibration sketch below. Open the Serial Monitor and wait for the prompt.

Position the Block:

Place the block on a firm flat surface such as a sturdy table. Type a character in the Serial Monitor and hit return. The sketch will take a measurement on that axis and print the results.

Reposition the Block:

Turn the block so a different side is flat on the table and type another key to measure that axis.

Repeat:

Repeat for all six sides of the block to measure the positive and negative aspects of each axis.

(Hint:)

For the sides obstructed by the breakout board and/or wires, press the block up against the bottom of the table while taking the reading.

Calibration Results:

Once all six sides have been sampled, the values printed in the Serial Monitor will represent actual measurements for +/- 1G forces on each axis. These values can be used to re-scale readings for better accuracy.

Calibration Sketch:

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345_U.h>

/* Assign a unique ID to this sensor at the same time */
Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345);


float AccelMinX = 0;
float AccelMaxX = 0;
float AccelMinY = 0;
float AccelMaxY = 0;
float AccelMinZ = 0;
float AccelMaxZ = 0;


void setup(void) 
{
  Serial.begin(9600);
  Serial.println("ADXL345 Accelerometer Calibration"); 
  Serial.println("");
  
  /* Initialise the sensor */
  if(!accel.begin())
  {
    /* There was a problem detecting the ADXL345 ... check your connections */
    Serial.println("Ooops, no ADXL345 detected ... Check your wiring!");
    while(1);
  }
}

void loop(void)
{
    Serial.println("Type key when ready..."); 
    while (!Serial.available()){}  // wait for a character
    
    /* Get a new sensor event */ 
    sensors_event_t accelEvent;  
    accel.getEvent(&accelEvent);
    
    if (accelEvent.acceleration.x < AccelMinX) AccelMinX = accelEvent.acceleration.x;
    if (accelEvent.acceleration.x > AccelMaxX) AccelMaxX = accelEvent.acceleration.x;
    
    if (accelEvent.acceleration.y < AccelMinY) AccelMinY = accelEvent.acceleration.y;
    if (accelEvent.acceleration.y > AccelMaxY) AccelMaxY = accelEvent.acceleration.y;
  
    if (accelEvent.acceleration.z < AccelMinZ) AccelMinZ = accelEvent.acceleration.z;
    if (accelEvent.acceleration.z > AccelMaxZ) AccelMaxZ = accelEvent.acceleration.z;
  
    Serial.print("Accel Minimums: "); Serial.print(AccelMinX); Serial.print("  ");Serial.print(AccelMinY); Serial.print("  "); Serial.print(AccelMinZ); Serial.println();
    Serial.print("Accel Maximums: "); Serial.print(AccelMaxX); Serial.print("  ");Serial.print(AccelMaxY); Serial.print("  "); Serial.print(AccelMaxZ); Serial.println();

    while (Serial.available())
    {
      Serial.read();  // clear the input buffer
    }
}

Typical Calibration Output:

ADXL345 Accelerometer Calibration

Type key when ready...
Accel Minimums: 0.00  0.00  0.00
Accel Maximums: 0.12  0.20  1.14
Type key when ready...
Accel Minimums: 0.00  0.00  0.00
Accel Maximums: 0.12  0.20  1.14
Type key when ready...
Accel Minimums: 0.00  0.00  0.00
Accel Maximums: 0.12  0.20  1.14
Type key when ready...
Accel Minimums: 0.00  0.00  0.00
Accel Maximums: 0.12  0.20  1.14
Type key when ready...
Accel Minimums: 0.00  0.00  -0.24
Accel Maximums: 0.12  1.37  1.14
Type key when ready...
Accel Minimums: 0.00  0.00  -0.24
Accel Maximums: 0.12  1.37  1.14
Type key when ready...
Accel Minimums: 0.00  -1.22  -0.27
Accel Maximums: 0.12  1.37  1.14
Type key when ready...
Accel Minimums: 0.00  -1.22  -0.27
Accel Maximums: 0.12  1.37  1.14
Type key when ready...
Accel Minimums: -1.18  -1.22  -0.27
Accel Maximums: 0.12  1.37  1.14
Type key when ready...

The results of the calibration sketch can be used to do a two-point calibraton as described here: Two Point Calibration

Constructor:

Adafruit_ADXL345(int32_t sensorID = -1)

Constructs an instance of the ADXL345 device driver object. 'sensorID' is a device identifier. It will be returned in the sensor_event in each call to getEvent(). The sensorID has no effect on the operation of the driver or device, but is useful in managing sensor events in systems with multiple sensors.

Initialization()

bool begin(void)

The begin() function initializes communication with the device. The return value is 'true' if it succeeds in connecting to the ADXL345.

Sensor Details:

void getSensor(sensor_t*);

The getSensor() function returns basic information about the sensor. For details about the sensor_t structure, refer to the ReadMe file for the Adafruit Sensor Library.

Getting and Setting the operating range:

void setRange(range_t range)

The setRange() function sets the operating range for the sensor. Higher values will have a wider measurement range. Lower values will have more sensitivity.

Valid range constants are:

  • ADXL345_RANGE_16_G
  • ADXL345_RANGE_8_G
  • ADXL345_RANGE_4_G
  • ADXL345_RANGE_2_G (default value)
range_t getRange(void);

The getRange() function returns the current operating range as set by setRange()

Getting and Setting the Data Rate:

void setDataRate(dataRate_t dataRate);

The setDataRate() function sets the rate at which the sensor output is updated. Rates above 100 Hz will exhibit increased noise. Rates below 6.25 Hz will be more sensitive to temperature variations. See the data sheet for details.

Valid data rate constants are:

  • ADXL345_DATARATE_3200_HZ
  • ADXL345_DATARATE_1600_HZ
  • ADXL345_DATARATE_800_HZ
  • ADXL345_DATARATE_400_HZ
  • ADXL345_DATARATE_200_HZ
  • ADXL345_DATARATE_100_HZ
  • ADXL345_DATARATE_50_HZ
  • ADXL345_DATARATE_25_HZ
  • ADXL345_DATARATE_12_5_HZ
  • ADXL345_DATARATE_6_25HZ
  • ADXL345_DATARATE_3_13_HZ
  • ADXL345_DATARATE_1_56_HZ
  • ADXL345_DATARATE_0_78_HZ
  • ADXL345_DATARATE_0_39_HZ
  • ADXL345_DATARATE_0_20_HZ
  • ADXL345_DATARATE_0_10_HZ (default value)
dataRate_t getDataRate(void);

The getDataRate() function returns the current data rate as set by setDataRate().

Reading Sensor Events:

void getEvent(sensors_event_t*);

The getEvent() function returns the next available reading in the form of a sensor_event. The sensor_event contains the sensor_id as passed to the constructor as well as the X, Y and Z axis readings from the accelerometer. For more information about sensor_events, see the ReadMe file for the Adafruit Sensor Library.

It's easy to use the ADXL343 or the ADXL345 with Python and CircuitPython, and the Adafruit CircuitPython ADXL34x module. This module allows you to easily write Python code that reads the acceleration, taps, motion and more from the breakout.

You can use this sensor with any CircuitPython microcontroller board or with a computer that has GPIO and Python thanks to Adafruit_Blinka, our CircuitPython-for-Python compatibility library.

The pinouts on the ADXL343 and the ADXL345 are slightly different, but the chips are essentially identical. This page includes different wiring diagrams for each. Other than initialising the proper chip, the code will be the same for both!

CircuitPython Microcontroller Wiring

First, wire up the breakout exactly as shown in the previous pages. Here is an example of wiring the ADXL343 to a Feather M0:

 

  • Connect SCL (yellow wire in STEMMA QT version) on the Feather to SCL on the ADXL343
  • Connect SDA (blue wire in STEMMA QT version) on the Feather to SDA in the ADXL343
  • Connect GND (black wire in STEMMA QT version) on the Feather to GND on the ADXL343
  • Connect 3.3V (red wire in STEMMA QT version) on the Feather to VIN on the ADXL343

Here's an example of wiring the ADXL345 to a Feather M0:

  • Connect SCL (blue wire) on the Feather to SCL on the ADXL345
  • Connect SDA (yellow wire) on the Feather to SDA in the ADXL345
  • Connect GND (black wire) on the Feather to GND on the ADXL345
  • Connect 3.3V (red wire) on the Feather to VIN on the ADXL345

Python Computer Wiring

Since there's dozens of Linux computers/boards you can use we will show wiring for Raspberry Pi. For other platforms, please visit the guide for CircuitPython on Linux to see whether your platform is supported

The following shows a Raspberry Pi connected to the ADXL343:

  • Connect SCL (yellow wire in STEMMA QT version) on the RPi to SCL on the ADXL343
  • Connect SDA (blue wire in STEMMA QT version) on the Rpi to SDA in the ADXL343
  • Connect GND (black wire in STEMMA QT version) on the Rpi to GND on the ADXL343
  • Connect 3.3V (red wire in STEMMA QT version) on the Rpi to VIN on the ADXL343

The following shows a Raspberry Pi connected to the ADXL345:

  • Connect SCL (blue wire) on the RPi to SCL on the ADXL345
  • Connect SDA (yellow wire) on the RPi to SDA in the ADXL345
  • Connect GND (black wire) on the RPi to GND on the ADXL345
  • Connect 3.3V (red wire) on the RPi to VIN on the ADXL345

Library Installation

You'll need to install the Adafruit CircuitPython ADXL34x library on your CircuitPython board.

First make sure you are running the latest version of Adafruit CircuitPython for your board.

Next you'll need to install the necessary libraries to use the hardware--carefully follow the steps to find and install these libraries from Adafruit's CircuitPython library bundle.  Our CircuitPython starter guide has a great page on how to install the library bundle.

For non-express boards like the Trinket M0 or Gemma M0, you'll need to manually install the necessary libraries from the bundle:

  • adafruit_adxl34x.mpy
  • adafruit_bus_device

Before continuing make sure your board's lib folder or root filesystem has the adafruit_adxl34x.mpy, and adafruit_bus_device files and folders copied over.

Next connect to the board's serial REPL so you are at the CircuitPython >>> prompt.

Python Installation of the ADXL34x Library

You'll need to install the Adafruit_Blinka library that provides the CircuitPython support in Python. This may also require enabling I2C on your platform and verifying you are running Python 3. Since each platform is a little different, and Linux changes often, please visit the CircuitPython on Linux guide to get your computer ready!

Once that's done, from your command line run the following command:

  • sudo pip3 install adafruit-circuitpython-adxl34x

If your default Python is version 3 you may need to run 'pip' instead. Just make sure you aren't trying to use CircuitPython on Python 2.x, it isn't supported!

CircuitPython & Python Usage

To demonstrate the usage of the breakout we'll initialize it and read the acceleration and more from the board's Python REPL.

Run the following code to import the necessary modules and create the I2C object:

import time
import board
import adafruit_adxl34x

i2c = board.I2C()

If you're using the ADXL343, run the following to initialise the I2C connection with the breakout:

accelerometer = adafruit_adxl34x.ADXL343(i2c)

If you're using the ADXL345, run the following to initialise the I2C connection with the breakout:

accelerometer = adafruit_adxl34x.ADXL345(i2c)

Now you're ready to read values from and enable features of the breakout using any of the following:

  • acceleration - The acceleration values on the x, y and z axes
  • enable_motion_detection - Enables motion detection. Allows for setting threshold. Threshold defaults to 18.
  • enable_tap_detection - Enables tap detection. Allows for single or double-tap detection.
  • enable_freefall_detection - Enables freefall detection. Allows for setting threshold and time. Threshold defaults to 10, time defaults to 25.
  • events - Used to read the events when motion detection, tap detection and freefall detection are enables. Requires specifying which event you are trying to read.

To print the acceleration values:

while True:
    print(accelerometer.acceleration)
    time.sleep(0.2)

That's all there is to reading acceleration values from the ADXL343 and ADXL345 using CircuitPython!

Full Example Code

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import time
import board
import adafruit_adxl34x

i2c = board.I2C()  # uses board.SCL and board.SDA
# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller

# For ADXL343
accelerometer = adafruit_adxl34x.ADXL343(i2c)
# For ADXL345
# accelerometer = adafruit_adxl34x.ADXL345(i2c)

while True:
    print("%f %f %f" % accelerometer.acceleration)
    time.sleep(0.2)

Motion, Tap and Freefall

There are examples for enabling and using motion, tap and freefall available on GitHub:

Save any of the files as code.py on your CircuitPython board, or run them from the Python REPL on your Linux computer, to try them out.

This guide was first published on Mar 26, 2013. It was last updated on Apr 13, 2024.