1

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()
0andriy
  • 4,183
  • 1
  • 24
  • 37
Xenoshell
  • 91
  • 2
  • 13
  • 1
    Welcome to [Stack Overflow.](https://stackoverflow.com/ "Stack Overflow") This is not a code-writing or tutoring service. Without information related to the connections to the sensor, there is no way we can answer your question. See the [How To Ask a Good Question](https://stackoverflow.com/help/how-to-ask "How To Ask a Good Question") page for details on how to best help us help you. – itprorh66 Jun 30 '22 at 15:52
  • @itprorh66 hey the raspberry pi 4 is connected to the Sense HAT which has this accelerometer integrated. This is the connection, i just plugged it onto the GPIO Pins and if i use the sense hat library it works fine, but if i try to use normal I2C it doesnt – Xenoshell Jul 01 '22 at 09:20
  • This sensor has a driver in the Linux kernel. Enable it and use it in fastest possible way. – 0andriy Jul 10 '22 at 20:02
  • @0andriy how do i even use the driver if my code is in python3? Thanks for your reply, my code looks somewhat similar but i will still update it – Xenoshell Jul 11 '22 at 23:49
  • How do you use another hardware (display, keyboard, network, etc) if your code in Python3? (No, this is a rhetorical question, just do your homework) – 0andriy Jul 12 '22 at 06:51
  • Here is the description of the IIO library: https://wiki.analog.com/resources/tools-software/linux-software/libiio, and for bindings: https://wiki.analog.com/resources/tools-software/linux-software/libiio_internals#python_bindings – 0andriy Jul 12 '22 at 06:58

0 Answers0