cancel
Showing results for 
Search instead for 
Did you mean: 

LSM6DSV16X unable to interact via I2C in Python on Raspberry Pi

azlan777
Associate II

Hi!

I have set up and connected a LSM6DSV16X via I2C to my raspberry pi, its on bus 10, address 0x6a. The sensor seems to have been detected via an I2C dump but the identifier (which should be 0x70) is stuck at 0x00 and I'm not sure why. 

azlan@argo:~ $ i2cdetect -y 10
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- UU -- -- -- -- -- -- --
40: -- -- -- -- -- UU -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- 6a -- -- -- -- --
70: -- -- -- -- -- -- -- --

azlan@argo:~ $ i2cdump -f 6 0x6a
0 1 2 3 4 5 6 7 8 9 a b c d e f 0123456789abcdef
00: 48 05 3d 3a 20 13 5e 9d 03 44 93 e2 80 13 00 00 H?=: ?^??D????..
10: 00 00 00 08 39 ff ff ff ff ff ff ff ff ff ff ff ...?9...........
20: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................
30: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................
40: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................
50: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................
60: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................
70: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................
80: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................
90: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................
a0: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................
b0: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................
c0: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................
d0: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................
e0: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................
f0: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................

 

I have also attached a schematic of the connection, I am trying to use it in Mode 1 as per the data sheet. 

 

Here is my python code: 

import smbus2
import time

# I2C bus and device address
I2C_BUS = 6
LSM6DSV16X_ADDRESS = 0x6A

# LSM6DSV16X Registers
WHO_AM_I = 0x0F
CTRL1_XL = 0x10
CTRL2_G = 0x11
CTRL3_C = 0x12
OUTX_L_G = 0x22
OUTX_H_G = 0x23
OUTY_L_G = 0x24
OUTY_H_G = 0x25
OUTZ_L_G = 0x26
OUTZ_H_G = 0x27

# Create an smbus2 object
bus = smbus2.SMBus(I2C_BUS)

def twos_complement(val, bits‌‌
"""Compute the 2's complement of a value if it's negative."""
if (val & (1 << (bits - 1))) != 0: # if sign bit is set
val = val - (1 << bits) # compute negative value
return val

def setup_lsm6dsv16x():
"""Initializes and configures the LSM6DSV16X sensor."""
try:
# Check device ID
who_am_i = bus.read_byte_data(LSM6DSV16X_ADDRESS, WHO_AM_I)
if who_am_i != 0x6A: # This is the expected WHO_AM_I value for this sensor
print(f"LSM6DSV16X not found. Found device with ID: {hex(who_am_i)}")
return False

print("LSM6DSV16X found!")

# --- Gyroscope Configuration ---
# CTRL2_G: Set Gyroscope ODR (Output Data Rate) and full-scale selection
# ODR = 208 Hz (0b0101 << 4), FS = 2000 dps (0b11 << 2)
# We will use the default value for this example which is 208Hz, 2000dps
bus.write_byte_data(LSM6DSV16X_ADDRESS, CTRL2_G, 0x5C)

# CTRL3_C: Control register 3
# BDU = 1 (Block Data Update) to ensure data registers are not updated during read
bus.write_byte_data(LSM6DSV16X_ADDRESS, CTRL3_C, 0x44) # BDU and IF_INC enabled by default

print("LSM6DSV16X configured successfully.")
return True

except IOError as e:
print(f"Error accessing I2C device: {e}")
return False

def read_gyro_data():
"""Reads the raw gyroscope data from the sensor."""
try:
# Read the six raw data registers
gyro_x_l = bus.read_byte_data(LSM6DSV16X_ADDRESS, OUTX_L_G)
gyro_x_h = bus.read_byte_data(LSM6DSV16X_ADDRESS, OUTX_H_G)
gyro_y_l = bus.read_byte_data(LSM6DSV16X_ADDRESS, OUTY_L_G)
gyro_y_h = bus.read_byte_data(LSM6DSV16X_ADDRESS, OUTY_H_G)
gyro_z_l = bus.read_byte_data(LSM6DSV16X_ADDRESS, OUTZ_L_G)
gyro_z_h = bus.read_byte_data(LSM6DSV16X_ADDRESS, OUTZ_H_G)

# Combine the high and low bytes to get the raw values
raw_gyro_x = (gyro_x_h << 8) | gyro_x_l
raw_gyro_y = (gyro_y_h << 8) | gyro_y_l
raw_gyro_z = (gyro_z_h << 8) | gyro_z_l

# Convert from 2's complement
gyro_x = twos_complement(raw_gyro_x, 16)
gyro_y = twos_complement(raw_gyro_y, 16)
gyro_z = twos_complement(raw_gyro_z, 16)

return (gyro_x, gyro_y, gyro_z)

except IOError as e:
print(f"Error reading gyro data: {e}")
return (None, None, None)

def main():
"""Main function to setup sensor and read data."""
if not setup_lsm6dsv16x():
return

while True:
gyro_x, gyro_y, gyro_z = read_gyro_data()
if gyro_x is not None:
# The sensitivity for 2000 dps is 70 mdps/LSB
sensitivity = 70.0

# Convert raw data to dps
dps_x = (gyro_x * sensitivity) / 1000.0
dps_y = (gyro_y * sensitivity) / 1000.0
dps_z = (gyro_z * sensitivity) / 1000.0

print(f"Gyro X: {dps_x:.2f} dps, Y: {dps_y:.2f} dps, Z: {dps_z:.2f} dps")

time.sleep(0.1)

if __name__ == '__main__':
main()

Edited to apply source code formatting - please see How to insert source code for future reference.

Note that the Source Code editor does support Python - among other languages.

Note also the 'Preformatted' paragraph format for tabulated text, ASCII art, etc.

1 ACCEPTED SOLUTION

Accepted Solutions
Federica Bossi
ST Employee

Hi @azlan777 ,

Can you share the wiring diagram? I don't understand what bus 10 is. If it's not the standard one, you may be missing the pull-up resistors.

Could you check the SDA and SCL voltages? If you are not using the standard GPIOs, you need to add pull-ups.

In order to give better visibility on the answered topics, please click on 'Accept as Solution' on the reply which solved your issue or answered your question.

View solution in original post

2 REPLIES 2
Federica Bossi
ST Employee

Hi @azlan777 ,

Can you share the wiring diagram? I don't understand what bus 10 is. If it's not the standard one, you may be missing the pull-up resistors.

Could you check the SDA and SCL voltages? If you are not using the standard GPIOs, you need to add pull-ups.

In order to give better visibility on the answered topics, please click on 'Accept as Solution' on the reply which solved your issue or answered your question.
Andrew Neil
Super User

Are you sure that you're using the correct address and in the format that RPi requires ?

https://community.st.com/t5/imaging-sensors/vl53l8cx-not-working-on-nrf9151-and-getting-naks/m-p/814396/highlight/true#M5695

https://community.st.com/t5/imaging-sensors/vl53l8cx-not-working-on-nrf9151-and-getting-naks/m-p/814452/highlight/true#M5697

 

And, as @Federica Bossi said, do you have the correct pullups?

 

Have you checked the comms with an oscilloscope and/or logic analyser?

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.