I have a problem while reading the my lsm9ds1 accelerator sensor registers. It always displays the same values.
My setup is a Raspberry Pi 4 and the Sense HAT which has this sensor integrated. I don't want to use the Sense HAT library but use the python3-smbus library to better understand I2C.
The registers I want to read from are OUT_X_XL, OUT_Y_XL and OUT_Z_XL which return linear values expressed as a 16-bit word in two's complement. Here is the Datasheet. I tried to convert them with two different functions I found, but got both times an error so I just wanted to see if there just would be a change if I move the sensor around.
Here is my output:
Complements Register 1 X: 221 Y: 59 Z: 254
Complements Register 2 X: 104 Y: 146 Z: 112
Complements Register 1 X: 221 Y: 59 Z: 254
Complements Register 2 X: 104 Y: 146 Z: 112
Do I have to do something different with the values or should I read the register differently? Preferably I would also like the values to be something close to float values what you would expect from an acceleration sensor. Any help is very appreciated.
Here is my code:
from smbus import SMBus
import time
i2c_adress = 0x1c # DEVICE ACCELEROMETER
# DEFINITION OF REGISTERS
OUT_X_XL1 = 0x28 # linear acceleration sensor output registers
OUT_X_XL2 = 0x29
OUT_Y_XL1 = 0x2A
OUT_Y_XL2 = 0x2B
OUT_Z_XL1 = 0x2C
OUT_Z_XL2 = 0x2D
def main():
while (True):
i2cbus = SMBus(1) # Create new I2C bus
comp_acc_x2 = i2cbus.read_byte_data(i2c_adress, OUT_X_XL2)
comp_acc_y2 = i2cbus.read_byte_data(i2c_adress, OUT_Y_XL2)
comp_acc_z2 = i2cbus.read_byte_data(i2c_adress, OUT_Z_XL2)
comp_acc_x1 = i2cbus.read_byte_data(i2c_adress, OUT_X_XL1)
comp_acc_y1 = i2cbus.read_byte_data(i2c_adress, OUT_Y_XL1)
comp_acc_z1 = i2cbus.read_byte_data(i2c_adress, OUT_Z_XL1)
print(f" Complements Register 1 X: {comp_acc_x1} Y: {comp_acc_y1} Z: {comp_acc_z1}")
print(f" Complements Register 2 X: {comp_acc_x2} Y: {comp_acc_y2} Z: {comp_acc_z2}")
time.sleep(0.5)
if __name__ == "__main__":
main()
Other code using pigpio library:
import pigpio
import time
i2c_adress = 0x1c # DEVICE ACCELEROMETER
#DEFINITION OF REGISTERS
OUT_X_XL1 = 0x28 # linear acceleration sensor output registers
OUT_X_XL2 = 0x29
OUT_Y_XL1 = 0x2A
OUT_Y_XL2 = 0x2B
OUT_Z_XL1 = 0x2C
OUT_Z_XL2 = 0x2D
def main():
pi = pigpio.pi()
acc_sensor = pi.i2c_open(1, i2c_adress)
while (True):
comp_acc_x2 = pi.i2c_read_byte_data(acc_sensor, OUT_X_XL2)
comp_acc_y2 = pi.i2c_read_byte_data(acc_sensor, OUT_Y_XL2)
comp_acc_z2 = pi.i2c_read_byte_data(acc_sensor, OUT_Z_XL2)
comp_acc_x1 = pi.i2c_read_byte_data(acc_sensor, OUT_X_XL1)
comp_acc_y1 = pi.i2c_read_byte_data(acc_sensor, OUT_Y_XL1)
comp_acc_z1 = pi.i2c_read_byte_data(acc_sensor, OUT_Z_XL1)
print(f" Complements Register 1 X: {comp_acc_x1} Y: {comp_acc_y1} Z: {comp_acc_z1}")
print(f" Complements Register 2 X: {comp_acc_x2} Y: {comp_acc_y2} Z: {comp_acc_z2}")
time.sleep(0.5)
if __name__ == "__main__":
main()