Atomic Stepmotor Base

Support the following products:

Atomic Stepmotor Base

UiFlow2 Example:

Direction control

Open the atoms3r_stepmotor_direction_control_example.m5f2 project in UiFlow2.

The example demonstrates motor direction control. Pressing the screen button toggles the rotation direction.

UiFlow2 Code Block:

atoms3r_stepmotor_direction_control_example.png

Example output:

None

Rotate control

Open the atoms3r_stepmotor_rotate_control_example.m5f2 project in UiFlow2.

The example demonstrates the motor continuously rotating for multiple turns, then reversing for multiple turns, and repeating the cycle after a 2-second pause.

UiFlow2 Code Block:

atoms3r_stepmotor_rotate_control_example.png

Example output:

None

MicroPython Example:

Direction control

The example demonstrates motor direction control. Pressing the screen button toggles the rotation direction.

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 base import AtomicStepmotorBase
 9import time
10
11
12title0 = None
13label0 = None
14label_vol = None
15base_stepmotor = None
16direction = None
17
18
19def btn_was_clicked_event(state):
20    global title0, label0, label_vol, base_stepmotor, direction
21    direction = not direction
22    base_stepmotor.set_direction(direction)
23
24
25def setup():
26    global title0, label0, label_vol, base_stepmotor, direction
27    M5.begin()
28    title0 = Widgets.Title("Steps Ctrl", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu18)
29    label0 = Widgets.Label("vol:", 5, 35, 1.0, 0xFFFFFF, 0x000000, Widgets.FONTS.DejaVu18)
30    label_vol = Widgets.Label("12.0V", 43, 35, 1.0, 0xFFFFFF, 0x000000, Widgets.FONTS.DejaVu18)
31    BtnA.setCallback(type=BtnA.CB_TYPE.WAS_CLICKED, cb=btn_was_clicked_event)
32    base_stepmotor = AtomicStepmotorBase(5, 7, 6, 38, 39)
33    label_vol.setText(str((str((base_stepmotor.get_voltage())) + str("V"))))
34    direction = True
35
36
37def loop():
38    global title0, label0, label_vol, base_stepmotor, direction
39    M5.update()
40    base_stepmotor.step()
41    time.sleep_ms(1)
42
43
44if __name__ == "__main__":
45    try:
46        setup()
47        while True:
48            loop()
49    except (Exception, KeyboardInterrupt) as e:
50        try:
51            from utility import print_error_msg
52
53            print_error_msg(e)
54        except ImportError:
55            print("please update to latest firmware")

Example output:

None

Rotate control

The example demonstrates the motor continuously rotating for multiple turns, then reversing for multiple turns, and repeating the cycle after a 2-second pause.

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 base import AtomicStepmotorBase
 9import time
10
11
12title0 = None
13label0 = None
14label_vol = None
15base_stepmotor = None
16step_per_rev = None
17microstep = None
18total_steps = None
19rotate_circle = None
20
21
22def setup():
23    global \
24        title0, \
25        label0, \
26        label_vol, \
27        base_stepmotor, \
28        step_per_rev, \
29        microstep, \
30        total_steps, \
31        rotate_circle
32
33    M5.begin()
34    title0 = Widgets.Title("Steps Ctrl", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu18)
35    label0 = Widgets.Label("vol:", 5, 35, 1.0, 0xFFFFFF, 0x000000, Widgets.FONTS.DejaVu18)
36    label_vol = Widgets.Label("12.0V", 43, 35, 1.0, 0xFFFFFF, 0x000000, Widgets.FONTS.DejaVu18)
37    base_stepmotor = AtomicStepmotorBase(5, 7, 6, 38, 39)
38    step_per_rev = 200
39    microstep = 1 / 2
40    rotate_circle = 5
41    total_steps = (step_per_rev / microstep) * rotate_circle
42    label_vol.setText(str((str((base_stepmotor.get_voltage())) + str("V"))))
43
44
45def loop():
46    global \
47        title0, \
48        label0, \
49        label_vol, \
50        base_stepmotor, \
51        step_per_rev, \
52        microstep, \
53        total_steps, \
54        rotate_circle
55    M5.update()
56    print(base_stepmotor.get_voltage())
57    base_stepmotor.rotate(total_steps, 1, True)
58    time.sleep_ms(100)
59    base_stepmotor.rotate(total_steps, 1, False)
60    time.sleep_ms(100)
61    label_vol.setText(str((str((base_stepmotor.get_voltage())) + str("V"))))
62    time.sleep_ms(2000)
63
64
65if __name__ == "__main__":
66    try:
67        setup()
68        while True:
69            loop()
70    except (Exception, KeyboardInterrupt) as e:
71        try:
72            from utility import print_error_msg
73
74            print_error_msg(e)
75        except ImportError:
76            print("please update to latest firmware")

Example output:

None

API

AtomicStepmotorBase

class base.stepmotor.AtomicStepmotorBase(en=5, dir=7, stp=6, flt=38, rst=39)

Bases: object

Create an AtomicStepmotorBase object.

Parameters:
  • en (int) – Enable pin, used to enable or disable the stepper motor.

  • dir (int) – Direction pin, used to control the rotation direction of the motor.

  • stp (int) – Step pin, used for step control of the motor.

  • flt (int) – Fault pin, used to monitor the motor’s fault status.

  • rst (int) – Reset pin, used to reset the motor driver.

UiFlow2 Code Block:

init.png

MicroPython Code Block:

from base import AtomicStepmotorBase

base_stepmotor = AtomicStepmotorBase(en=5, dir=7, stp=6, flt=38, rst=39)
disable()

Disable the stepper motor driver.

UiFlow2 Code Block:

disable.png

MicroPython Code Block:

base_stepmotor.disable()
enable()

Enable the stepper motor driver.

UiFlow2 Code Block:

enable.png

MicroPython Code Block:

base_stepmotor.enable()
Return type:

None

get_status()

Get motor driver status.

Returns:

Returns True if the driver is operating normally, or False if a fault is detected.

Return type:

bool

UiFlow2 Code Block:

get_status.png

MicroPython Code Block:

base_stepmotor.get_status()
get_voltage()

Get voltage.

Returns:

The driver input voltage. unit: V

Return type:

float

UiFlow2 Code Block:

get_voltage.png

MicroPython Code Block:

base_stepmotor.get_voltage()
reset()

Reset the stepper motor driver.

UiFlow2 Code Block:

reset.png

MicroPython Code Block:

base_stepmotor.reset()
Return type:

None

rotate(steps, delay_ms=0, direction=True)

Rotate the stepper motor for a specified number of steps.

Parameters:
  • steps (int) – Number of steps to rotate.

  • delay_ms (int) – Delay between steps (in milliseconds), default is 0ms.

  • direction (bool) – Rotation direction (True or False).

Return type:

None

The actual rotation direction (clockwise or counterclockwise) depends on the motor wiring.

UiFlow2 Code Block:

rotate.png

MicroPython Code Block:

base_stepmotor.rotate(steps, delay_ms, direction)
set_direction(direction=True)

Set direction.

Parameters:

direction (bool) – Rotation direction. True or False.

Return type:

None

UiFlow2 Code Block:

set_direction.png

MicroPython Code Block:

base_stepmotor.set_direction(direction)
step()

Move the stepper motor one step.

UiFlow2 Code Block:

step.png

MicroPython Code Block:

base_stepmotor.step()
Return type:

None

stop()

Stop motor.

UiFlow2 Code Block:

stop.png

MicroPython Code Block:

base_stepmotor.stop()
Return type:

None