How to Use an ADXL345 Accelerometer Sensor with Arduino

Introduction

Welcome to the realm of Arduino and motion sensing! In this guide, we’ll delve into How to Use an ADXL345 Accelerometer Sensor with Arduino. The ADXL345 represents a potent and adaptable sensor module enabling the measurement of acceleration across three axes. Whether you’re crafting a tilt sensor, motion tracker, or gesture recognition system, the ADXL345 accelerometer furnishes invaluable data for a myriad of projects. Let’s embark on a journey to explore the intricacies of the ADXL345 accelerometer and discover how to seamlessly integrate it with Arduino.

Hardware Required

You will require the following Hardware Components for the How to Interfacing ADXL345 Accelerometer Sensor with Arduino.

Components#Buy From Amazon
Arduino UNO1Buy Link
ADX345 Accelerometer1Buy Link
9v DC Adapter (Optional)1Buy Link
Jumper Wires4Buy Link
Breadboard1Buy Link

What is an ADXL345 Accelerometer?

The ADXL345 stands as a three-axis accelerometer sensor crafted by Analog Devices. It is meticulously engineered to gauge acceleration along three orthogonal axes: X, Y, and Z. Facilitating communication with Arduino via the I2C (Inter-Integrated Circuit) or SPI (Serial Peripheral Interface) interface, the ADXL345 proves facile to incorporate into Arduino projects. With its diminutive footprint, minimal power consumption, and impressive resolution, the ADXL345 accelerometer proves apt for diverse applications necessitating motion sensing and orientation detection.

Pinout

ADXL345-Accelerometer-Sensor-Module-Pinout

Pin Configuration

Pin NumberPin NameDescription
1+5VPower supply voltage input (5V)
2+3V3Power supply voltage input (3.3V)
3GNDGround
4SCLSerial Clock (I2C)
5SDASerial Data (I2C)
6CSChip Select (SPI)
7USUnused or not connected (NC)
8SDOSerial Data Out (SPI)
9IN1Input/Output pin 1 (function configurable)
10IN2Input/Output pin 2 (function configurable)

Features

  1. Three-Axis Acceleration Sensing: Simultaneous measurement of acceleration along the X, Y, and Z axes, facilitating comprehensive motion sensing capabilities.
  2. Digital Output: Furnishes digital output data for effortless integration with microcontrollers like Arduino.
  3. Low Power Consumption: Engineered for low-power operation, rendering it ideal for battery-powered applications.
  4. High Resolution: Delivers high-resolution measurements for accurate motion detection and analysis.
  5. Built-in FIFO Buffer: Includes a built-in FIFO (First In, First Out) buffer for efficient storage and retrieval of acceleration data, enhancing data processing efficiency.

Specifications

  1. Acceleration Measurement:
    • Measurement Range: ±2g, ±4g, ±8g, ±16g (selectable).
    • Resolution: Up to 13 bits.
    • Sensitivity: Adjustable.
  2. Communication Interface:
    • I2C (Inter-Integrated Circuit) or SPI (Serial Peripheral Interface) interface for seamless communication with Arduino.
    • Operating voltage: 2.0V to 3.6V.
  3. Sampling Rate:
    • Programmable sampling rates up to 3200Hz.
  4. Temperature Range:
    • Operating Temperature: -40°C to +85°C.
  5. Dimensions:
    • Compact form factor conducive to integration into a myriad of electronic projects.
    • Standard breakout board design streamlining connection with Arduino.

Circuit Diagram

The following circuit shows you the connection of the How to Use an ADXL345 Accelerometer Sensor with Arduino, Please make the connection carefully

How-to-Use-an-ADXL345-Accelerometer-Sensor-with-Arduino-Circuit

Circuit Connections

ArduinoADXL345 Sensor
+3.3VVCC Pin
+3.3VCS
GNDGND Pin
A4SDA
A5SCL

Installing Arduino IDE Software

First, you will require to Download the updated version of Arduino IDE Software and Install it on your PC or laptop. if you Learn How to install the Arduino step-by-step guide then click on how to install Arduino Button given Blow

Installing Libraries

Now when you are Ready to upload the code, to the Arduino Board you will need first to add the Following Libraries in Arduino, If you Learn How to add the library in the Arduino step-by-step guide click on how to install the library Button given Blow

Code

//For more Projects: www.arduinocircuit.com

// Gnd - GND
// 3.3v - VCC
// 3.3v - CS
// Analog 4 - SDA
// Analog 5 - SLC

#include <Wire.h>

//Device address
const int DEVICE_ADDRESS = (0x53);

byte _buff[6];

