Go Down

Topic: Error with NEW ADXL345 thirple axis accelerometer (Read 2188 times) previous topic - next topic

Achira

i bought a adxl 345 accelerometer $) $). i dont know how this connect with arduino mega :smiley-surprise: :smiley-surprise:.





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.  :smiley-roll: :smiley-roll:
follow is my code, please help me any one.
THANK yOU :) :)


Code: [Select]
#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
}


olivierH

hello,

i'll be honest. I'm totally new and i never programmed an arduino (but i have ordered one, together with an adxl345) but i found this. And i think it might be very helpfull.
I hope i have helped.

https://www.sparkfun.com/tutorials/240

Kind regards,
Oli

Achira

Thanks Friend,
i will look at this web site and i feel it can help to me. also i should notice that i found one thing. that is in my type of sensors the pin called CS should supply with 3.3v. because of then sensor know that we use I2C method.
following are the pin configuration.
For Arduino mega.
ADXL345                           Arduino
3.3v                to                    3.3v
GND               to                   GND
SDA                to                   SDA
SDL                to                   SDL
CS                   to                  3.3v

olivierH

yep indeed.

i found this info in the datasheet  :)

I2C and SPI digital communications are available. In both cases, the ADXL345 operates as a slave. I2C mode is enabled if the CS pin is tied high to VDD I/O. The CS pin should always be tied high to VDD I/O or be driven by an external controller because there is no default mode if the CS pin is left unconnected. Therefore, not taking these precautions may result in an inability to communicate with the part. In SPI mode, the CS pin is controlled by the bus master. In both SPI and I2C modes of operation, data transmitted from the ADXL345 to the master device should be ignored during writes to the ADXL345

The reason it has both 5v and 3.3v inputs is because the breakout board has a 3.3v regulator chip, so it can be powered by both.

Do watch though, sending 5v down the I2C  bus could eventually damage the accelerometer, and I'm not sure the breakout has logic level conversion.

olivierH

hey,

mine has a voltage regulator on board ( power supply between 3 and 5V) and also voltage deviders / corrections (simple with resistors and transistors.) for 5v communication at 3.3V pins for as far that i can figure out :).
it is ADXL345 chipset and GY-29 board speacial for arduino, that is what they have told me.

olivierH

i received my arduino today,
set up with the adl345 and it is working. I can see the data on the serial monitor.  :D

Go Up