Difference between revisions of "ADXL345 Triple Axis Accelerometer Breakout"

From Geeetech Wiki
Jump to: navigation, search
(Usage)
(Example code)
Line 23: Line 23:
  
 
==Example code==
 
==Example code==
  #include <Wire.h>
+
  #include <Wire.h> // I2C library, gyroscope
  #define Register_ID 0
+
  // Accelerometer ADXL345
  #define Register_2D 0x2D
+
  #define ACC (0x1A>>7)    //ADXL345 ACC address
  #define Register_X0 0x32
+
  #define A_TO_READ (6)        //num of bytes we are going to read each time (two bytes for each axis)
  #define Register_X1 0x33
+
  void initAcc() {
#define Register_Y0 0x34
+
  //Turning on the ADXL345
#define Register_Y1 0x35
+
  writeTo(ACC, 0x2D, 1<<3);     
#define Register_Z0 0x36
+
  writeTo(ACC, 0x31, 0x0B);
#define Register_Z1 0x37
+
  writeTo(ACC, 0x2C, 0x09);
//
+
  //by default the device is in +-2g range reading
  int ADXAddress = 0xA7>>1; 
+
}
int reading = 0;
+
  void getAccelerometerData(int * result) {
int val = 0;
+
  int regAddress = 0x32;   //first axis-acceleration-data register on the ADXL345
int X0,X1,X_out;
+
  byte buff[A_TO_READ];
  int Y0,Y1,Y_out;
+
  readFrom(ACC, regAddress, A_TO_READ, buff); //read the acceleration data from the ADXL345
int Z1,Z0,Z_out;
+
  //each axis reading comes in 10 bit resolution, ie 2 bytes. Least Significat Byte first!!
  double Xg,Yg,Zg;
+
  //thus we are converting both bytes in to one int
 +
  result[0] = (((int)buff[1]) << 8) | buff[0]; 
 +
  result[1] = (((int)buff[3])<< 8) | buff[2];
 +
  result[2] = (((int)buff[5]) << 8) | buff[4];
 +
  }
 
  void setup()
 
  void setup()
 
  {
 
  {
 
   Serial.begin(9600);
 
   Serial.begin(9600);
   Wire.begin();
+
   Wire.begin();
   delay(100);
+
   initAcc();
  Wire.beginTransmission(ADXAddress);
 
  Wire.send(Register_2D);
 
  Wire.send(8);
 
  Wire.endTransmission();
 
 
  }
 
  }
 
  void loop()
 
  void loop()
 
  {
 
  {
  Wire.beginTransmission(ADXAddress);
+
int hx,hy,hz;
  Wire.send(Register_X0);
+
int acc[3];
  Wire.send(Register_X1);
+
getAccelerometerData(acc);
  Wire.endTransmission();
+
hx = acc[0];
  Wire.requestFrom(ADXAddress,2);
+
hy = acc[1];
  if(Wire.available()<=2);
+
hz = acc[2];
  {
+
 
    X0 = Wire.receive();
+
Serial.print(" X=");
    X1 = Wire.receive();
+
Serial.print(hx);
    X1 = X1<<8;
+
Serial.print(" Y=");
    X_out = X0+X1;
+
Serial.print(hy);
  }
+
Serial.print(" Z=");
  //
+
Serial.println(hz);
  Wire.beginTransmission(ADXAddress);
+
delay(50);  
  Wire.send(Register_Y0);
+
}
  Wire.send(Register_Y1);
+
//---------------- Functions
  Wire.endTransmission();
+
//Writes val to address register on ACC
  Wire.requestFrom(ADXAddress,2);
+
void writeTo(int DEVICE, byte address, byte val) {
  if(Wire.available()<=2);
+
  Wire.beginTransmission(DEVICE); //start transmission to ACC
  {
+
  Wire.send(address);       // send register address
    Y0 = Wire.receive();
+
  Wire.send(val);       // send value to write
    Y1 = Wire.receive();
+
  Wire.endTransmission(); //end transmission
    Y1 = Y1<<8;
+
}
    Y_out = Y0+Y1;
+
//reads num bytes starting from address register on ACC in to buff array
  }
+
   void readFrom(int DEVICE, byte address, int num, byte buff[]) {
  //
+
   Wire.beginTransmission(DEVICE); //start transmission to ACC
  Wire.beginTransmission(ADXAddress);
+
   Wire.send(address);       //sends address to read from
  Wire.send(Register_Z0);
+
  Wire.endTransmission(); //end transmission
  Wire.send(Register_Z1);
+
    
  Wire.endTransmission();
+
   Wire.beginTransmission(DEVICE); //start transmission to ACC
   Wire.requestFrom(ADXAddress,2);
+
   Wire.requestFrom(DEVICE, num);   // request 6 bytes from ACC
   if(Wire.available()<=2);
+
   int i = 0;
   {
+
   while(Wire.available())    //ACC may send less than requested (abnormal)
    Z0 = Wire.receive();
+
   {
    Z1 = Wire.receive();
+
    buff[i] = Wire.receive(); // receive a byte
    Z1 = Z1<<8;
+
    i++;
    Z_out = Z0+Z1;
 
  }
 
  //
 
   Xg = X_out/256.00;
 
   Yg = Y_out/256.00;
 
  Zg = Z_out/256.00;
 
  //
 
   Serial.print(" X=");
 
  Serial.print(Xg);
 
  //
 
   Serial.print(" Y=");
 
   Serial.print(Yg);
 
  //
 
  Serial.print(" Z=");
 
   Serial.println(Zg);
 
  delay(300);  
 
 
   }
 
   }
 +
  Wire.endTransmission(); //end transmission
 +
}
  
 
==How to buy==
 
