Hi guys, I'm a student in need of help : I have a project and I need to collect data from an accelerometer. The accelerometer that I'm using is the LIS331.
I saw an other topic on the forum about this sensor but it didn't help me. Let me explain you the problem :
I found a program (the same for the guy in the other topic, attached at the end of my message) which is using a library and works perfectly but when I'll need to explain how it works in my project review I'm sure I won't be able to do it. So i prefer to have my own program and be sure to understand everything.
So here is my program : I'll attach the datasheet of the accelerometer.
#include <Wire.h>
#define adress 0x31 // transmit to device #49 (0x31)
void setup()
{
Wire.begin(); // join i2c bus
Serial.begin(9600);
config();
}
void loop()
{
dispvalues();
delay(100);
}
void config(void)
{
Wire.beginTransmission(adress);
Wire.write(0x20); //going to CTRL_REG1
Wire.write(0x27); //default value of the register is 0x07 which means that X,Y,Z axis are enabled
//but the power is down so i need to change power mode by setting it to normal mode
//which is 0x27 if you look at page 24 of the datasheet.
Wire.endTransmission();
delay(15);
Wire.beginTransmission(adress);
Wire.write(0x21); //going to CTRL_REG2
Wire.write(0x00); //begining to be lost here I don't know if i need to access to all the
//ctrl registers so I don't write anything in this one. Even if i need to access to several
//registers I don't know if it is in a particular sequence.
Wire.endTransmission();
}
void dispvalues(void)
{
Serial.print("X=");
Serial.print(readX());
Serial.print("\t");
Serial.print("Y=");
Serial.print(readY());
Serial.print("\t");
Serial.print("Z=");
Serial.print(readZ());
Serial.println();
}
int readX(void)
{
int XL, XH;
Wire.beginTransmission(adress);
Wire.write(0x28);//going to OUT_X_L REG
Wire.endTransmission();
delay(10);
Wire.requestFrom(adress, 1);
while(Wire.available())
{
XL = Wire.read();
}
delay(10);
Wire.beginTransmission(adress);
Wire.write(0x29);//going to OUT_X_H REG
Wire.endTransmission();
delay(10);
Wire.requestFrom(adress, 1);
while(Wire.available())
{
XH = Wire.read();
}
int xout = (XL|(XH << 8));
return xout;
}
int readY(void)
{
int YL, YH;
Wire.beginTransmission(adress);
Wire.write(0x2A);//going to OUT_Y_L REG
Wire.endTransmission();
delay(10);
Wire.requestFrom(adress, 1);
while(Wire.available())
{
YL = Wire.read();
}
delay(10);
Wire.beginTransmission(adress);
Wire.write(0x2B);//going to OUT_Y_H REG
Wire.endTransmission();
delay(10);
Wire.requestFrom(adress, 1);
while(Wire.available())
{
YH = Wire.read();
}
int yout = (YL|(YH << 8));
return yout;
}
int readZ(void)
{
int ZL, ZH;
Wire.beginTransmission(adress);
Wire.write(0x2C);//going to OUT_Z_L REG
Wire.endTransmission();
delay(10);
Wire.requestFrom(adress, 1);
while(Wire.available())
{
ZL = Wire.read();
}
delay(10);
Wire.beginTransmission(adress);
Wire.write(0x2D);//going to OUT_Z_H REG
Wire.endTransmission();
delay(10);
Wire.requestFrom(adress, 1);
while(Wire.available())
{
ZH = Wire.read();
}
int zout = (ZL|(ZH << 8));
return zout;
}
As you can see I'm not sure about what I did accessing the registers and configuring all of this. I can ensure you that the connections are correct because when I use the program with the library I have my X,Y and Z values displayed correctly. I repost the link of the library because it's clearer to read it : LIS331.h LIS331.cpp
If someone could help me I would be glad because I feel I'm close to make it works
LIS331.h (2.72 KB)
LIS331.cpp (5.83 KB)
AccelerometerValues.ino (334 Bytes)
LIS331HH datasheet.pdf (678 KB)