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
 5import board
 6import adafruit_lis331
 7
 8i2c = board.I2C()  # uses board.SCL and board.SDA
 9# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
10lis = adafruit_lis331.LIS331HH(i2c)
11
12while True:
13    print("Acceleration : X: %.2f, Y:%.2f, Z:%.2f ms^2" % lis.acceleration)
14    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
 5import board
 6from adafruit_lis331 import LIS331HH, Rate, Frequency
 7
 8i2c = board.I2C()  # uses board.SCL and board.SDA
 9# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
10
11# un-comment the sensor you are using
12# lis = H3LIS331(i2c)
13lis = LIS331HH(i2c)
14
15# `data_rate` must be a `LOWPOWER` rate to use the low-pass filter
16lis.data_rate = Rate.RATE_LOWPOWER_10_HZ
17# next set the cutoff frequency. Anything changing faster than
18# the specified frequency will be filtered out
19lis.lpf_cutoff = Frequency.FREQ_74_HZ
20
21# Once you've seen the filter do its thing, you can comment out the
22# lines above to use the default data rate without the low pass filter
23# and see the difference it makes
24
25while True:
26    print(lis.acceleration)  # plotter friendly printing
27    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
 5import board
 6import adafruit_lis331
 7
 8i2c = board.I2C()  # uses board.SCL and board.SDA
 9# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
10# un-comment the sensor you are using
11# lis = H3LIS331(i2c)
12lis = adafruit_lis331.LIS331HH(i2c)
13
14# use a nice fast data rate to for maximum resolution
15lis.data_rate = adafruit_lis331.Rate.RATE_1000_HZ
16
17# enable the high pass filter without a reference or offset
18lis.enable_hpf(
19    True, cutoff=adafruit_lis331.RateDivisor.ODR_DIV_100, use_reference=False
20)
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)