LIS2DH short Config

Hey,

i try to read the ZAxis L/H Registers with a Mega.

First Step is to set config 1 Register to 124 (01111100) and enable the Z Achsis.
Second Step is reading the Registers 2C/2D.
But my code isnt working. Hope anybody can help me.

#include <Wire.h>
byte1 data;
byte2 data;
void setup(){
  Serial.begin(57600);
  Serial.println("Start");
  Wire.begin();
  Wire.beginTransmission(0x19); 
  Wire.write(0x32);  //Adresse im Speicher
  Wire.write(0x124);
  Wire.endTransmission();

}

void loop(){
Wire.beginTransmission(0x19);
Wire.write(0x44);     //Speicherzelle bei der das Lesen anfängt
Wire.endTransmission();

Wire.requestFrom(0x19, 1);
   byte data = Wire.read();
    Serial.println(data1);
    
Wire.beginTransmission(0x19);
Wire.write(0x44);     //Speicherzelle bei der das Lesen anfängt
Wire.endTransmission();

Wire.requestFrom(0x19, 1);
   byte data = Wire.read();
    Serial.println(data2);




                   

delay(500);

}
 byte data = Wire.read();
    Serial.println(data1);

DOH!

Mark

byte1 data;
byte2 data;

DOHH

   byte data = Wire.read();
    Serial.println(data2);

And again DOH!

Mark

my code is silly, i know. :slight_smile:

Here is the new one.

Can you say me how i can enable the z axis over i2c in the correct way?

#include <Wire.h>

void setup(){
  Serial.begin(9600);
  Serial.println("Start");
  Wire.begin();
Wire.beginTransmission(_device);
Wire.write(_ctrlRegister);  
Wire.write(_configbits);
Wire.endTransmission();
}

void loop(){

Wire.beginTransmission(_device);
Wire.write(_zAxis);     
Wire.endTransmission();

Wire.requestFrom(_device, 1);
if (Wire.available())
{
   byte zaxis = Wire.read();
   Serial.println(zaxis);
}


delay(500);
}

Without you posting all the code, no :slight_smile: Please press Ctrl+T before you post it. Looks better, doesn't it?

Ok, this is the whole code.
...with CTRL + T :slight_smile:

#include <Wire.h>

void setup() {
  Serial.begin(9600);
  Serial.println("Start");
  Wire.begin();
  Wire.beginTransmission(_device);
  Wire.write(_ctrlRegister);
  Wire.write(_configbits);
  Wire.endTransmission();
}

void loop() {

  Wire.beginTransmission(_device);
  Wire.write(_zAxis);
  Wire.endTransmission();

  Wire.requestFrom(_device, 1);
  if (Wire.available())
  {
    byte zaxis = Wire.read();
    Serial.println(zaxis);
  }


  delay(500);
}

That still can't be all the code ::slight_smile: Where is '_device' defined? Or '_zAxis'? Or all the other variables...

This is with the Adress and Registers:

0x19 -> 19h-> Device Adress
0x20-> Config Register 1 Adress
0x7C-> 01111100, The Bits to config the Device how the description in Datasheet of LIS2DH
0x2C-> The OUT_Z_L Register
0x2D-> The OUT_Z_H Register

Datasheet: https://www.st.com/resource/en/datasheet/lis2dh.pdf

#include <Wire.h>

void setup() {
  Serial.begin(9600);
  Serial.println("Start");
  Wire.begin();
  Wire.beginTransmission(0x19);
  Wire.write(0x20);
  Wire.write(0x7c);
  Wire.endTransmission();
}

void loop() {

  Wire.beginTransmission(0x19);
  Wire.write(0x2C);
  Wire.endTransmission();

  Wire.requestFrom(0x19, 1);
  if (Wire.available())
  {
    byte zaxis = Wire.read();
    Serial.println(zaxis);
  }


  delay(500);
}

I was perfectly fine with variables in everything. Made it much more readable. They just where not defined anywhere which made the code uncompileable...

But what exactly is the problem you're facing? Is it printing the wrong value? If so, how do you know?

The Problem is i see in SerialPrint Window only:

-My println("start") is not at the beginning.
-Im not sure the config register is right.
-zAchsis...only zeros

Ok, i think its done.
I read the wrong register. I get some z achsis Data from register 2D.

Next Step is understanding what H & L means and how to deal with it.

Thank you so much for your Help. :slight_smile:

High and Low. Every read is just a byte aka 8-bit. Which is not enough if set to a resolution of 12-bit :slight_smile:

int totalOut = (outH << (setResolution - 8)) | (outL >> (16 - setResolution ));