Skip to content

Instantly share code, notes, and snippets.

@stonehippo
Created November 17, 2024 21:19
Show Gist options
  • Save stonehippo/d618f16a4cba09d0e993fa889342fe38 to your computer and use it in GitHub Desktop.
Save stonehippo/d618f16a4cba09d0e993fa889342fe38 to your computer and use it in GitHub Desktop.
Notes on writing a CircuitPython driver for the LSM6DSV16X IMU.

LSM6DSV16X CircuitPython Driver

Notes on writing a CircuitPython driver for the LSM6DSV16X IMU.

First steps

For starts, I went to the Adafruit CircuitPython driver for LSM6DS-class chips. It covers several models with similar setups, so it seemed liked a good place to start.

Once I figured out that the Sparkfun dev board that I'm using was set up for the alternate I2C address (0x6b instead of 0x6a) and that I needed to use the chip ID specific to the LSM6DSV16X (0x70), I was able to connect with the LSM6DS library, using a new implemenation file (which really only setup the new CHIP_ID):

import board
# use a new class implementation that I add to the Adafruit library
from adafruit_lsm6ds.lsm6dsv16x import LSM6DSV16X 

i2c = board.STEMMA_I2C() # or whatever way you get an I2C object in CP
imu = LSM6DSV16X(i2c, address = 0x6b)

print(imu.accelerate)
print(imu.gyro)

This works, sort of. I was getting data out of the accelerometer side of the IMU, but the gyro was returning nothing ((0,0,0)). Hmm. Something's not right, clearly.

Looking at the datasheets

I decided to do a little digging into datasheets to see what was happening. I grabbed the sheet for the LSM6DSV16X and the LSM6DS032, one of the chips already supported by the Adafruit library. First thing I noticed is that data rates for the LSM6DS032 are very different from the LSM6DSV16X. The V16X seems to use a simpler doubling scheme, starting at 7.5HZ, then 15HZ, 30HZ, etc. Perhaps the defalt data rate setting for the gyro was wrong?

It turns out it was, but it's more complicated than that. The out registers for data from the accelerometer and gyro seem to be the same, but the control registers are somewhat different. How they set up the parameters for the IMU needs to differ.

So, know I've got to deal with a couple of questions. In addition to figuring out how the new registers work compared with the old ones, I also need to decide if there's enough shared code between the existing library code and what I need to do to get this all working. And if things are really different, should the new driver implementation get included in a pull request to the Adafruit library, as I originially intended?

I'm going to skip over the architectural question for now. I think the LSM6DSV16X driver belongs alongside its cousins, even if it's not exactly the same, which would make shared issues in the code easier. But there are some things to work out. For example, the data rate definitons are in the parent class. If I need to come up with a whole new set, that seems…

…not great.

I guess it's time to dive in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment