ADXL345 with the Arduino ADK using I2c

Hi, i was wondering if someone could help me i’ve been trying to get my accelerometer working for days trying different code on the forum but nothing has worked so far… i did however find some open source code online from github using I2C and i was thinking i could stick to this but its giving me “ASCII junk” on the terminal

This is my code i have followed the pin configuration exactly as well

// Cabling for i2c using Sparkfun breakout with an Arduino Mega / Mega ADK:
// Arduino <-> Breakout board
// Gnd      -  GND
// 3.3v     -  VCC
// 3.3v     -  CS
// 20       -  SDA
// 21       -  SLC

#include <Wire.h>

#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
}

can this code give me numerical acceleration values on the terminal or do i have to modify it a little before trying again

Thanks,
Shaz

but its giving me "ASCII junk"

Can you be more specific or provide an example?

Have you got the correct line speed selected?

seems weird : Serial.begin(57000);

I doubt your serial monitor is set at that speed.

Thanks for the tip that actually helped see all the data coming in which i think the real problem is now that the serial monitor is showing all zeros now.... I'll think about this problem before posting again (fyi i forgot to mention that i'm really new to the arduino)

Could you post a schematic of your wiring ? Did you put pull-up resistors on the SDA and SCL lines ?

Arduino<–> AdaFruit Breakout
Gnd - GND
3.3v - VCC
3.3v - CS
20 - SDA
21 - SLC

What would you recommend for pull-up resistors, because i didn’t think we needed to do that?

4k7 should be OK

I’m using this post because I’m having the same problems described from the other user but I’m a electronic noob so I don’t understand what it means to pull-up some resistors. Can anyone please show me an example of something similar because basically my problem is connect the 3,3v to both the CS and CVV of the accelerometer (ADXL345). I’m using an Arduino Mega and the same example code posted above.

This is my configuration:

Pull-up resistors are resistors that go from a pin on an IC or in this instance a break-out board to power (3.3V or 5V usually) this helps components either stabilize and reduce noise in the circuit or add more current going into the sensor

my problem is connect the 3,3v to both the CS and CVV of the accelerometer (ADXL345)

Sp. "Vcc".

If it's a 3.3 volt device, you'll need level converters if you want to connect it to a 5volt device.