Chain Encoder
EncoderChain is the helper class for encoder devices on the Chain bus. It provides methods to read encoder values and increments, reset the encoder, configure the clockwise rotation direction, and handle button events with RGB LED feedback.
Support the following products:
UiFlow2 Example
Encoder reading with brightness control
Open the m5core_chain_encoder_basic_example.m5f2 project in UiFlow2.
This example demonstrates how to read encoder values and increments, handle button click events, and control RGB LED brightness based on encoder rotation. The encoder value is displayed on screen and updated in real-time.
UiFlow2 Code Block:
Example output:
None
MicroPython Example
Encoder reading with brightness control
This example demonstrates how to read encoder values and increments, handle button click events, and control RGB LED brightness based on encoder rotation. The encoder value is displayed on screen and updated in real-time.
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 EncoderChain 9from chain import ChainBus 10import time 11 12 13title0 = None 14label_brightness = None 15label_val = None 16label_count = None 17bus2 = None 18chain_encoder_0 = None 19count = None 20last_time = None 21value = None 22brightness = None 23 24 25def chain_encoder_0_click_event(args): 26 global \ 27 title0, \ 28 label_brightness, \ 29 label_val, \ 30 label_count, \ 31 bus2, \ 32 chain_encoder_0, \ 33 count, \ 34 last_time, \ 35 value, \ 36 brightness 37 count = (count if isinstance(count, (int, float)) else 0) + 1 38 label_count.setText(str((str("Button Count: ") + str(count)))) 39 40 41def setup(): 42 global \ 43 title0, \ 44 label_brightness, \ 45 label_val, \ 46 label_count, \ 47 bus2, \ 48 chain_encoder_0, \ 49 count, \ 50 last_time, \ 51 value, \ 52 brightness 53 M5.begin() 54 Widgets.setRotation(0) 55 Widgets.fillScreen(0x222222) 56 title0 = Widgets.Title("Title", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu24) 57 label_brightness = Widgets.Label( 58 "Brightness:", 5, 100, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu24 59 ) 60 label_val = Widgets.Label("Value:", 5, 60, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu24) 61 label_count = Widgets.Label( 62 "Button Count:", 5, 140, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu24 63 ) 64 bus2 = ChainBus(2, tx=21, rx=22) 65 chain_encoder_0 = EncoderChain(bus2, 1) 66 chain_encoder_0.set_click_callback(chain_encoder_0_click_event) 67 chain_encoder_0.set_button_mode(chain_encoder_0.MODE_EVENT) 68 chain_encoder_0.set_rgb_color(0x00FF00) 69 chain_encoder_0.set_rgb_brightness(80, save=False) 70 chain_encoder_0.set_cw_increase(True, save=False) 71 print(chain_encoder_0.get_encoder_increment()) 72 brightness = 0 73 74 75def loop(): 76 global \ 77 title0, \ 78 label_brightness, \ 79 label_val, \ 80 label_count, \ 81 bus2, \ 82 chain_encoder_0, \ 83 count, \ 84 last_time, \ 85 value, \ 86 brightness 87 M5.update() 88 if (time.ticks_diff((time.ticks_ms()), last_time)) >= 100: 89 last_time = time.ticks_ms() 90 value = chain_encoder_0.get_encoder_value() 91 label_val.setText(str((str("Value: ") + str(value)))) 92 brightness = min(max(brightness + (chain_encoder_0.get_encoder_increment()), 0), 100) 93 label_brightness.setText(str((str("Brightness: ") + str(brightness)))) 94 chain_encoder_0.set_rgb_brightness(brightness, save=False) 95 96 97if __name__ == "__main__": 98 try: 99 setup() 100 while True: 101 loop() 102 except (Exception, KeyboardInterrupt) as e: 103 try: 104 bus2.deinit() 105 from utility import print_error_msg 106 107 print_error_msg(e) 108 except ImportError: 109 print("please update to latest firmware")
Example output:
None
API
EncoderChain
- class chain.encoder.EncoderChain(bus, device_id)
Bases:
KeyChainEncoder Chain class for interacting with encoder devices over Chain bus.
- Parameters:
UiFlow2 Code Block:

MicroPython Code Block:
from chain import ChainBus from chain import EncoderChain chainbus_0 = ChainBus(2, 32, 33, verbose=True) encoder_0 = EncoderChain(chainbus_0, 1)
For other button and some general methods, please refer to the
ChainKeyclass.- get_encoder_value()
Get the encoder value.
- Returns:
Encoder value as int16_t (-32768 to 32767), or None if failed.
- Return type:
UiFlow2 Code Block:

MicroPython Code Block:
value = encoder_0.get_encoder_value()
- get_encoder_increment()
Get the encoder increment value.
- Returns:
Encoder increment value as int16_t (-32768 to 32767), or None if failed.
- Return type:
UiFlow2 Code Block:

MicroPython Code Block:
increment = encoder_0.get_encoder_increment()
- reset_encoder_value()
Reset the encoder value.
- Returns:
True if the operation was successful, False otherwise.
- Return type:
UiFlow2 Code Block:

MicroPython Code Block:
success = encoder_0.reset_encoder_value()
- reset_encoder_increment()
Reset the encoder increment value.
- Returns:
True if the operation was successful, False otherwise.
- Return type:
UiFlow2 Code Block:

MicroPython Code Block:
success = encoder_0.reset_increment()
- set_cw_increase(clockwise_increase=False, save=False)
Set whether clockwise rotation increases the encoder value.
- Parameters:
- Returns:
True if the setting was set successfully, False otherwise.
- Return type:
UiFlow2 Code Block:

MicroPython Code Block:
success = encoder_0.set_cw_increase(True, True)
- get_cw_increase()
Get whether clockwise rotation increases the encoder value.
- Returns:
Whether clockwise rotation increases. True means clockwise increases, False means clockwise decreases. Returns False if failed.
- Return type:
UiFlow2 Code Block:

MicroPython Code Block:
increase = encoder_0.get_cw_increase()

