Difference between revisions of "ADXL345 Triple Axis Accelerometer Breakout"

From Geeetech Wiki
Jump to: navigation, search
(Usage)
 
(14 intermediate revisions by the same user not shown)
Line 14: Line 14:
 
*I/O voltage range: 1.7 V to VS
 
*I/O voltage range: 1.7 V to VS
 
*SPI (3- and 4-wire) and I2C digital interfaces  
 
*SPI (3- and 4-wire) and I2C digital interfaces  
 +
 +
==Document==
 +
[http://www.geeetech.com/Documents/ADXL345%20Datasheet.pdf ADXL345 Datasheet]
  
 
==Usage==
 
==Usage==
Line 19: Line 22:
 
Arduino should be connected to the pins on the accelerometer:  
 
Arduino should be connected to the pins on the accelerometer:  
  
[[File:345 wiring.jpg|500px]]
+
[[File:345 wiring1.jpg|500px]]
 
+
[[File:ADX table123.jpg]]
[[File:ADX table.jpg.jpg]]
 
  
 
==Example code==
 
==Example code==
  #include <Wire.h>
+
For Arduino 1.0+
  #define Register_ID 0
+
  #include <Wire.h> // I2C library, gyroscope
  #define Register_2D 0x2D
+
  // Accelerometer ADXL345
  #define Register_X0 0x32
+
  #define ACC (0xA7>>1)    //ADXL345 ACC address
  #define Register_X1 0x33
+
  #define A_TO_READ (6)        //num of bytes we are going to read each time (two bytes for each axis)
  #define Register_Y0 0x34
+
  void initAcc() {
  #define Register_Y1 0x35
+
  //Turning on the ADXL345
  #define Register_Z0 0x36
+
  writeTo(ACC, 0x2D, 1<<3);     
  #define Register_Z1 0x37
+
  writeTo(ACC, 0x31, 0x0B);
  //
+
  writeTo(ACC, 0x2C, 0x09);
  int ADXAddress = 0xA7>>1; 
+
  //by default the device is in +-2g range reading
  int reading = 0;
+
}
  int val = 0;
+
  void getAccelerometerData(int * result) {
  int X0,X1,X_out;
+
  int regAddress = 0x32;   //first axis-acceleration-data register on the ADXL345
  int Y0,Y1,Y_out;
+
  byte buff[A_TO_READ];
  int Z1,Z0,Z_out;
+
  readFrom(ACC, regAddress, A_TO_READ, buff); //read the acceleration data from the ADXL345
  double Xg,Yg,Zg;
+
//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()
 
  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];
  {
+
Serial.print(" X=");
    X0 = Wire.receive();
+
Serial.print(hx);
    X1 = Wire.receive();
+
Serial.print(" Y=");
    X1 = X1<<8;
+
Serial.print(hy);
    X_out = X0+X1;
+
Serial.print(" Z=");
  }
+
Serial.println(hz);
  //
+
delay(50);  
  Wire.beginTransmission(ADXAddress);
+
}
  Wire.send(Register_Y0);
+
//---------------- Functions
  Wire.send(Register_Y1);
+
//Writes val to address register on ACC
  Wire.endTransmission();
+
void writeTo(int DEVICE, byte address, byte val) {
  Wire.requestFrom(ADXAddress,2);
+
   Wire.beginTransmission(DEVICE); //start transmission to ACC
  if(Wire.available()<=2);
+
   Wire.write(address);       // send register address
  {
+
   Wire.write(val);       // send value to write
    Y0 = Wire.receive();
+
   Wire.endTransmission(); //end transmission
    Y1 = Wire.receive();
+
}
    Y1 = Y1<<8;
+
//reads num bytes starting from address register on ACC in to buff array
    Y_out = Y0+Y1;
+
void readFrom(int DEVICE, byte address, int num, byte buff[]) {
  }
+
Wire.beginTransmission(DEVICE); //start transmission to ACC
  //
+
Wire.write(address);       //sends address to read from
   Wire.beginTransmission(ADXAddress);
+
Wire.endTransmission(); //end transmission
   Wire.send(Register_Z0);
+
   Wire.send(Register_Z1);
+
Wire.beginTransmission(DEVICE); //start transmission to ACC
   Wire.endTransmission();
+
Wire.requestFrom(DEVICE, num);   // request 6 bytes from ACC
  Wire.requestFrom(ADXAddress,2);
+
int i = 0;
  if(Wire.available()<=2);
+
while(Wire.available())    //ACC may send less than requested (abnormal)
  {
+
{
    Z0 = Wire.receive();
+
  buff[i] = Wire.read(); // receive a byte
    Z1 = Wire.receive();
+
  i++;
    Z1 = Z1<<8;
+
}
    Z_out = Z0+Z1;
+
Wire.endTransmission(); //end transmission
  }
+
}
  //
 
  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);
 
  }
 
  
 
==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]

Latest revision as of 01:39, 10 December 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

Document

ADXL345 Datasheet

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:

345 wiring1.jpg ADX table123.jpg

Example code

For Arduino 1.0+

#include <Wire.h> // I2C library, gyroscope
// Accelerometer ADXL345
#define ACC (0xA7>>1)    //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.write(address);        // send register address
 Wire.write(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.write(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.read(); // receive a byte
  i++;
}
Wire.endTransmission(); //end transmission
}

How to buy

Click here to buy ADXL345 3-Axis Accelerometer Breakout