Atomic PWM Base

Support the following products:

Atomic PWM Base

UiFlow2 Example:

PWM output control

Open the atoms3r_pwm_base_example.m5f2 project in UiFlow2.

The example demonstrates controlling the PWM signal’s duty cycle to fluctuate between low to high and high to low.

UiFlow2 Code Block:

atoms3r_pwm_base_example.png

Example output:

None

MicroPython Example:

PWM output control

The example demonstrates controlling the PWM signal’s duty cycle to fluctuate between low to high and high to low.

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 AtomicPWMBase
 9import time
10
11
12title0 = None
13label0 = None
14label1 = None
15label_freq = None
16label_duty = None
17base_pwm = None
18i = None
19
20
21def setup():
22    global title0, label0, label1, label_freq, label_duty, base_pwm, i
23    M5.begin()
24    title0 = Widgets.Title("PWM Control", 0, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu18)
25    label0 = Widgets.Label("freq:", 1, 35, 1.0, 0xFFFFFF, 0x000000, Widgets.FONTS.DejaVu18)
26    label1 = Widgets.Label("duty:", 2, 65, 1.0, 0xFFFFFF, 0x000000, Widgets.FONTS.DejaVu18)
27    label_freq = Widgets.Label("1000Hz", 47, 35, 1.0, 0xFFFFFF, 0x000000, Widgets.FONTS.DejaVu18)
28    label_duty = Widgets.Label("0", 55, 65, 1.0, 0xFFFFFF, 0x000000, Widgets.FONTS.DejaVu18)
29    base_pwm = AtomicPWMBase(5, 1000)
30    label_freq.setText(str((str((base_pwm.get_freq())) + str("Hz"))))
31
32
33def loop():
34    global title0, label0, label1, label_freq, label_duty, base_pwm, i
35    M5.update()
36    for i in range(100):
37        base_pwm.set_duty_u16(i * 150)
38        label_duty.setText(str(base_pwm.get_duty_u16()))
39        time.sleep_ms(40)
40    for i in range(100):
41        base_pwm.set_duty_u16(15000 - i * 150)
42        label_duty.setText(str(base_pwm.get_duty_u16()))
43        time.sleep_ms(40)
44
45
46if __name__ == "__main__":
47    try:
48        setup()
49        while True:
50            loop()
51    except (Exception, KeyboardInterrupt) as e:
52        try:
53            from utility import print_error_msg
54
55            print_error_msg(e)
56        except ImportError:
57            print("please update to latest firmware")

Example output:

None

API

PWM

class base.pwm.AtomicPWMBase(out_pin=5, freq=1000)

Bases: object

Create an AtomicPWMBase object.

Parameters:
  • out_pin (int) – The PWM output pin. Default is 5.

  • freq (int) – The PWM frequency. Default is 1000.

UiFlow2 Code Block:

init.png

MicroPython Code Block:

from base import AtomicPWMBase

base_pwm = AtomicPWMBase(out_pin=5, freq=1000)
set_freq(freq=1000)

Set PWM frequency.

Parameters:

freq (int) – The PWM frequency. Default is 1000.

Return type:

None

UiFlow2 Code Block:

set_freq.png

MicroPython Code Block:

base_pwm.set_freq()
get_freq()

Get PWM frequency.

Returns:

PWM frequency.

Return type:

int

UiFlow2 Code Block:

get_freq.png

MicroPython Code Block:

base_pwm.get_freq()
set_duty_u16(duty=0)

Set PWM duty cycle.

set the current duty cycle of the PWM output, as an unsigned 16-bit value in the range 0 to 65535 inclusive.

Parameters:

duty (int) – The PWM duty cycle. Range: 0 ~ 65535. Default is 0.

UiFlow2 Code Block:

set_duty_u16.png

MicroPython Code Block:

base_pwm.set_duty_u16()
get_duty_u16()

Get PWM duty cycle.

Returns:

PWM duty cycle. Range: 0~65535.

Return type:

int

UiFlow2 Code Block:

get_duty_u16.png

MicroPython Code Block:

base_pwm.get_duty_u16()