Chain Angle

AngleChain is the helper class for angle sensor devices on the Chain bus. It provides methods to read angle values in different resolutions (12-bit and 8-bit ADC) and configure the clockwise rotation direction.

Support the following products:

Chain Angle

UiFlow2 Example

Angle reading with RGB brightness control

Open the m5core_chain_angle_basic_example.m5f2 project in UiFlow2.

This example demonstrates how to read angle values from the Chain Angle sensor and control RGB brightness based on the angle value. The example reads 8-bit ADC values (0-255) and maps them to brightness values (0-100) for visual feedback.

UiFlow2 Code Block:

m5core_chain_angle_basic_example.png

Example output:

None

MicroPython Example

Angle reading with RGB brightness control

This example demonstrates how to read angle values from the Chain Angle sensor and control RGB brightness based on the angle value. The example reads 8-bit ADC values (0-255) and maps them to brightness values (0-100) for visual feedback.

MicroPython Code Block:

 1# SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
 2#
 3# SPDX-License-Identifier: MIT
 4
 5import os, sys, io
 6import M5
 7from M5 import *
 8from chain import ChainBus
 9from chain import AngleChain
10import time
11import m5utils
12
13
14title0 = None
15label_brightness = None
16label_val = None
17bus2 = None
18chain_angle_0 = None
19last_time = None
20value = None
21brightness = None
22
23
24def setup():
25    global title0, label_brightness, label_val, bus2, chain_angle_0, last_time, value, brightness
26    M5.begin()
27    Widgets.setRotation(1)
28    Widgets.fillScreen(0x222222)
29    title0 = Widgets.Title("Chain Angle Example", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu24)
30    label_brightness = Widgets.Label(
31        "Brightness: ", 5, 117, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu24
32    )
33    label_val = Widgets.Label("Value:", 5, 69, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu24)
34    bus2 = ChainBus(2, tx=21, rx=22)
35    chain_angle_0 = AngleChain(bus2, 1)
36    chain_angle_0.set_rgb_color(0x00CCCC)
37
38
39def loop():
40    global title0, label_brightness, label_val, bus2, chain_angle_0, last_time, value, brightness
41    M5.update()
42    if (time.ticks_diff((time.ticks_ms()), last_time)) >= 100:
43        last_time = time.ticks_ms()
44        value = chain_angle_0.get_adc8()
45        brightness = int(m5utils.remap(value, 0, 255, 0, 100))
46        label_val.setText(str((str("Value: ") + str(value))))
47        label_brightness.setText(str((str("Brightness: ") + str(brightness))))
48        chain_angle_0.set_rgb_brightness(brightness, save=False)
49
50
51if __name__ == "__main__":
52    try:
53        setup()
54        while True:
55            loop()
56    except (Exception, KeyboardInterrupt) as e:
57        try:
58            bus2.deinit()
59            from utility import print_error_msg
60
61            print_error_msg(e)
62        except ImportError:
63            print("please update to latest firmware")

Example output:

None

API

AngleChain

class chain.angle.AngleChain(bus, device_id)

Bases: KeyChain

Angle Chain class for interacting with angle devices over Chain bus.

Parameters:
  • bus (ChainBus) – The Chain bus instance.

  • device_id (int) – The device ID of the angle sensor on the Chain bus.

UiFlow2 Code Block:

init.png

MicroPython Code Block:

from chain import ChainBus
from chain import AngleChain

chainbus_0 = ChainBus(2, 32, 33, verbose=True)
angle_0 = AngleChain(chainbus_0, 1)

For other button and some general methods, please refer to the ChainKey class.

get_adc12()

Get the angle value in 12-bit ADC resolution.

Returns:

Angle value in 12-bit ADC (0-4095), or None if failed.

Return type:

int

UiFlow2 Code Block:

get_adc12.png

MicroPython Code Block:

angle = angle_0.get_adc12()
get_adc8()

Get the angle value in 8-bit ADC resolution.

Returns:

Angle value in 8-bit ADC (0-255), or None if failed.

Return type:

int

UiFlow2 Code Block:

get_adc8.png

MicroPython Code Block:

angle = angle_0.get_adc8()
set_cw_increase(increase=True, save=False)

Set whether clockwise rotation increases the angle value.

Parameters:
  • increase (bool) – Whether clockwise rotation increases. False means clockwise decreases, True means clockwise increases.

  • save (bool) – Whether to save to flash. False means don’t save, True means save.

Returns:

True if the setting was set successfully, False otherwise.

Return type:

bool

UiFlow2 Code Block:

set_cw_increase.png

MicroPython Code Block:

success = angle_0.set_cw_increase(True, False)
get_cw_increase()

Get whether clockwise rotation increases the angle value.

Returns:

Whether clockwise rotation increases. False means clockwise decreases, True means clockwise increases. Returns False if failed.

Return type:

bool

UiFlow2 Code Block:

get_cw_increase.png

MicroPython Code Block:

increase = angle_0.get_cw_increase()