ADXL345

Overview

The ADXL345 Accelerometer [Product Link] is great for measuring accelerations in robots.

Features:

ADXL345 Accelerometer
ADXL345 Accelerometer
  • 13-bit resolution
  • Measuring range of ± 2, ± 4, ± 8, ± 16g
  • SPI and I2C Communication
  • Ultra Low Power: 40uA in measurement mode, 0.1uA in standby@ 2.5V
  • Operating voltage: 2.2V to 3.6V

Description

The ADXL-345 is a low power 3-axis MEMS accelerometer. This GY-291 module can measure the static acceleration of gravity and dynamic acceleration.

Special sensing functions are- Activity/Inactivity monitoring, Single/Double tap detection, Free-fall detection. These functions can be mapped to one of two interrupt output pins. Low power modes enable intelligent motion-based power management with threshold sensing and active acceleration measurement at extremely low power dissipation.
ADXL345 sensor has 8 pins: Gnd, Vcc, INT1, INT2, CS, SD0, SDA, SCL.

Connections

ADXL345 SDA SCL Vcc CS GND SC0 INT1 INT2
Arduino Uno A4 A5 3.3V 3.3V GND GND Not Connected Not Connected

Make the connections as shown above and upload the code given below to your Arduino UNO. After this open the serial monitor Arduino IDE which will display different values of three dimensions(x,y and z) according to the position of your accelerometer in space.

#include <Wire.h> // I2C library, gyroscope
// Accelerometer ADXL345
#define ACC (0xA7>>1) //ADXL345 ACC address
#define A_TO_READ (6) //number 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);
}

//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 number of 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
}

Downloads

Datasheet [Link]

Possible Application Areas

The ADXL345 can be used in applications that require the accelerations and tilt values as an input to perform a particular function.
1. Remote Controlled Robot Car [Link].
2. Quadcopters.

Comments

So empty here ... leave a comment!

Leave a Reply

Your email address will not be published. Required fields are marked *

Sidebar