i bought a adxl 345 accelerometer $) $). i dont know how this connect with arduino mega .
unlike other accelerometers this has 3.3v and 5v-vcc both pins. i try with this.[/font]
here i use arduino mega, therefore i use pin 20 and 21 instead of AnIn4 and AnIn5. and i connect 3.3v pin only to 3.3v pin in arduino. and others are same.
but it always show 0 to the output.
follow is my code, please help me any one.
THANK yOU
#include <Wire.h>
#define DEVICE (0x53) Â Â //ADXL345 device address
#define TO_READ (6) Â Â Â Â //num of bytes we are going to read each time (two bytes for each axis)
byte buff[TO_READ] ; Â Â //6 bytes buffer for saving data read from the device
char str[512]; Â Â Â Â Â Â Â Â Â Â Â //string buffer to transform data before sending it to the serial port
void setup()
{
 Wire.begin();     // join i2c bus (address optional for master)
 Serial.begin(9600);  // start serial for output
Â
//Turning on the ADXL345
writeTo(DEVICE, 0x2D, 0);
writeTo(DEVICE, 0x2D, 16);
writeTo(DEVICE, 0x2D, 8);
// adding 1011 binary to 0x31 register??
writeTo(DEVICE, 0x31, 11);
}
void loop()
{
 int regAddress = 0x32;   //first axis-acceleration-data register on the ADXL345
 int x, y, z;
Â
 readFrom(DEVICE, regAddress, 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
 x = (((int)buff[1]) << 8) | buff[0]; Â
 y = (((int)buff[3])<< 8) | buff[2];
 z = (((int)buff[5]) << 8) | buff[4];
Â
 //we send the x y z values as a string to the serial port
 sprintf(str, "%d %d %d", x, y, z); Â
 Serial.print(str);
 Serial.write(10);
Â
 //It appears that delay is needed in order not to clog the port
 delay (500);
}
//---------------- Functions
//Writes val to address register on device
void writeTo(int device, byte address, byte val) {
 Wire.beginTransmission(device); //start transmission to device
 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 device in to buff array
void readFrom(int device, byte address, int num, byte buff[]) {
 Wire.beginTransmission(device); //start transmission to device
 Wire.write(address);     //sends address to read from
 Wire.endTransmission(); //end transmission
Â
 Wire.beginTransmission(device); //start transmission to device
 Wire.requestFrom(device, num);   // request 6 bytes from device
Â
 int i = 0;
 while(Wire.available())   //device may send less than requested (abnormal)
 {
  buff[i] = Wire.read(); // receive a byte
  i++;
 }
 Wire.endTransmission(); //end transmission
}
#include <Wire.h>
#define DEVICE (0x53) Â Â //ADXL345 device address
#define TO_READ (6) Â Â Â Â //num of bytes we are going to read each time (two bytes for each axis)
byte buff[TO_READ] ; Â Â //6 bytes buffer for saving data read from the device
char str[512]; Â Â Â Â Â Â Â Â Â Â Â //string buffer to transform data before sending it to the serial port
void setup()
{
 Wire.begin();     // join i2c bus (address optional for master)
 Serial.begin(9600);  // start serial for output
Â
//Turning on the ADXL345
writeTo(DEVICE, 0x2D, 0);
writeTo(DEVICE, 0x2D, 16);
writeTo(DEVICE, 0x2D, 8);
// adding 1011 binary to 0x31 register??
writeTo(DEVICE, 0x31, 11);
}
void loop()
{
 int regAddress = 0x32;   //first axis-acceleration-data register on the ADXL345
 int x, y, z;
Â
 readFrom(DEVICE, regAddress, 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
 x = (((int)buff[1]) << 8) | buff[0]; Â
 y = (((int)buff[3])<< 8) | buff[2];
 z = (((int)buff[5]) << 8) | buff[4];
Â
 //we send the x y z values as a string to the serial port
 sprintf(str, "%d %d %d", x, y, z); Â
 Serial.print(str);
 Serial.write(10);
Â
 //It appears that delay is needed in order not to clog the port
 delay (500);
}
//---------------- Functions
//Writes val to address register on device
void writeTo(int device, byte address, byte val) {
 Wire.beginTransmission(device); //start transmission to device
 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 device in to buff array
void readFrom(int device, byte address, int num, byte buff[]) {
 Wire.beginTransmission(device); //start transmission to device
 Wire.write(address);     //sends address to read from
 Wire.endTransmission(); //end transmission
Â
 Wire.beginTransmission(device); //start transmission to device
 Wire.requestFrom(device, num);   // request 6 bytes from device
Â
 int i = 0;
 while(Wire.available())   //device may send less than requested (abnormal)
 {
  buff[i] = Wire.read(); // receive a byte
  i++;
 }
 Wire.endTransmission(); //end transmission
}