lsm6dsox — LSM6DSOX 6-axis IMU
Driver for the ST LSM6DSOX iNEMO 6-axis inertial measurement unit (3-axis accelerometer plus 3-axis gyroscope) over I2C or SPI. In addition to the basic IMU output, the driver exposes the on-chip embedded functions: a configurable pedometer (step counter) and the Machine Learning Core (MLC), which can be programmed via UCF files exported from ST’s Unico-GUI tool.
Example:
import time
from machine import Pin, SPI, I2C
from lsm6dsox import LSM6DSOX
# Init in I2C mode.
lsm = LSM6DSOX(I2C(0, scl=Pin(13), sda=Pin(12)))
# Or init in SPI mode.
# lsm = LSM6DSOX(SPI(5), cs=Pin(10))
while True:
print("Accelerometer: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}".format(*lsm.accel()))
print("Gyroscope: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}".format(*lsm.gyro()))
time.sleep_ms(100)
Classes
- class lsm6dsox.LSM6DSOX(bus: machine.I2C | machine.SPI, cs: machine.Pin | None = None, address: int = 0x6A, gyro_odr: float = 104, accel_odr: float = 104, gyro_scale: int = 2000, accel_scale: int = 4, ucf: str | None = None)
Construct an
LSM6DSOXinstance. The bus type is auto-detected: ifbusexposesreadfrom_memit is treated as amachine.I2C, otherwise it is treated as amachine.SPIandcsmust be supplied. The chip is soft-reset, the requested ODR and scale are programmed, and an MLC UCF file is loaded if provided.busEither a configured
machine.I2Cormachine.SPIbus.csChip-select
machine.Pinused in SPI mode. Required whenbusis an SPI instance, ignored otherwise.address7-bit I2C address of the device. Defaults to
0x6A; some boards strap the SDO pin high which selects0x6B.gyro_odrGyroscope output data rate in Hz. Must be one of
0(off),1.6,3.33,6.66,12.5,26,52,104,208,416or888.accel_odrAccelerometer output data rate in Hz. Same set of values as
gyro_odr.gyro_scaleGyroscope full-scale range in degrees-per-second. Must be one of
250,500,1000or2000.accel_scaleAccelerometer full-scale range in g. Must be one of
2,4,8or16.ucfOptional path to an ST Unico-GUI
.ucfregister-dump file. If supplied the file is parsed and applied to the MLC during construction by way ofload_mlc().
- reset() None
Issue a software reset via
CTRL3_Cand block until the reset bit clears. RaisesOSErrorif the device fails to come back within ten retries.
- set_mem_bank(bank: int) None
Switch the FUNC_CFG register bank. Used internally to access the embedded function and sensor-hub register pages; rarely needed by application code.
- set_embedded_functions(enable: bool, emb_ab: tuple[int, int] | None = None) tuple[int, int]
Enable or disable the embedded-function block. When
enableisTruethe two-byteemb_abtuple is written toEMB_FUNC_EN_A/EMB_FUNC_EN_B. WhenFalsethe current values are read, the MLC/pedometer enable bits are cleared, and the previous values are returned so they can be restored later.
- load_mlc(ucf: str) None
Apply an MLC UCF program from the file at path
ucf. The driver walks the file line-by-line, applies eachAc <reg> <val>write, then enables BDU, routes MLC events to interrupt pin 1 and turns the embedded functions back on.
- mlc_output() bytes | None
If new MLC results are available (
MLC_STATUSbit set), return the contents of the eightMLC0_SRC..MLC7_SRCregisters as a bytes-like object. Otherwise returnNone.
- pedometer_config(enable: bool = True, debounce: int = 10, int1_enable: bool = False, int2_enable: bool = False) None
Configure the embedded pedometer.
enableEnables or disables step detection on the embedded function block.
debounceStep debounce value written into
PEDO_DEB_STEPS_CONF.int1_enableIf
True, route pedometer events to theINT1pin.int2_enableIf
True, route pedometer events to theINT2pin.