Trouble reading Accelerometer via I2C

I have a Nano v3.0 that I am using to, among other things, communicate with the LIS302DL 3 axis accelerometer. I am using the following code (with thanks to Ben Gatti, I can’t remember where I found this):

#include <Wire.h>

#define i2cID 0x1D

//TWI (I2C) sketch to communicate with the LIS302DL accelerometer - Modified and tested Ben Gatti - 6/9/2008
//http://www.st.com/stonline/products/literature/ds/12726.pdf
//Note 5.6K pullup resister on data lines.
//Device is 3 volt was tested at 5 volts no level shifting.
//Reference claims 6 volts is pin max.

//Modified from // TWI (I2C) sketch to communicate with the LIS3LV02DQ accelerometer
//http://www.nearfuturelaboratory.com/2007/01/11/arduino-and-twi/
//Modified code from http://research.techkwondo.com/blog/julian/279
//Thanks Julian.

// Using the Wire library (created by Nicholas Zambetti)
// http://wiring.org.co/reference/libraries/Wire/index.html
// On the Arduino board, Analog In 4 is SDA, Analog In 5 is SCL
// These correspond to pin 27 (PC4/ADC4/SDA) and pin 28 (PC5/ADC5/SCL) on the Atmega8
// The Wire class handles the TWI transactions, abstracting the nitty-gritty to make
// prototyping easy.

void setup()

{

  Wire.begin(); // join i2c bus (address optional for master)
  Serial.begin(57600);

  Serial.println("Wire.begin");

  Wire.beginTransmission(i2cID);
  Wire.send(0x21); // CTRL_REG2 (21h)
  Wire.send(B01000000);
  //SPI 4/3 wire
  //1=ReBoot - reset chip defaults
  //n/a
  //filter off/on
  //filter for freefall 2
  //filter for freefall 1
  //filter freq MSB
  //filter freq LSB - Hipass filter (at 400hz) 00=8hz, 01=4hz, 10=2hz, 11=1hz (lower by 4x if sample rate is 100hz)
  Wire.endTransmission();

  Wire.beginTransmission(i2cID);
  Wire.send(0x20); // CTRL_REG1 (20h)
  Wire.send(B01000111);
  //sample rate 100/400hz
  //power off/on
  //2g/8g
  //self test
  //self test
  //z enable
  //y enable
  //x enable
  Wire.endTransmission();

}

void loop()
{

#define outXhigh 0x29
#define outYhigh 0x2B
#define outZhigh 0x2D
#define statusReg 0x27

boolean goodRead;
goodRead = false;

//----------Status Register-----------------------
  byte status;
  status = B00000000;
  //

//-------------------------------
  Wire.beginTransmission(i2cID);
  Wire.send(statusReg);
  Wire.endTransmission();

  Wire.requestFrom(i2cID, 1);
  if(Wire.available())
 {
   status = Wire.receive();
 }



//----------X Values-----------------------
  byte x_val;

//-------------------------------
  Wire.beginTransmission(i2cID);
  Wire.send(outXhigh);
  Wire.endTransmission();

Wire.requestFrom(i2cID, 1);
if(Wire.available())
 {
   x_val = Wire.receive();
 }


//----------Y Values-----------------------
  byte y_val;

//-------------------------------
  Wire.beginTransmission(i2cID);
  Wire.send(outYhigh);
  Wire.endTransmission();

Wire.requestFrom(i2cID, 1);
if(Wire.available())
 {
   y_val = Wire.receive();
 }

 //----------Z Values-----------------------
  byte z_val;

//-------------------------------
  Wire.beginTransmission(i2cID);
  Wire.send(outZhigh);
  Wire.endTransmission();

Wire.requestFrom(i2cID, 1);
if(Wire.available())
 {
   z_val = Wire.receive();
   goodRead=true;
 }
//-------------------------------


 if (goodRead==true)
 {

//Serial.print("x_val_l= "); Serial.println(x_val_l, DEC);
//Serial.print("x_val_h= "); Serial.println(x_val_h, DEC);
//Serial.print("x_val= "); Serial.println(x_val, DEC);

//Serial.print("y_val_l= "); Serial.println(y_val_l, DEC);
//Serial.print("y_val_h= "); Serial.println(y_val_h, DEC);
//Serial.print("y_val= "); Serial.println(y_val, DEC);

//Serial.print("z_val_l= "); Serial.println(z_val_l, DEC);
//Serial.print("z_val_h= "); Serial.println(z_val_h, DEC);
Serial.print("status "); Serial.println(status, BIN);
Serial.print(" val= "); Serial.print(x_val, DEC);
Serial.print(":"); Serial.print(y_val, DEC);
Serial.print(":"); Serial.println(z_val, DEC);
 }
 else
Serial.print("no data");

  delay(100);
}

Unfortunately the output looks like this when I open the Serial Monitor:

ÏÿBÒBFÿBÒBÐBÒBFÿBÒBÐBÒBFÿBÒBFÿBÒBÐBÒBÐBÒBÐBÒBÐBÒBFÿBÒBÐBÒBÐBÒBÆÐB
ÒBÆÐBÒBÆÐBÒBÐBÒBFÿBÒBÐBÒBÐBÒBFÿBÒBFÿBÒBÐBÒBFÿBÒBFÿBÒBÐBÒBFÿBÒBFÿBÒ
BÐBÒBÆÐBÒBFÿBÒBÐBÒBÆÐBÒBÐBÒBÐBÒBFÿBÒBÐBÒBÐBÒBÐBÒBFÿBÒBÐBÒBFÿBÒBÐB
ÒBÐBÒBFÿBÒBFÿBÒBÐBÒBÐBÒBFÿBÒBÐBÒBÆÐBÒBFÿBÒBFÿBÒBFÿBÒBÐBÒBÐBÒBFÿBÒB
ÐBÒBFÿBÒBÆÐBÒBÐBÒBÆÐBÒBFÿBÒBFÿBÒBFÿBÒBÐBÒBFÿBÒBÆÐBÒBÐBÒBÐBÒBÐBÒÂÖò
BÒB
'BÒBþBÒÂHþBÒBþBÒBZÐBÒBÐBÒBÐBÒBÐBÒBÐBÒBÆÐBÒBÐBÒBÐBÒBÐBÒBÐB
ÒÂÆþBÒBHþBÒBHþBÒBRôBÒÂVþBÒBÐBÒBÆÐBÒBÐBÒBÐBÒB
ÐBÒB–BÒBÂÒBÒBÆÐBÒBÆÐBÒBÐBÒB
ÿBÒB
ÿBÒB
ÿBÒB

I am a totally noob with digital communication so I would be happy to hear that it is just some silly mistake that I haven’t accounted for in the code. I’ve triple checked all the connections and such so I am sure the problem does not lie with those sorts of issues.

Can anyone help?

Best regards,

Adbo

Looks like a baud rate mismatch?

Also, there were some issues with ATmega328 serial settings, so if you have a 328 try changing the baud rate by a factor of 2 (sorry, can't remember if you should double it or half it).

-j

Good eye...that is exactly what it was. I had the baud rate in the serial monitor left on the default setting (9600) while the rate in the code was 57600.

Thanks for your help...works perfectly now!

Best,

Adbo