ADXL 345 Accelerometer (with ADXL library)

Hello,

I’m trying to get the ADXL 345 up and running with the Spark Core, using the ADXL 345 library. I have tried both including the library into a project, and using the ‘no library’ variant, which talks directly to registers on the break out board.

Unfortunately, I’m not having much success at reading the X, Y and Z values at the moment (or any of the more advanced ‘tap’ features of the board.

Here is the code I’m using:

// Cabling for i2c using Sparkfun breakout with a Spar Core
// Spark Core <-> Breakout board
// Gnd         -  GND
// 3.3v        -  VCC
// 3.3v        -  CS
// Digital 0   -  SDA
// Digital 1   -  SCL


#define DEVICE (0x53) // Device address as specified in data sheet 

byte _buff[6];

char POWER_CTL = 0x2D;	//Power Control Register
char DATA_FORMAT = 0x31;
char DATAX0 = 0x32;	//X-Axis Data 0
char DATAX1 = 0x33;	//X-Axis Data 1
char DATAY0 = 0x34;	//Y-Axis Data 0
char DATAY1 = 0x35;	//Y-Axis Data 1
char DATAZ0 = 0x36;	//Z-Axis Data 0
char DATAZ1 = 0x37;	//Z-Axis Data 1

void setup()
{
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(57000);  // start serial for output. Make sure you set your Serial Monitor to the same!
  Serial.print("init");
  
  //Put the ADXL345 into +/- 4G range by writing the value 0x01 to the DATA_FORMAT register.
  writeTo(DATA_FORMAT, 0x01);
  //Put the ADXL345 into Measurement Mode by writing 0x08 to the POWER_CTL register.
  writeTo(POWER_CTL, 0x08);
}

void loop()
{
  readAccel(); // read the x/y/z tilt
  delay(500); // only read every 0,5 seconds
}

void readAccel() {
  uint8_t howManyBytesToRead = 6;
  readFrom( DATAX0, howManyBytesToRead, _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
  int x = (((int)_buff[1]) << 8) | _buff[0];   
  int y = (((int)_buff[3]) << 8) | _buff[2];
  int z = (((int)_buff[5]) << 8) | _buff[4];
  Serial.print("x: ");
  Serial.print( x );
  Serial.print(" y: ");
  Serial.print( y );
  Serial.print(" z: ");
  Serial.println( z );
}

void writeTo(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(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
}

Here is my breadboard:

I confess to being a bit of a newbie here, and I may have bitten off more than I can chew, but would welcome any advice or guidance. Thanks!

~ M

I’m super curious… the headers on the breakout board does not look like it is soldered on?!

Also, maybe cs is not connected correctly. (I have yet to check the datasheet)

Hey I was also wondering about that I am doing something thing similar. Have you fixed it.
Thanks Meth

Bringing this thread alive… As I haven’t been able to work with this Sensor… Has anyone figured it out yet?
Both with library and no library examples I’m unable to get any readings at all.

Thanks

I feel so silly… Forgot my pullups, sorry guys.

Hi Iv4n,

I have the same sensor. Do you have it working? Can you share your code? I’m having trouble.

thanks, Scott

Make sure you have good solder connections. That was my problem.