|
| 1 | +""" |
| 2 | +LSM6DSOX IMU Pedometer Example. |
| 3 | +
|
| 4 | +This example demonstrates how to use the built-in pedometer feature of the LSM6DSOX IMU. |
| 5 | +The pedometer counts the number of steps taken based on the accelerometer data |
| 6 | +and can generate interrupts when a step is detected. |
| 7 | +
|
| 8 | +Copyright (C) Arduino s.r.l. and/or its affiliated companies |
| 9 | +""" |
| 10 | + |
| 11 | +import time |
| 12 | +from lsm6dsox import LSM6DSOX |
| 13 | + |
| 14 | +from machine import Pin, I2C |
| 15 | + |
| 16 | +lsm = LSM6DSOX(I2C(0)) |
| 17 | +# Or init in SPI mode. |
| 18 | +# lsm = LSM6DSOX(SPI(5), cs=Pin(10)) |
| 19 | + |
| 20 | +# Enable the pedometer feature. |
| 21 | +lsm.pedometer_enabled = True |
| 22 | + |
| 23 | +# Set debounce steps to 5 (default is 10). |
| 24 | +# This means that after a step is detected, the pedometer will ignore any new steps for the next 5 step detections. |
| 25 | +# This can help to filter out false positives and improve step counting accuracy. |
| 26 | +lsm.pedometer_debounce_steps = 5 |
| 27 | + |
| 28 | +# Enable interrupts for step detection on both INT1 and INT2 pins. |
| 29 | +lsm.pedometer_int1_enabled = True |
| 30 | +lsm.pedometer_int2_enabled = True |
| 31 | + |
| 32 | +# Register interrupt handler on a Pin. e.g. D8 |
| 33 | +interrupt_pin = Pin("D8", Pin.IN, Pin.PULL_UP) |
| 34 | + |
| 35 | + |
| 36 | +def on_step_detected(pin): |
| 37 | + print("Step detected!") |
| 38 | + |
| 39 | + |
| 40 | +# Configure the interrupt pin to trigger on falling edge (active low) when a step is detected. |
| 41 | +interrupt_pin.irq(trigger=Pin.IRQ_FALLING, handler=on_step_detected) |
| 42 | + |
| 43 | +last_steps = 0 # Keep track of the last step count to detect changes. |
| 44 | + |
| 45 | +while True: |
| 46 | + steps = lsm.pedometer_steps |
| 47 | + |
| 48 | + if steps != last_steps: |
| 49 | + print(f"Steps: {steps}") |
| 50 | + last_steps = steps |
| 51 | + |
| 52 | + time.sleep_ms(100) |
0 commit comments