PandaByte ADXL345 Accelerometer Module: I2C and SPI Communication Guide

by pandabyte in Circuits > Arduino

11 Views, 0 Favorites, 0 Comments

PandaByte ADXL345 Accelerometer Module: I2C and SPI Communication Guide

adxl.png

The PandaByte ADXL345 Accelerometer Module is a compact 3-axis digital accelerometer capable of measuring acceleration along the X, Y, and Z axes. It is widely used in applications such as motion detection, vibration monitoring, tilt sensing, robotics, wearable devices, and IoT systems.

One of the major advantages of the ADXL345 is that it supports both I2C and SPI communication, allowing users to choose the interface that best suits their application.

Like other PandaByte Grove modules, it supports both:

  1. Traditional male header connections
  2. 4-pin Grove connector for quick plug-and-play prototyping (For I2C only)

In this guide, you will learn:

  1. ADXL345 pin configuration
  2. I2C communication
  3. SPI communication
  4. Circuit connections
  5. Reading acceleration values
  6. Arduino programming examples


Supplies

adxl2.png

Hardware

  1. PandaByte ADXL345 Accelerometer Module
  2. PandaByte xC3m ESP32-C3 Development Board
  3. PandaByte Grove Expansion Board
  4. USB Cable
  5. Grove Cable (or Dupont jumper wires)

Software

  1. Arduino IDE
  2. SparkFun ADXL345 Library

Install Library

  1. Open Arduino IDE
  2. Sketch → Include Library → Manage Libraries
  3. Search for SparkFun ADXL345
  4. Install the library developed by SparkFun Electronics.

Circuit Connections

pins.png

Perform the connection based on the table based on whether you are connecting in I2C or SPI mode.

Programming

Program 1: Reading Acceleration using I2C

#include <Wire.h>
#include <SparkFun_ADXL345.h>
ADXL345 adxl = ADXL345();
void setup()
{
Serial.begin(9600);
Wire.begin();
adxl.powerOn();
adxl.setActivityXYZ(1,1,1);
}

void loop()
{
int x,y,z;
adxl.readAccel(&x,&y,&z);
Serial.print(x);
Serial.print(", ");
Serial.print(y);
Serial.print(", ");
Serial.println(z);
delay(200);
}

Program 2: Reading Acceleration using SPI

#include <SparkFun_ADXL345.h>
ADXL345 adxl = ADXL345(3); // CS connected to GPIO 3

void setup()
{
Serial.begin(9600);
adxl.powerOn();
adxl.setSpiBit(0); // 4-wire SPI
adxl.setActivityXYZ(1,1,1);
}

void loop()
{
int x,y,z;
adxl.readAccel(&x,&y,&z);
Serial.print(x);
Serial.print(", ");
Serial.print(y);
Serial.print(", ");
Serial.println(z);
delay(200);
}


Understanding the Output

The Serial Monitor displays three values:

-12, 245, 980

where

  1. First value → X-axis acceleration
  2. Second value → Y-axis acceleration
  3. Third value → Z-axis acceleration

Rotate or tilt the sensor and observe how these values change.

When the module is placed flat on a table, the Z-axis will typically show the largest value due to Earth's gravitational acceleration.

Important Information

  1. The ADXL345 supports both I2C and SPI communication.
  2. I2C requires only SDA and SCL, making wiring simple and suitable for most projects.
  3. For PandaByte xC3m dev board, SDA and SCL pins are GPIO 1 and GPIO 0, respectively. If you are using a different dev board, make sure to connect the sensor to correct SCL and SDA pins.
  4. For PandaByte xC3m dev board, SPI pins are SCK (GPIO 4), MISO (GPIO 5), MOSI (GPIO 6). If you are using a different dev board, make sure to connect the sensor to correct SPI pins.
  5. SPI offers faster communication and is preferred in applications requiring higher sampling rates.
  6. The SparkFun ADXL345 library supports both communication modes using the same programming interface.
  7. The sensor measures acceleration along three independent axes (X, Y, and Z) and can detect motion, vibration, orientation, and free-fall events.