ADXL345 Digital Accelerometer sensor interfacing with Arduino

ADXL345 Digital Accelerometer sensor module interfacing with Arduino Board is all about the measuring 3-axis acceleration using ADXL345 Sensors and wiring with Arduino board. I’ll explain how it works and how to interface with Arduino and circuit diagram.


ADXL345 Sensor:

              The ADXL345 Digital Accelerometer is a small, thin, ultralow power, 3-axis accelerometer with high resolution 13-bit digital measurement at up to ±16 g. output of ADXL345 Digital Accelerometer is formatted as 16-bit twos complement and is acces-sible through either a 3- or 4-wire SPI or I2C digital interface.                                

  The ADXL345 is well suited for many applications like Handsets,Medical instrumentation,Gaming and pointing devices,Industrial instrumentation,Personal navigation devices,Hard disk drive (HDD) protection. It measures the static acceleration of gravity in tilt-sensing appli-cations, as well as dynamic acceleration resulting from motion or shock. Its high resolution "3.9 mg/LSB" enables measurement of inclination changes less than 1.0°.

              Several special sensing functions are provided. Activity and inactivity sensing detect the presence or lack of motion by comparing the acceleration on any axis with user-set thresholds. Tap sensing detects single and double taps in any direction. Free-fall sensing detects if the device is falling. These functions can be mapped individually to either of two interrupt output pins. An integrated memory management system with a 32-level first in, first out (FIFO) buffer can be used to store data to minimize host processor activity and lower overall system power consumption.

              Low power modes enable intelligent motion-based power management with threshold sensing and active acceleration measurement at extremely low power dissipation.

Circuit diagram:

As shown in the image connect a ADXL345 Digital Accelerometer sensor pin to Arduino-uno pins.

// Cabling for i2c using ADXL345 with an Arduino Uno:

 Arduino <-> ADXL345 board

==> Gnd      -  GND

==> 3.3v     -  VCC

==> 3.3v     -  CS

==> Analog 4 -  SDA

==> Analog 5 -  SLC

Arduino Code:

-----------------------------------------------------------------------------------------------------------------------


#include <Wire.h>


#define DEVICE (0x53) // Device address as specified in data sheet 


byte _buff[6];


char POWER_CTL = 0x2D; //Power Control Register

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()

{

  Wire.begin();        // join i2c bus (address optional for master)

  Serial.begin(115200);  // start serial for output. Make sure you set your Serial Monitor to the same!

  Serial.print("init");

  

  //Put the ADXL345 into +/- 4G range by writing the value 0x01 to the DATA_FORMAT register.

  writeTo(DATA_FORMAT, 0x00); //writeTo(DATA_FORMAT, 0x01);

  //Put the ADXL345 into Measurement Mode by writing 0x08 to the POWER_CTL register.

  writeTo(POWER_CTL, 0x08);

}


void loop()

{

  readAccel(); // read the x/y/z tilt

  delay(1000); // only read every 0,5 seconds

}


void readAccel()

{

  uint8_t howManyBytesToRead = 6;

  readFrom( DATAX0, howManyBytesToRead, _buff); //read the acceleration data from the ADXL345


  // each axis reading comes in 10 bit resolution, ie 2 bytes.  Least Significat Byte first!!

  // thus we are converting both bytes in to one int

  int x = (((int)_buff[1]) << 8) | _buff[0];   

  int y = (((int)_buff[3]) << 8) | _buff[2];

  int z = (((int)_buff[5]) << 8) | _buff[4];

  

  if(x > 20)Serial.print("move forword");

  else if(x < -20)Serial.print("move backword");

  else if(y > 20)Serial.print("move left side");

  else if(y < -20)Serial.print("move right side");

  Serial.print("x: ");

  Serial.print( x );

  Serial.print(" y: ");

  Serial.print( y );

  Serial.print(" z: ");

  Serial.println( z );

}


void writeTo(byte address, byte val) 

{

  Wire.beginTransmission(DEVICE); // start transmission to device 

  Wire.write(address);             // send register address

  Wire.write(val);                 // send value to write

  Wire.endTransmission();         // end transmission

}


// Reads num bytes starting from address register on device in to _buff array

void readFrom(byte address, int num, byte _buff[]) 

{

  Wire.beginTransmission(DEVICE); // start transmission to device 

  Wire.write(address);             // sends address to read from

  Wire.endTransmission();         // end transmission


  Wire.beginTransmission(DEVICE); // start transmission to device

  Wire.requestFrom(DEVICE, num);    // request 6 bytes from device


  int i = 0;

  while(Wire.available())         // device may send less than requested (abnormal)

  { 

    _buff[i] = Wire.read();    // receive a byte

    i++;

  }

  Wire.endTransmission();         // end transmission

}


-----------------------------------------------------------------------------------------------------------------------------

Tutorial Video:

          here you can see the video tutorial of ADXL345 sensor and Arduino-Uno board.serial terminal print the value as you Chang module.


 If this tutorial helpful for you then like and subscribe to my you-tube channel for more helpful tutorials, thank you.

Post a Comment

0 Comments