Skip to content

Commit b4a401a

Browse files
committed
lsm6dsox: Add pedometer example.
Signed-off-by: Sebastian Romero <s.romero@arduino.cc>
1 parent 4105af7 commit b4a401a

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
def on_step_detected(pin):
36+
print("Step detected!")
37+
38+
# Configure the interrupt pin to trigger on falling edge (active low) when a step is detected.
39+
interrupt_pin.irq(trigger=Pin.IRQ_FALLING, handler=on_step_detected)
40+
41+
last_steps = 0 # Keep track of the last step count to detect changes.
42+
43+
while True:
44+
steps = lsm.pedometer_steps
45+
46+
if steps != last_steps:
47+
print(f"Steps: {steps}")
48+
last_steps = steps
49+
50+
time.sleep_ms(100)

0 commit comments

Comments
 (0)