Accel Unit

This is the driver library of Accel Unit, which is used to obtain data from the acceleration sensor and support motion detection.

Support the following products:

ACCEL

UiFlow2 Example

get accel value

Open the stickcplus2_unit_accel_example.m5f2 project in UiFlow2.

This example gets the acceleration value of the Accel Unit and displays it on the screen.

UiFlow2 Code Block:

example.png

Example output:

None

MicroPython Example

get accel value

This example gets the acceleration value of the Accel Unit and displays it on the screen.

MicroPython Code Block:

 1# SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
 2#
 3# SPDX-License-Identifier: MIT
 4
 5import os, sys, io
 6import M5
 7from M5 import *
 8from hardware import *
 9from unit import AccelUnit
10
11
12label0 = None
13label1 = None
14label2 = None
15title0 = None
16label3 = None
17label4 = None
18label5 = None
19i2c0 = None
20accel_0 = None
21
22
23acc = None
24
25
26def setup():
27    global label0, label1, label2, title0, label3, label4, label5, i2c0, accel_0, acc
28
29    M5.begin()
30    label0 = Widgets.Label("x:", 4, 48, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
31    label1 = Widgets.Label("y:", 4, 88, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
32    label2 = Widgets.Label("z:", 4, 128, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
33    title0 = Widgets.Title("ACCEL UNIT", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu18)
34    label3 = Widgets.Label("label3", 24, 48, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
35    label4 = Widgets.Label("label4", 24, 88, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
36    label5 = Widgets.Label("label5", 24, 128, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
37
38    i2c0 = I2C(0, scl=Pin(33), sda=Pin(32), freq=100000)
39    accel_0 = AccelUnit(i2c0, 0x53)
40
41
42def loop():
43    global label0, label1, label2, title0, label3, label4, label5, i2c0, accel_0, acc
44    M5.update()
45    acc = accel_0.get_accel()
46    label3.setText(str(acc[0]))
47    label4.setText(str(acc[1]))
48    label5.setText(str(acc[2]))
49
50
51if __name__ == "__main__":
52    try:
53        setup()
54        while True:
55            loop()
56    except (Exception, KeyboardInterrupt) as e:
57        try:
58            from utility import print_error_msg
59
60            print_error_msg(e)
61        except ImportError:
62            print("please update to latest firmware")

Example output:

None

API

AccelUnit

class unit.accel.AccelUnit(i2c, address=83)

Bases: ADXL345

Create an AccelUnit object.

Parameters:
  • i2c (I2C) – The I2C bus the Accel Unit is connected to.

  • address (int) – The I2C address of the device. Default is 0x53.

UiFlow2 Code Block:

init.png

MicroPython Code Block:

from hardware import I2C
from unit import AccelUnit

acceli2c0 = I2C(0, scl=Pin(1), sda=Pin(2), freq=100000)
accel_0 = AccelUnit(i2c0)
disable_freefall_detection()

Disable freefall detection.

UiFlow2 Code Block:

disable_freefall_detection.png

MicroPython Code Block:

accel_0.disable_freefall_detection()
Return type:

None

disable_motion_detection()

Disable motion detection.

UiFlow2 Code Block:

disable_motion_detection.png

MicroPython Code Block:

accel_0.disable_motion_detection()
Return type:

None

disable_tap_detection()

Disable tap detection.

UiFlow2 Code Block:

disable_tap_detection.png

MicroPython Code Block:

accel_0.disable_tap_detection()
Return type:

None

enable_freefall_detection(*, threshold=10, time=25)

Freefall detection parameters:

Parameters:
  • threshold (int) – The value that acceleration on all axes must be under to register as dropped. The scale factor is 62.5 mg/LSB.

  • time (int) – The amount of time that acceleration on all axes must be less than threshold to register as dropped. The scale factor is 5 ms/LSB. Values between 100 ms and 350 ms (20 to 70) are recommended.

Return type:

None

If you wish to set them yourself rather than using the defaults, you must use keyword arguments:

accelerometer.enable_freefall_detection(time=30)

UiFlow2 Code Block:

enable_freefall_detection1.png

enable_freefall_detection2.png

MicroPython Code Block:

accel_0.enable_freefall_detection()
enable_motion_detection(*, threshold=18)

The activity detection parameters.

Parameters:

threshold (int) – The value that acceleration on any axis must exceed to register as active. The scale factor is 62.5 mg/LSB.

Return type:

None

If you wish to set them yourself rather than using the defaults, you must use keyword arguments:

accelerometer.enable_motion_detection(threshold=20)

UiFlow2 Code Block:

enable_motion_detection1.png

enable_motion_detection2.png

MicroPython Code Block:

accel_0.enable_motion_detection(threshold=18)
enable_tap_detection(*, threshold=20, duration=50, latency=20, window=255)

The tap detection parameters.

Parameters:
  • tap_count (int) – 1 to detect only single taps, and 2 to detect only double taps.

  • threshold (int) – A threshold for the tap detection. The scale factor is 62.5 mg/LSB The higher the value the less sensitive the detection.

  • duration (int) – This caps the duration of the impulse above threshold. Anything above duration won’t register as a tap. The scale factor is 625 µs/LSB.

  • latency (int) – (double tap only) The length of time after the initial impulse falls below threshold to start the window looking for a second impulse. The scale factor is 1.25 ms/LSB.

  • window (int) – (double tap only) The length of the window in which to look for a second tap. The scale factor is 1.25 ms/LSB.

Return type:

None

If you wish to set them yourself rather than using the defaults, you must use keyword arguments:

accelerometer.enable_tap_detection(duration=30, threshold=25)

UiFlow2 Code Block:

enable_tap_detection1.png

enable_tap_detection2.png

MicroPython Code Block:

accel_0.enable_tap_detection(tap_count=1, threshold=20, duration=50, latency=20, window=255)
get_accel()

The x, y, z acceleration values returned in a 3-tuple in \(m / s ^ 2\).

Returns:

x, y, z acceleration values in \(m / s ^ 2\).

Return type:

tuple[float, float, float]

UiFlow2 Code Block:

get_accel.png

MicroPython Code Block:

accel_0.get_accel()
get_data_rate()

Get the data rate of the sensor.

Returns:

The data rate of the sensor.

Return type:

int

UiFlow2 Code Block:

get_data_rate.png

MicroPython Code Block:

accel_0.get_data_rate()
get_range()

Get the measurement range of the sensor.

Returns:

The measurement range of the sensor.

Return type:

int

UiFlow2 Code Block:

get_range.png

MicroPython Code Block:

accel_0.get_range()
is_freefall()

Returns True if freefall has been detected.

Returns:

True if freefall has been detected.

Return type:

bool

UiFlow2 Code Block:

is_freefall.png

MicroPython Code Block:

accel_0.is_freefall()
is_motion()

Returns True if motion has been detected.

Returns:

True if motion has been detected.

Return type:

bool

UiFlow2 Code Block:

is_motion.png

MicroPython Code Block:

accel_0.is_motion()
is_tap()

Returns True if a tap has been detected.

Returns:

True if a tap has been detected.

Return type:

bool

UiFlow2 Code Block:

is_tap.png

MicroPython Code Block:

accel_0.is_tap()
set_data_rate(rate)

Set the data rate of the sensor.

Parameters:

rate (int) – The data rate of the sensor.

Return type:

None

UiFlow2 Code Block:

set_data_rate.png

MicroPython Code Block:

accel_0.set_data_rate(accel_0.RATE_3200_HZ)
set_range(range)

The measurement range of the sensor.

Parameters:

range (int) – The measurement range of the sensor.

Return type:

None

UiFlow2 Code Block:

set_range.png

MicroPython Code Block:

accel_0.set_range(accel_0.RANGE_2_G)

ADXL345

class unit.accel.ADXL345(i2c, address=micropython.const)

Bases: object

Driver for the ADXL345 3 axis accelerometer.

Parameters:
  • i2c (I2C) – The I2C bus the ADXL345 is connected to.

  • address (int) – The I2C device address for the sensor. Default is 0x53.

Quickstart: Importing and using the device

Here is an example of using the ADXL345 class. First you will need to import the libraries to use the sensor:

import machine
import adxl34x

Once this is done you can define your I2C object and define your sensor object:

i2c = machine.I2C(0)  # uses board default SDA and SCL pins
accelerometer = adxl34x.ADXL343(i2c)

Now you have access to the acceleration attribute:

acceleration = accelerometer.acceleration
property acceleration: tuple[int, int, int]

The x, y, z acceleration values returned in a 3-tuple in \(m / s ^ 2\).

property data_rate: int

The data rate of the sensor.

disable_freefall_detection()

Disable freefall detection.

Return type:

None

disable_motion_detection()

Disable motion detection.

Return type:

None

disable_tap_detection()

Disable tap detection.

Return type:

None

enable_freefall_detection(*, threshold=10, time=25)

Freefall detection parameters:

Parameters:
  • threshold (int) – The value that acceleration on all axes must be under to register as dropped. The scale factor is 62.5 mg/LSB.

  • time (int) – The amount of time that acceleration on all axes must be less than threshold to register as dropped. The scale factor is 5 ms/LSB. Values between 100 ms and 350 ms (20 to 70) are recommended.

Return type:

None

If you wish to set them yourself rather than using the defaults, you must use keyword arguments:

accelerometer.enable_freefall_detection(time=30)
enable_motion_detection(*, threshold=18)

The activity detection parameters.

Parameters:

threshold (int) – The value that acceleration on any axis must exceed to register as active. The scale factor is 62.5 mg/LSB.

If you wish to set them yourself rather than using the defaults, you must use keyword arguments:

accelerometer.enable_motion_detection(threshold=20)
enable_tap_detection(*, tap_count=1, threshold=20, duration=50, latency=20, window=255)

The tap detection parameters.

Parameters:
  • tap_count (int) – 1 to detect only single taps, and 2 to detect only double taps.

  • threshold (int) – A threshold for the tap detection. The scale factor is 62.5 mg/LSB The higher the value the less sensitive the detection.

  • duration (int) – This caps the duration of the impulse above threshold. Anything above duration won’t register as a tap. The scale factor is 625 µs/LSB.

  • latency (int) – (double tap only) The length of time after the initial impulse falls below threshold to start the window looking for a second impulse. The scale factor is 1.25 ms/LSB.

  • window (int) – (double tap only) The length of the window in which to look for a second tap. The scale factor is 1.25 ms/LSB.

If you wish to set them yourself rather than using the defaults, you must use keyword arguments:

accelerometer.enable_tap_detection(duration=30, threshold=25)
property events: dict[str, bool]

events will return a dictionary with a key for each event type that has been enabled.

The possible keys are:

Key

Description

tap

True if a tap was detected recently. Whether it’s looking for a single or double tap is determined by the tap param of enable_tap_detection.

motion

True if the sensor has seen acceleration above the threshold set with enable_motion_detection.

freefall

True if the sensor was in freefall. Parameters are set when enabled with enable_freefall_detection.

property offset: tuple[int, int, int]

The x, y, z offsets as a tuple of raw count values.

See offset_calibration example for usage.

property range: int

The measurement range of the sensor.

property raw_x: int

The raw x value.

property raw_y: int

The raw y value.

property raw_z: int

The raw z value.