//ADXL345 register addresses
char POWER_CTL = 0x2D;
char DATA_FORMAT = 0x31;
char DATAX0 = 0x32; //X-Axis Data 0
char DATAX1 = 0x33; //X-Axis Data 1
char DATAY0 = 0x34; //Y-Axis Data 0
char DATAY1 = 0x35; //Y-Axis Data 1
char DATAZ0 = 0x36; //Z-Axis Data 0
char DATAZ1 = 0x37; //Z-Axis Data 1

void setup()
{
  Serial.begin(57000);
  Serial.print("Started");
  
  Wire.begin();
  writeTo(DEVICE_ADDRESS, DATA_FORMAT, 0x01); //Set ADXL345 to +- 4G
  writeTo(DEVICE_ADDRESS, POWER_CTL, 0x08); //Put the ADXL345
}

void loop()
{
  readAccel(); //Read acceleration x, y, z
  delay(500);
}

void readAccel() {
  //Read the data
  uint8_t numBytesToRead = 6;
  readFrom(DEVICE_ADDRESS, DATAX0, numBytesToRead, _buff);

  //Read the values ​​from the register and convert to int (Each axis has 10 bits, in 2 Bytes LSB)
  int x = (((int)_buff[1]) << 8) | _buff[0];
  int y = (((int)_buff[3]) << 8) | _buff[2];
  int z = (((int)_buff[5]) << 8) | _buff[4];
  Serial.print("x: ");
  Serial.print( x );
  Serial.print(" and: ");
  Serial.print( and );
  Serial.print(" z: ");
  Serial.println( z );
}

//Auxiliary writing function
void writeTo(int device, byte address, byte val) {
  Wire.beginTransmission(device);
  Wire.write(address);
  Wire.write(val);
  Wire.endTransmission();
}

//Auxiliary reading function
void readFrom(int device, byte address, int num, byte _buff[]) {
  Wire.beginTransmission(device);
  Wire.write(address);
  Wire.endTransmission();

  Wire.beginTransmission(device);
  Wire.requestFrom(device, num);

  int i = 0;
  while(Wire.available())
  {
    _buff[i] = Wire.read();
    i++;
  }
  Wire.endTransmission();
}

We can use libraries like Sparkfun’s for the ADXL345 In above, offering I2C and SPI communication, configuration, and interrupt handling for activity detection. The library includes code examples, with the following being a condensed version that achieves similar functionality with less code.

//For more Projects: www.arduinocircuit.com

#include <SPI.h>
#include <Wire.h>
#include <SparkFun_ADXL345.h>

ADXL345 adxl = ADXL345();

void setup()
{
  Serial.begin(9600);
  Serial.println("Start");
  Serial.println();

  adxl.powerOn();
  adxl.setRangeSetting(16); //Define the range, values ​​2, 4, 8 or 16
}

void loop()
{
  //read the values ​​and print them
  int x, y, z;
  adxl.readAccel(&x, &y, &z);
  Serial.print(x);
  Serial.print(", ");
  Serial.print(y);
  Serial.print(", ");
  Serial.println(z);
}

Applications

  1. Tilt Sensing: Employ the ADXL345 accelerometer to discern tilt and orientation alterations in applications such as gaming controllers, robotics, and electronic levels.
  2. Motion Tracking: Implement motion tracking systems leveraging the ADXL345 accelerometer to monitor movement and acceleration in sports analysis, fitness tracking, and activity monitoring devices.
  3. Gesture Recognition: Develop gesture recognition systems harnessing the ADXL345 accelerometer to identify and interpret hand gestures for seamless human-computer interaction in smart devices and wearable technology.
  4. Vehicle Dynamics Monitoring: Integrate the ADXL345 accelerometer into vehicle dynamics monitoring systems to measure acceleration, inclination, and vibration, facilitating applications such as vehicle stability control and rollover detection.
  5. Structural Health Monitoring: Utilize the ADXL345 accelerometer for structural health monitoring in civil engineering applications, monitoring vibrations and movements in buildings, bridges, and infrastructure to detect structural defects and damage early on.

Conclusion

With its advanced features and adaptability, the ADXL345 accelerometer offers boundless possibilities for motion sensing and orientation tracking in Arduino projects. Whether crafting tilt sensors, motion trackers, gesture recognition systems, vehicle dynamics monitors, or structural health monitoring devices, the ADXL345 provides precise and reliable acceleration data, enhancing project functionality and performance. Let’s embark on the journey of employing the ADXL345 accelerometer with Arduino and unlock the potential of motion sensing in your projects!

Leave a Comment


error: