Difference between revisions of "ADXL345 Triple Axis Accelerometer Breakout"
(→Usage) |
(→Example code) |
||
Line 23: | Line 23: | ||
==Example code== | ==Example code== | ||
− | #include <Wire.h> | + | #include <Wire.h> // I2C library, gyroscope |
− | + | // Accelerometer ADXL345 | |
− | #define | + | #define ACC (0x1A>>7) //ADXL345 ACC address |
− | #define | + | #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 | |
− | int | + | } |
− | + | void getAccelerometerData(int * result) { | |
− | + | int regAddress = 0x32; //first axis-acceleration-data register on the ADXL345 | |
− | + | byte buff[A_TO_READ]; | |
− | int | + | 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() | void setup() | ||
{ | { | ||
Serial.begin(9600); | Serial.begin(9600); | ||
− | Wire.begin(); | + | Wire.begin(); |
− | + | initAcc(); | |
− | |||
− | |||
− | |||
− | |||
} | } | ||
void loop() | 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== | ==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
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:
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