==How to buy==
 
Click here to buy [http://www.geeetech.com/adxl345triple-axis-accelerometer-breakout-p-384.html ADXL345 3-Axis Accelerometer Breakout]
 
Click here to buy [http://www.geeetech.com/adxl345triple-axis-accelerometer-breakout-p-384.html ADXL345 3-Axis Accelerometer Breakout]

Revision as of 02:57, 14 July 2012

Introduction

ADXL345 1.jpg

This is a breakout board for Analog Devices ADXL345 3-Axis Accelerometer.The ADXL345 is a small, thin, ultralow power, 3-axis accelerometer with high resolution (13-bit) measurement at up to ±16 g. Digital output data is formatted as 16-bit twos complement and is accessible through either a SPI (3- or 4-wire) or I2C digital interface. The ADXL345 is well suited for mobile device applications. It measures the static acceleration of gravity in tilt-sensing applications, 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°.

Features

  • Ultralow power: as low as 23 μA in measurement mode
  • Fixed 10-bit resolution Full resolution, up to 13-bit resolution at ±16 g
  • Single tap/double tap detection
  • Activity/inactivity monitoring
  • Free-fall detection
  • Supply voltage range: 2.0 V to 3.6 V
  • I/O voltage range: 1.7 V to VS
  • SPI (3- and 4-wire) and I2C digital interfaces

Usage

Here is the guide illustrates how to connect an Arduino to the ADXL335 breakout board. The following is a table describing which pins on the Arduino should be connected to the pins on the accelerometer:

500px File:ADX table.jpg

Example code

#include <Wire.h> // I2C library, gyroscope
// Accelerometer ADXL345
#define ACC (0x1A>>7)    //ADXL345 ACC address
#define A_TO_READ (6)        //num of bytes we are going to read each time (two bytes for each axis)
void initAcc() {
 //Turning on the ADXL345
 writeTo(ACC, 0x2D, 1<<3);      
 writeTo(ACC, 0x31, 0x0B);
 writeTo(ACC, 0x2C, 0x09);
 //by default the device is in +-2g range reading
}
void getAccelerometerData(int * result) {
 int regAddress = 0x32;    //first axis-acceleration-data register on the ADXL345
 byte buff[A_TO_READ];
 readFrom(ACC, regAddress, A_TO_READ, 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
 result[0] = (((int)buff[1]) << 8) | buff[0];   
 result[1] = (((int)buff[3])<< 8) | buff[2];
 result[2] = (((int)buff[5]) << 8) | buff[4];
}
void setup()
{
 Serial.begin(9600);
 Wire.begin();
 initAcc();
}
void loop()
{
int hx,hy,hz;
int acc[3];
getAccelerometerData(acc);
hx = acc[0];
hy = acc[1];
hz = acc[2];
Serial.print(" X=");
Serial.print(hx);
Serial.print(" Y=");
Serial.print(hy);
Serial.print(" Z=");
Serial.println(hz);
delay(50); 
}
//---------------- Functions
//Writes val to address register on ACC
void writeTo(int DEVICE, byte address, byte val) {
  Wire.beginTransmission(DEVICE); //start transmission to ACC 
  Wire.send(address);        // send register address
  Wire.send(val);        // send value to write
  Wire.endTransmission(); //end transmission
}
//reads num bytes starting from address register on ACC in to buff array
 void readFrom(int DEVICE, byte address, int num, byte buff[]) {
 Wire.beginTransmission(DEVICE); //start transmission to ACC 
 Wire.send(address);        //sends address to read from
 Wire.endTransmission(); //end transmission
 
 Wire.beginTransmission(DEVICE); //start transmission to ACC
 Wire.requestFrom(DEVICE, num);    // request 6 bytes from ACC
 int i = 0;
 while(Wire.available())    //ACC may send less than requested (abnormal)
 { 
   buff[i] = Wire.receive(); // receive a byte
   i++;
 }
 Wire.endTransmission(); //end transmission
}

How to buy

Click here to buy ADXL345 3-Axis Accelerometer Breakout