Tuesday 1 December 2015

3 Axis Accelerometer (ADXL345) using Arduino

Accelerometer is a device to measure the rate of change of velocity, or to measure acceleration. The unit of this acceleration is meter per second squared (m/s2) or in G-Forces (g). One G-Force is equal to 9.8m/s2. Accelerometer is useful enough to make a device that utilize earth gravitation. Accelerometer is usually measured in 3-axis (X Axis, Y Axis, and Z Axis). The common device module to measure the acceleration is ADXL345.

ADXL345 is a small, thin, ultralow power, 3-axis accelerometer with 13 bit resolution measurement up to ±16g. The acceleration measurement system can be selected from ±2g±4g±8g, or ±16g. ADXL345 is well suited for mobile applications.




In this opportunity, I would like to share about how to make accelerometer (ADXL345) using Arduino.


Step 1: Component Preparation

To Make an accelerometer using Arduino, here are some component that required to make a simple accelerometer.


1x Arduino UNO
1x Accelerometer ADXL345 (the datasheet can be downloaded here)
4x Cable Jumper

Step 2: Wiring

ADXL345 module has I2C address of 0x53 as you can see from the datasheet. It can share I2C bus with other devices as long as each device has unique address.



Only 4 connection we need to make an I2C communication.
ADXL345 ↔ Arduino
  • VIN ↔ 5V
  • GND ↔ GND
  • SDA ↔ SDA (Analog pin 4)
  • SCL ↔ SCL (Analog pin 5)

Here I made my own Arduino shield for ADXL345, but the connection follow the wiring diagram above.


Step 3: Coding

To understand more about ADXL345 I2C address and how to access the ADXL345, I suggest you read the datasheet.
 #include <Wire.h>  
 #define DEVICE (0x53)    //ADXL I2C device address  
 #define TO_READ (6)     //Total bytes to be read (2 bytes each axis)  
 byte buff[TO_READ];     //Save read data to buffer  
 char value[512];  
 void setup(){  
  Wire.begin();       //Start I2C communication  
  Serial.begin(9600);    //Start serial communication to   
                //show read output from ADXL  
  //Turning on ADXL  
  writeTo(DEVICE, 0x02, 0);  
  writeTo(DEVICE, 0x02, 16);  
  writeTo(DEVICE, 0x02, 8);  
 }  
 void loop(){  
  int regAddress = 0x32;  //First data from ADXL  
  int Axis_X, Axis_Y, Axis_Z; //Defining three Axis from ADXL  
  readFrom(DEVICE, regAddress, TO_READ, buff); //Read data from ADXL  
  //Read data from ADXL is bytes so must convert it to int  
  Axis_X = (((int)buff[1]) << 8) | buff[0];  
  Axis_Y = (((int)buff[3]) << 8) | buff[2];  
  Axis_Z = (((int)buff[5]) << 8) | buff[4];  
  //Print the values to PC  
  sprintf(value, "X Axis = %d, Y Axis = %d, Z Axis = %d", Axis_X, Axis_Y, Axis_Z);  
  Serial.println(value);  
  delay(500);  
 }  
 //Write value to ADXL  
 void writeTo(int device, byte address, byte val){  
  Wire.beginTransmission(device); //Start communicating ADXL  
  Wire.write(address);       //Send register address0x03  
  Wire.write(val) ;        //Send value to write  
  Wire.endTransmission();     //End communication  
 }  
 void readFrom(int device, byte address, int num, byte buff[]){  
  Wire.beginTransmission(device); //Start communicating ADXL  
  Wire.write(address);       //Send register address  
  Wire.endTransmission();     //End communication  
  Wire.beginTransmission(device); //Start communicating ADXL  
  Wire.requestFrom(device, num);  //Request number of byte from ADXL  
  int i = 0;  
  while(Wire.available()){      
   buff[i] = Wire.read();     //Receive value  
   i++;  
  }  
  Wire.endTransmission();     //End communication  
 }               

Step 4: Testing

Connect the ADXL345 to Arduino using I2C communication, then open Arduino serial monitor (Ctrl+Shift+M) to see the result.



Thank you, I hope this will help you.





No comments:

Post a Comment