Skip to content

Commit 017f862

Browse files
sebromerodpgeorge
authored andcommitted
lsm6dsox: Add pedometer example code.
Signed-off-by: Sebastian Romero <s.romero@arduino.cc>
1 parent 338338f commit 017f862

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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, set debounce steps to 5, and enable interrupts on both INT1 and INT2.
21+
# Default debounce steps is 10. This means that after a step is detected, the pedometer
22+
# will ignore any new steps for the next 5 step detections. This can help to filter out
23+
# false positives and improve step counting accuracy.
24+
# If you just want to enable the pedometer, simply call lsm.pedometer_config(enable=True).
25+
lsm.pedometer_config(debounce=5, int1_enable=True, int2_enable=True)
26+
27+
# Register interrupt handler on a Pin. e.g. D8
28+
# The interrupt pins are push-pull outputs by default that go low when a step is detected.
29+
# You can connect either INT1 or INT2 to the interrupt pin.
30+
interrupt_pin = Pin("D8", Pin.IN) # Change this to your desired interrupt pin.
31+
interrupt_fired = False # Flag to indicate if the interrupt has been fired.
32+
33+
34+
def on_step_detected(pin):
35+
global interrupt_fired
36+
interrupt_fired = True
37+
38+
39+
# Configure the interrupt pin to trigger on falling edge (active low) when a step is detected.
40+
interrupt_pin.irq(trigger=Pin.IRQ_FALLING, handler=on_step_detected)
41+
42+
last_steps = None # Keep track of the last step count to detect changes.
43+
44+
while True:
45+
if interrupt_fired:
46+
print("Step detected!")
47+
interrupt_fired = False # Reset the flag after handling the interrupt.
48+
49+
steps = lsm.steps()
50+
51+
if steps != last_steps:
52+
print(f"Steps: {steps}")
53+
last_steps = steps
54+
55+
time.sleep_ms(100)

0 commit comments

Comments
 (0)