help with LIS2DH accelerometer

Hi everyone. I’m really not good with this stuff but I need to make this work for a project. I tried something but I have little knowledge of programming… Can anyone help me with the code? I only need read the axis. Thank you.

    #include <Wire.h> //I2C Arduino Library 
 
 
#define address 0x18 //I2C 7bit address 
 
 
void setup(){ 
  //Initialize Serial and I2C communications 
  Serial.begin(9600); 
  Wire.begin(); 
   
  //Put into the correct operating mode 
  Wire.beginTransmission(address); //open communication with 
  Wire.write(0x20);  
  Wire.write(0x27);  
  Wire.endTransmission(); 
}     
 
 
void loop(){ 
   
  int x,y,z; //triple axis data 
 
 
  Wire.beginTransmission(address); 
  Wire.write(0x28); 
  Wire.endTransmission(); 
   
  
 //Read data from each axis 
  Wire.requestFrom(address, 6); 
  if(6<=Wire.available()){ 
    x = Wire.read()<<8;  
    x |= Wire.read();  
    z = Wire.read()<<8;  
    z |= Wire.read();  
    y = Wire.read()<<8;  
    y |= Wire.read();  
  } 
   
  //Print out values of each axis 
  Serial.print("x: "); 
  Serial.print(x); 
  Serial.print("  y: "); 
  Serial.print(y); 
  Serial.print("  z: "); 
  Serial.println(z); 
   
  delay(250); 
}

Hi nowords

What happens when you run the program?

Can you post a diagram of how your hardware is connected.

Thanks

Ray

Couple of comments on the code, based on what I can see in the datasheet.

 Wire.write(0x28);

Since you go on to read multiple bytes starting from this register address (for OUT_X_L), I think you need to set bit 7 in the address. So the value should be 0xA8.

The OUT_X_H register comes after OUT_X_L. High byte is below low byte.

So this code …

    x = Wire.read()<<8;  
    x |= Wire.read();

needs to become …

    x = Wire.read();  
    x |= Wire.read() <<8;

Lastly, the registers are in the order X, Y then Z. Your code is reading x, z then y. Need to swap the order.

Regards

Ray

Thank you man, but still is not working. Maybe I'm doing the connections wrong.

vdd ->> 3.3V
sdo ->> gnd
sda ->> A4
scl ->> A5
gnd ->> gnd

I'm getting this all the time
x:1500 y:0 z: -16384

vdd ->> 3.3V
sdo ->> gnd
sda ->> A4
scl ->> A5
gnd ->> gnd

The CS pin needs to be connected (or pulled down) to ground to select I2C mode.

Is the LIS2DH on a breakout board? Do you have a link to that? It would be worth checking if it has pullup resistors fitted on SDA and SCL.

And can you please post your latest code.

Sorry, I forgot to write the CS, it is connected to the ground.

link to the breakout board

#include <Wire.h> //I2C Arduino Library 
 
 
#define address 0x18 //I2C 7bit address 
 
 
void setup(){ 
  //Initialize Serial and I2C communications 
  Serial.begin(9600); 
  Wire.begin(); 
   
  //Put into the correct operating mode 
  Wire.beginTransmission(address); //open communication with 
  Wire.write(0x20);  
  Wire.write(0x27);  
  Wire.endTransmission(); 
}     
 
 
void loop(){ 
   
  int x,y,z; //triple axis data 
 
 
  Wire.beginTransmission(address); 
  Wire.write(0xA8); 
  Wire.endTransmission(); 
   
  
 //Read data from each axis 
  Wire.requestFrom(address, 6); 
  if(6<=Wire.available()){ 
    x = Wire.read();  
    x |= Wire.read() <<8;  
    y |= Wire.read();
    y = Wire.read()<<8;     
    z |= Wire.read(); 
    z = Wire.read()<<8; 
  } 
   
  //Print out values of each axis 
  Serial.print("x: "); 
  Serial.print(x); 
  Serial.print("  y: "); 
  Serial.print(y); 
  Serial.print("  z: "); 
  Serial.println(z); 
   
  delay(450); 
}

Try these changes to your assignment statements. What output do you get?

    x = (int)Wire.read();  
    x |= (int)Wire.read()<<8;  
    y = (int)Wire.read();         // Note change in operator on this and next three statements
    y |= (int)Wire.read()<<8;     
    z = (int)Wire.read(); 
    z |= (int)Wire.read()<<8;

I got this...

x:21248 y:212 z:1510
x:21248 y:212 z:1510
x:21248 y:212 z:1510

Do the values change if you move the board around?

In particular, what readings do you get if you turn the board upside down?

And to confirm if the I2C is working OK, try this code which should just read and print the ID register on the device. It should give a result of 0x33.

#include <Wire.h>
#define address 0x18

void setup()
{ 
  Serial.begin(9600); 
  Wire.begin(); 
   
  //Put into the correct operating mode 
  Wire.beginTransmission(address); 
  Wire.write(0x20);  
  Wire.write(0x27);  
  Wire.endTransmission(); 

  // Read device "who am I?" register
  Wire.beginTransmission(address);
  Wire.write(0x0F);  
  Wire.endTransmission(); 
  Wire.requestFrom(address, 1); 
  if (Wire.available() > 0){ 
    Serial.print("WHO_AM_I: ");
    Serial.println(Wire.read(), HEX);
  }
}

Found this application note which may be useful.

AN3308: LIS3DH: MEMS digital output motion sensor ultra low-power high performance 3-axis “nano” accelerometer

I tried your code and the device is not given the result. So the I2C is the problem?
Thank you all for your help.

What result is it giving?

Edit

And what about my other questions?

Do the X, Y, Z values change if you move the board around?
In particular, what readings do you get if you turn the board upside down?