ADXL345 Accelerometer library not working

Status
Not open for further replies.

UnaClocker

Active member
I'm using a Teensy 3, with the final TeensyDuino code, Arduino 1.0.3.. ADXL345 hooked up via i2c. I've tried this library:
http://code.google.com/p/adxl345driver/
When I run the test sketch in that library, nothing happens on the serial monitor. I don't even get the initial message. With or without the ADXL345 connected, same result.
So then I tried the sketch from this URL:
http://codeyoung.blogspot.com/2009/11/adxl345-accelerometer-breakout-board.html
Code:
//ADXL345 code from http://codeyoung.blogspot.com/2009/11/adxl345-accelerometer-breakout-board.html

#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(57600);  // start serial for output
  
  //Turning on the ADXL345
  writeTo(DEVICE, 0x2D, 0);      
  writeTo(DEVICE, 0x2D, 16);
  writeTo(DEVICE, 0x2D, 8);
}

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);  
  sprintf(str, "%d", z);  
  Serial.print(str);
  Serial.print(10, BYTE);
  
  //It appears that delay is needed in order not to clog the port
  delay(15);
}

void writeTo(int device, byte address, byte val) 
{
  Wire.beginTransmission(device); //start transmission to device 
  Wire.send(address);        // send register address
  Wire.send(val);        // send value to write
  Wire.endTransmission(); //end transmission
}

void readFrom(int device, byte address, int num, byte buff[]) 
{
  Wire.beginTransmission(device); //start transmission to device 
  Wire.send(address);        //sends address to read from
  Wire.endTransmission(); //end transmission
  
  Wire.beginTransmission(device); //start transmission to device (initiate again)
  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.receive(); // receive a byte
    i++;
  }
  Wire.endTransmission(); //end transmission
}
It runs, which is better than the last one, but I get nothing but zero's returned. Which is the same thing I get when the ADXL345 isn't even connected to the T3..
I have 1k pull-up resistors from 3.3v to pins 18 and 19, I don't quite understand why it's not working at this point.
 
Last edited:
Some more troubleshooting.. Looks like there are comms happening on the i2c bus.. I scoped the clock and data pins and I see things happening..
SCL.png

The above is the SCL (clock) line
SDA.png

And this is the SDL (data) line...
 
Last edited:
Ok, must have missed it in the datasheet last time, I didn't tie CS to VCC and SDO to ground, which I should have. So I revised my board and am having new boards made. Try try again. I'll revive this thread when I have the chip wired correctly, if I encounter any more problems..
 
Welp, got my new board made. Tried again. Exact same problem. Not sure where I've gone wrong, or why it's not working. I've attached my design, if anyone wants to have a look, see if I made any mistakes. It's just the ADXL that's not working. The voltage regulator, temp sensor, and SDCard seem to work fine.
View attachment problem_board.zip
 
Welp, got my new board made. Tried again. Exact same problem. Not sure where I've gone wrong, or why it's not working. I've attached my design, if anyone wants to have a look, see if I made any mistakes. It's just the ADXL that's not working. The voltage regulator, temp sensor, and SDCard seem to work fine.
View attachment 272

Have you tried this library http://bildr.org/2011/03/adxl345-arduino/ ? I got it to compile with some tweaks. It seemed to work ok with the limited testing I did.
 
Status
Not open for further replies.
Back
Top