QRCode Module
This library is the driver for Module13.2 QRCode, and the module communicates via UART.
Support the following products:
UiFlow2 Example:
QRCode Scan in Continuous Mode
Open the cores3_qrcode_continuous_mode_example.m5f2 project in UiFlow2.
In Continuous Mode, pressing the button once starts decoding, and pressing the button again stops decoding.
UiFlow2 Code Block:
Example output:
None
QRCode Scan in Auto Mode
Open the cores3_qrcode_auto_mode_example.m5f2 project in UiFlow2.
In Auto Mode, the module starts decoding when powered on and cannot be stopped.
UiFlow2 Code Block:
Example output:
None
QRCode Scan in Pulse Mode
Open the cores3_qrcode_pulse_mode_example.m5f2 project in UiFlow2.
In Pulse Mode, set the TRIG pin to hold a low level for more than 20ms to trigger decoding once.
UiFlow2 Code Block:
Example output:
None
QRCode Scan in Motion Sensing Mode
Open the cores3_qrcode_motion_sensing_mode_example.m5f2 project in UiFlow2.
In Motion Sensing Mode, the module automatically triggers decoding when it detects a change in the scene based on visual recognition information.
UiFlow2 Code Block:
Example output:
None
MicroPython Example:
QRCode Scan in Continuous Mode
In Continuous Mode, pressing the button once starts decoding, and pressing the button again stops decoding.
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 module import QRCodeModule 9 10 11title0 = None 12label_status = None 13label_data = None 14module_qrcode_0 = None 15is_scanning = None 16data = None 17 18 19def btn_pwr_was_clicked_event(state): 20 global title0, label_status, label_data, module_qrcode_0, is_scanning, data 21 if is_scanning: 22 module_qrcode_0.stop_decode() 23 label_status.setColor(0xFFFFFF, 0x222222) 24 label_status.setText(str("stop scan")) 25 else: 26 module_qrcode_0.start_decode() 27 label_status.setColor(0x00FF00, 0x222222) 28 label_status.setText(str("scanning")) 29 is_scanning = not is_scanning 30 31 32def setup(): 33 global title0, label_status, label_data, module_qrcode_0, is_scanning, data 34 M5.begin() 35 Widgets.fillScreen(0x222222) 36 title0 = Widgets.Title("QRCode", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu24) 37 label_status = Widgets.Label( 38 "stop scan", 5, 50, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu24 39 ) 40 label_data = Widgets.Label("data", 4, 100, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu24) 41 BtnPWR.setCallback(type=BtnPWR.CB_TYPE.WAS_CLICKED, cb=btn_pwr_was_clicked_event) 42 module_qrcode_0 = QRCodeModule(1, tx=17, rx=18) 43 module_qrcode_0.set_trigger_mode(QRCodeModule.TRIGGER_MODE_CONTINUOUS) 44 module_qrcode_0.stop_decode() 45 is_scanning = False 46 47 48def loop(): 49 global title0, label_status, label_data, module_qrcode_0, is_scanning, data 50 M5.update() 51 data = module_qrcode_0.read() 52 if data: 53 label_data.setText(str(data.decode())) 54 print(data.decode()) 55 56 57if __name__ == "__main__": 58 try: 59 setup() 60 while True: 61 loop() 62 except (Exception, KeyboardInterrupt) as e: 63 try: 64 from utility import print_error_msg 65 66 print_error_msg(e) 67 except ImportError: 68 print("please update to latest firmware")
Example output:
None
QRCode Scan in Auto Mode
In Auto Mode, the module starts decoding when powered on and cannot be stopped.
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 module import QRCodeModule 9 10 11title0 = None 12label_status = None 13label_data = None 14module_qrcode_0 = None 15data = None 16 17 18def setup(): 19 global title0, label_status, label_data, module_qrcode_0, data 20 M5.begin() 21 Widgets.fillScreen(0x222222) 22 title0 = Widgets.Title("QRCode", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu24) 23 label_status = Widgets.Label( 24 "scanning", 5, 50, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu24 25 ) 26 label_data = Widgets.Label("data", 5, 100, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu24) 27 module_qrcode_0 = QRCodeModule(2, tx=17, rx=18) 28 module_qrcode_0.set_trigger_mode(QRCodeModule.TRIGGER_MODE_AUTO) 29 30 31def loop(): 32 global title0, label_status, label_data, module_qrcode_0, data 33 M5.update() 34 data = module_qrcode_0.read() 35 if data: 36 label_data.setText(str(data.decode())) 37 38 39if __name__ == "__main__": 40 try: 41 setup() 42 while True: 43 loop() 44 except (Exception, KeyboardInterrupt) as e: 45 try: 46 from utility import print_error_msg 47 48 print_error_msg(e) 49 except ImportError: 50 print("please update to latest firmware")
Example output:
None
QRCode Scan in Pulse Mode
In Pulse Mode, set the TRIG pin to hold a low level for more than 20ms to trigger decoding once.
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 module import QRCodeModule 9import time 10 11 12title0 = None 13label_status = None 14label_data = None 15module_qrcode_0 = None 16data = None 17 18 19def btn_pwr_was_clicked_event(state): 20 global title0, label_status, label_data, module_qrcode_0, data 21 module_qrcode_0.set_trig(0) 22 time.sleep_ms(20) 23 module_qrcode_0.set_trig(1) 24 label_status.setColor(0x00FF00, 0x222222) 25 label_status.setText(str("scanning")) 26 27 28def setup(): 29 global title0, label_status, label_data, module_qrcode_0, data 30 M5.begin() 31 Widgets.fillScreen(0x222222) 32 title0 = Widgets.Title("QRCode", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu24) 33 label_status = Widgets.Label( 34 "scanning", 5, 50, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu24 35 ) 36 label_data = Widgets.Label("data", 5, 100, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu24) 37 BtnPWR.setCallback(type=BtnPWR.CB_TYPE.WAS_CLICKED, cb=btn_pwr_was_clicked_event) 38 module_qrcode_0 = QRCodeModule(2, tx=17, rx=18) 39 module_qrcode_0.set_trigger_mode(QRCodeModule.TRIGGER_MODE_PULSE) 40 41 42def loop(): 43 global title0, label_status, label_data, module_qrcode_0, data 44 M5.update() 45 data = module_qrcode_0.read() 46 if data: 47 label_data.setText(str(data.decode())) 48 label_status.setColor(0xFFFFFF, 0x222222) 49 label_status.setText(str("stop scan")) 50 51 52if __name__ == "__main__": 53 try: 54 setup() 55 while True: 56 loop() 57 except (Exception, KeyboardInterrupt) as e: 58 try: 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
QRCode Scan in Motion Sensing Mode
In Motion Sensing Mode, the module automatically triggers decoding when it detects a change in the scene based on visual recognition information.
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 module import QRCodeModule 9 10 11title0 = None 12label_status = None 13label_data = None 14module_qrcode_0 = None 15data = None 16 17 18def setup(): 19 global title0, label_status, label_data, module_qrcode_0, data 20 M5.begin() 21 Widgets.fillScreen(0x222222) 22 title0 = Widgets.Title("QRCode", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu24) 23 label_status = Widgets.Label( 24 "detecting", 5, 50, 1.0, 0x00FF00, 0x222222, Widgets.FONTS.DejaVu24 25 ) 26 label_data = Widgets.Label("data", 5, 100, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu24) 27 module_qrcode_0 = QRCodeModule(2, tx=17, rx=18) 28 module_qrcode_0.set_trigger_mode(QRCodeModule.TRIGGER_MODE_MOTION_SENSING) 29 module_qrcode_0.set_motion_sensitivity(1) 30 31 32def loop(): 33 global title0, label_status, label_data, module_qrcode_0, data 34 M5.update() 35 data = module_qrcode_0.read() 36 if data: 37 label_data.setText(str(data.decode())) 38 39 40if __name__ == "__main__": 41 try: 42 setup() 43 while True: 44 loop() 45 except (Exception, KeyboardInterrupt) as e: 46 try: 47 from utility import print_error_msg 48 49 print_error_msg(e) 50 except ImportError: 51 print("please update to latest firmware")
Example output:
None
API
QRCodeModule
- class module.qrcode.QRCodeModule
Create an QRCodeModule object.
UiFlow2 Code Block:
MicroPython Code Block:
from module import ModuleQRCode module_qrcode = ModuleQRCode(id = 1, tx = 17, rx = 18)
- set_power(enable)
Set power.
- Parameters:
enable (bool) –
True
: power on.False
: power off.
UiFlow2 Code Block:
MicroPython Code Block:
module_qrcode.set_power(enable)
- set_trig(value)
Set trigger pin value.
- Parameters:
value (int) –
0
: low level.1
: high level.
UiFlow2 Code Block:
MicroPython Code Block:
module_qrcode.set_trig(value)
- start_decode()
Start decode.
UiFlow2 Code Block:
MicroPython Code Block:
module_qrcode.start_decode()
- stop_decode()
Stop decode.
UiFlow2 Code Block:
MicroPython Code Block:
module_qrcode.stop_decode()
- read()
Read decode data.
- Returns:
qrcode data.
- Return type:
None | bytes
If no data is received, return None.
UiFlow2 Code Block:
MicroPython Code Block:
module_qrcode.read()
- set_trigger_mode(mode)
Set trigger mode.
- Parameters:
mode (int) –
The trigger mode. Available options:
TRIGGER_MODE_KEY
: Key Mode, Triggers a single decode; decoding stops after a successful read.TRIGGER_MODE_CONTINUOUS
: Continuous Mode, Triggers continuous decoding; decoding continues even after a successful read and stops only when triggered again.TRIGGER_MODE_AUTO
: Auto Mode, Performs continuous decoding upon power-up and cannot be stopped.TRIGGER_MODE_PULSE
: Pulse Mode, The Trig pin’s low-level signal triggers decoding, which stops after a successful read or when the single read time limit is reached.TRIGGER_MODE_MOTION_SENSING
: Motion Sensing Mode, Uses image recognition; decoding starts when a scene change is detected.
UiFlow2 Code Block:
MicroPython Code Block:
module_qrcode.set_trigger_mode(mode)
- set_decode_delay(delay_ms)
Set decode delay.
- Parameters:
delay_ms (int) – decode delay time(ms), 0 means continuous decoding until success.
UiFlow2 Code Block:
MicroPython Code Block:
module_qrcode.set_decode_delay(delay_ms)
- set_trigger_timeout(timeout_ms)
Set trigger timeout.
- Parameters:
timeout_ms – trigger timeout time(ms), Decoding will automatically stop when the duration exceeds this value.
UiFlow2 Code Block:
MicroPython Code Block:
module_qrcode.set_trigger_timeout(timeout_ms)
- set_motion_sensitivity(level)
Set motion detection sensitivity. (in Motion Sensing Mode)
- Parameters:
level (int) – sensitivity level. Range: 1~5. The higher the level, the more sensitive it is to scene changes.
UiFlow2 Code Block:
MicroPython Code Block:
module_qrcode.set_motion_sensitivity(level)
- set_continuous_decode_delay(delay_100ms)
Set continuous decode delay. (in Motion Sensing Mode)
- Parameters:
delay_ms (int) – delay time(unit: 100ms), 0 means continuous decoding until timeout.
UiFlow2 Code Block:
MicroPython Code Block:
module_qrcode.set_continuous_decode_delay(delay_ms)
- set_trigger_decode_delay(delay_ms):
Set trigger decode delay. (in Motion Sensing Mode)
Sets the trigger decoding delay time. This is the delay between re-entering the scene change detection phase and starting recognition again after detecting a change.
- Parameters:
delay_ms (int) – Trigger decode delay time(unit: ms).
UiFlow2 Code Block:
MicroPython Code Block:
module_qrcode.set_trigger_decode_delay(delay_ms)
- set_same_code_interval(interval_ms)
Set same code interval.
- Parameters:
interval_ms (int) – The interval time for repeated recognition of the same code (unit: ms).
UiFlow2 Code Block:
MicroPython Code Block:
module_qrcode.set_same_code_interval(interval_ms)
- set_diff_code_interval(interval_ms)
Set difference code interval.
- Parameters:
interval_ms (int) – The interval time for repeated recognition of the difference code (unit: ms).
UiFlow2 Code Block:
MicroPython Code Block:
module_qrcode.set_diff_code_interval(interval_ms)
- set_same_code_no_delay(enable)
Set same code no delay.
- Parameters:
enable (bool) – Whether to enable non-delay output for the same code. True means enabled, False means disabled.
UiFlow2 Code Block:
MicroPython Code Block:
module_qrcode.set_same_code_no_delay(enable)
- set_fill_light_mode(mode)
Set fill light mode.
- Parameters:
mode (int) –
The fill light mode. Available options:
FILL_LIGHT_OFF
: Light off.FILL_LIGHT_ON
: Light on.FILL_LIGHT_ON_DECODE
: Light on during decoding.
UiFlow2 Code Block:
MicroPython Code Block:
module_qrcode.set_fill_light_mode(mode)
- set_fill_light_brightness(brightness)
Set fill light brightness.
- Parameters:
brightness (int) – The fill light brightness. Range: 0~100.
UiFlow2 Code Block:
MicroPython Code Block:
module_qrcode.set_fill_light_brightness(brightness)
- set_pos_light_mode(mode)
Set positioning light mode.
- Parameters:
mode (int) –
The positioning light mode. Available options:
POS_LIGHT_OFF
: Light off.POS_LIGHT_ON_DECODE
: Light on during decoding.POS_LIGHT_FLASH_ON_DECODE
: Light flash during decoding.
UiFlow2 Code Block:
MicroPython Code Block:
module_qrcode.set_pos_light_mode(mode)
- set_startup_tone(mode)
Set startup tone.
- Parameters:
mode (int) –
0
: Disable startup tone.1
: Play 4 beeps.2
: Play 2 beeps.
UiFlow2 Code Block:
MicroPython Code Block:
module_qrcode.set_startup_tone(mode)
- set_decode_success_beep(count)
Set decode success beep.
- Parameters:
count (int) –
0
: No prompt sound.1
: Play prompt sound once.2
: Play prompt sound twice.
UiFlow2 Code Block:
MicroPython Code Block:
module_qrcode.set_decode_success_beep(count)
- set_case_conversion(mode)
Set case conversion.
- Parameters:
mode (int) –
0
: Off (Original data).1
: Convert to uppercase.2
: Convert to lowercase.
UiFlow2 Code Block:
MicroPython Code Block:
module_qrcode.set_case_conversion(mode)
- set_protocol_format(mode):
- set_protocol_format(mode)
- Parameters:
mode (int) –
0
: No protocol1
: Format 1: [0x03] + Data Length (2 bytes) + Data2
: Format 2: [0x03] + Data Length + Number of Barcodes + Code 1 Data Length + Code 1 Data + … + CRC3
: Format 3: [0x03] + Data Length + Number of Barcodes + Code 1 ID + Code 1 Data Length + Code 1 Data + … + CRC
CRC generate reference program.
def crc16_calc(data: bytes) -> int: ca_crc = 0 for byte in data: for i in range(7, -1, -1): if ca_crc & 0x8000: ca_crc = (ca_crc << 1) ^ 0x18005 else: ca_crc <<= 1 if (byte & (1 << i)) != 0: ca_crc ^= 0x18005 return ca_crc & 0xFFFF
UiFlow2 Code Block:
MicroPython Code Block:
module_qrcode.set_protocol_format(mode)