Difference between revisions of "ADXL345 Triple Axis Accelerometer Breakout"

From Geeetech Wiki
Jump to: navigation, search
(Example code)
(Example code)
Line 26: Line 26:
  
 
==Example code==
 
==Example code==
#include <Wire.h> // I2C library, gyroscope
+
#include <Wire.h> // I2C library, gyroscope
// Accelerometer ADXL345
+
// Accelerometer ADXL345
#define ACC (0xA7>>1)    //ADXL345 ACC address
+
#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)
+
#define A_TO_READ (6)        //num of bytes we are going to read each time (two bytes for each axis)
void initAcc() {
+
void initAcc() {
 
  //Turning on the ADXL345
 
  //Turning on the ADXL345
 
  writeTo(ACC, 0x2D, 1<<3);       
 
  writeTo(ACC, 0x2D, 1<<3);       
Line 36: Line 36:
 
  writeTo(ACC, 0x2C, 0x09);
 
  writeTo(ACC, 0x2C, 0x09);
 
  //by default the device is in +-2g range reading
 
  //by default the device is in +-2g range reading
}
+
}
void getAccelerometerData(int * result) {
+
void getAccelerometerData(int * result) {
 
  int regAddress = 0x32;    //first axis-acceleration-data register on the ADXL345
 
  int regAddress = 0x32;    //first axis-acceleration-data register on the ADXL345
 
  byte buff[A_TO_READ];
 
  byte buff[A_TO_READ];
Line 46: Line 46:
 
  result[1] = (((int)buff[3])<< 8) | buff[2];
 
  result[1] = (((int)buff[3])<< 8) | buff[2];
 
  result[2] = (((int)buff[5]) << 8) | buff[4];
 
  result[2] = (((int)buff[5]) << 8) | buff[4];
}
+
}
void setup()
+
void setup()
{
+
{
 
  Serial.begin(9600);
 
  Serial.begin(9600);
 
  Wire.begin();
 
  Wire.begin();
 
  initAcc();
 
  initAcc();
}
+
}
void loop()
+
void loop()
{
+
{
int hx,hy,hz;
+
int hx,hy,hz;
int acc[3];
+
int acc[3];
getAccelerometerData(acc);
+
getAccelerometerData(acc);
hx = acc[0];
+
hx = acc[0];
hy = acc[1];
+
hy = acc[1];
hz = acc[2];
+
hz = acc[2];
Serial.print(" X=");
+
Serial.print(" X=");
Serial.print(hx);
+
Serial.print(hx);
Serial.print(" Y=");
+
Serial.print(" Y=");
Serial.print(hy);
+
Serial.print(hy);
Serial.print(" Z=");
+
Serial.print(" Z=");
Serial.println(hz);
+
Serial.println(hz);
delay(50);  
+
delay(50);  
}
+
}
//---------------- Functions
+
//---------------- Functions
//Writes val to address register on ACC
+
//Writes val to address register on ACC
void writeTo(int DEVICE, byte address, byte val) {
+
void writeTo(int DEVICE, byte address, byte val) {
 
   Wire.beginTransmission(DEVICE); //start transmission to ACC  
 
   Wire.beginTransmission(DEVICE); //start transmission to ACC  
 
   Wire.write(address);        // send register address
 
   Wire.write(address);        // send register address
 
   Wire.write(val);        // send value to write
 
   Wire.write(val);        // send value to write
 
   Wire.endTransmission(); //end transmission
 
   Wire.endTransmission(); //end transmission
}
+
}
//reads num bytes starting from address register on ACC in to buff array
+
//reads num bytes starting from address register on ACC in to buff array
 
  void readFrom(int DEVICE, byte address, int num, byte buff[]) {
 
  void readFrom(int DEVICE, byte address, int num, byte buff[]) {
 
  Wire.beginTransmission(DEVICE); //start transmission to ACC  
 
  Wire.beginTransmission(DEVICE); //start transmission to ACC  
Line 92: Line 92:
 
  }
 
  }
 
  Wire.endTransmission(); //end transmission
 
  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 01:37, 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

#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