Thursday, June 16, 2016

ADXL345 3 Axis Accelerometer


The ADXL345 is a 3-axis acceleration module. Playing with Arduino, Wire.h library is included in the skecth to communicate with the module. In this example, the ADXL345 is configured in I2C mode. Pin A4 of the Arduino (Uno) is connected to the SDA (data) while pin A5 is wired to SCL (clock). CS pin is tied high to enable this mode. As no other devices are connected to the I2C bus, external pull-up resistors are not used in this circuit.


Here, the AUTO_SLEEP (D4) and Measure (D3) bits of the 0x2D register are activated by inserting two lines of code "Wire.write(16)" and "Wire.write(8)" (dec 8 = binary 1000 and dec 16 = binary 10000). Register 0x32 to Register 0x37 hold the output data for each axis (X, Y, Z). Put it simply, two registers are used to store the data for each axis.

#include <Wire.h>

#define accel_module (0x53)

void setup(){
  Wire.begin();
  Serial.begin(9600);

  Wire.beginTransmission(accel_module);
  Wire.write(0x2D);
  Wire.write(0);
  Wire.endTransmission();

  Wire.beginTransmission(accel_module);
  Wire.write(0x2D);
  Wire.write(16);
  Wire.endTransmission();

  Wire.beginTransmission(accel_module);
  Wire.write(0x2D);
  Wire.write(8);
  Wire.endTransmission();
}

void loop(){
  int x1reg=0x32;
  int x2reg=0x33;
  int y1reg=0x34;
  int y2reg=0x35;
  int z1reg=0x36;
  int z2reg=0x37;

  int x1pos, x2pos, y1pos, y2pos, z1pos, z2pos;
  
  byte values[6];
  char output[512];

  Wire.beginTransmission(accel_module);
  Wire.write(x1reg); //Move the register pointer back to 0x32
  Wire.endTransmission();

  Wire.beginTransmission(accel_module);
  Wire.requestFrom(accel_module,1);
  values[0]=Wire.read();
  x1pos=(int)values[0];
  sprintf(output,"%d ",x1pos);
  Serial.print(output);
  Wire.endTransmission();


  Wire.beginTransmission(accel_module);
  Wire.write(x2reg);
  Wire.endTransmission();

  Wire.beginTransmission(accel_module);
  Wire.requestFrom(accel_module,1);
  values[1]=Wire.read();
  x2pos=(int)values[1];
  sprintf(output,"%d ",x2pos);
  Serial.print(output);
  Wire.endTransmission();


  Wire.beginTransmission(accel_module);
  Wire.write(y1reg);
  Wire.endTransmission();

  Wire.beginTransmission(accel_module);
  Wire.requestFrom(accel_module,1);
  values[2]=Wire.read();
  y1pos=(int)values[2];
  sprintf(output,"%d ",y1pos);
  Serial.print(output);
  Wire.endTransmission();


  Wire.beginTransmission(accel_module);
  Wire.write(y2reg);
  Wire.endTransmission();

  Wire.beginTransmission(accel_module);
  Wire.requestFrom(accel_module,1);
  values[3]=Wire.read();
  y2pos=(int)values[3];
  sprintf(output,"%d ",y2pos);
  Serial.print(output);
  Wire.endTransmission();


  Wire.beginTransmission(accel_module);
  Wire.write(z1reg);
  Wire.endTransmission();

  Wire.beginTransmission(accel_module);
  Wire.requestFrom(accel_module,1);
  values[4]=Wire.read();
  z1pos=(int)values[4];
  sprintf(output,"%d ",z1pos);
  Serial.print(output);
  Wire.endTransmission();

  Wire.beginTransmission(accel_module);
  Wire.write(z2reg);
  Wire.endTransmission();

  Wire.beginTransmission(accel_module);
  Wire.requestFrom(accel_module,1);
  values[5]=Wire.read();
  z2pos=(int)values[5];
  sprintf(output,"%d ",z2pos);
  Serial.print(output);
  Wire.endTransmission();

  Serial.write(10);

  delay(2000);
}


No comments:

Post a Comment