Simple test
Ensure your device works with this simple test.
examples/lis331_simpletest.py
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4import time
5
6import board
7
8import adafruit_lis331
9
10i2c = board.I2C() # uses board.SCL and board.SDA
11# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
12lis = adafruit_lis331.LIS331HH(i2c)
13
14while True:
15 print(
16 f"Acceleration : X: {lis.acceleration[0]:.2f}, Y:{lis.acceleration[1]:.2f}, Z:{lis.acceleration[2]:.2f} ms^2"
17 )
18 time.sleep(0.1)
Low Pass Filter
Example showing a low pass filter example
examples/lis331_low_pass_filter.py
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4import time
5
6import board
7
8from adafruit_lis331 import LIS331HH, Frequency, Rate
9
10i2c = board.I2C() # uses board.SCL and board.SDA
11# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
12
13# un-comment the sensor you are using
14# lis = H3LIS331(i2c)
15lis = LIS331HH(i2c)
16
17# `data_rate` must be a `LOWPOWER` rate to use the low-pass filter
18lis.data_rate = Rate.RATE_LOWPOWER_10_HZ
19# next set the cutoff frequency. Anything changing faster than
20# the specified frequency will be filtered out
21lis.lpf_cutoff = Frequency.FREQ_74_HZ
22
23# Once you've seen the filter do its thing, you can comment out the
24# lines above to use the default data rate without the low pass filter
25# and see the difference it makes
26
27while True:
28 print(lis.acceleration) # plotter friendly printing
29 time.sleep(0.002)
High Pass Filter
Example showing a high pass filter example
examples/lis331_high_pass_filter.py
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4import time
5
6import board
7
8import adafruit_lis331
9
10i2c = board.I2C() # uses board.SCL and board.SDA
11# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
12# un-comment the sensor you are using
13# lis = H3LIS331(i2c)
14lis = adafruit_lis331.LIS331HH(i2c)
15
16# use a nice fast data rate to for maximum resolution
17lis.data_rate = adafruit_lis331.Rate.RATE_1000_HZ
18
19# enable the high pass filter without a reference or offset
20lis.enable_hpf(True, cutoff=adafruit_lis331.RateDivisor.ODR_DIV_100, use_reference=False)
21
22# you can also uncomment this section to set and use a reference to offset the measurements
23# lis.hpf_reference = 50
24# lis.enable_hpf(True, cutoff=RateDivisor.ODR_DIV_100, use_reference=True)
25
26
27# watch in the serial plotter with the sensor still and you will see the
28# z-axis value go from the normal around 9.8 with the filter off to near zero with it
29# enabled. If you have a reference enabled and set, that will determind the center point.
30
31# If you shake the sensor, you'll still see the acceleration values change! This is the
32# Filter removing slow or non-changing values and letting through ones that move more quickly
33
34while True:
35 print(lis.acceleration) # plotter friendly printing
36 time.sleep(0.02)