Wiznet makers

ruilixin6

Published August 05, 2026 ©

94 UCC

0 VAR

0 Contests

0 Followers

0 Following

Raspberry Pi Pico MicroPython Basic Example: PWM Controlled LED Breathing Lamp

MicroPython PWM‑driven LED breathing light, with periodic timer callback and explanation of Python `global` keyword.

COMPONENTS
PROJECT DESCRIPTION

【Preliminary Note】The original hardware example in this article was written based on the RP2040. The actual hardware used in this hands-on demonstration features the W55RP20 as the main controller chip. The circuit logic and UF2 flashing operation principles are universally applicable, with only the main controller model differing. The original chip model mentioned in the circuit descriptions below is provided for reference purposes only.

PWM Signal Controlled LED Breathing Light

All the code for the experiments below is open-source and can be found in the elegance-devkit v1\Demo\26 PWM_LED folder of the resource package we provide.
Here, we use PWM signals to control the on-board LED breathing light and print the PWM duty cycle. The code is as follows:
# Python env   : MicroPython v1.23.0
# -*- coding: utf-8 -*-
# @Time    : 2024/7/10 下午10:42
# @Author  : 李清水
# @File    : main.py
# @Description : PWM定时器实验,使用PWM完成呼吸灯

# ======================================== 导入相关模块 ========================================

# 导入硬件相关的模块
from machine import Pin, PWM, Timer
# 导入时间相关模块
import time

# ======================================== 全局变量 ============================================

# PWM占空比参数系数
pwm_duty = 0
# 占空比
value    = 0

# ======================================== 功能函数 ============================================

def breathing(t:Timer) -> None:
    '''
    呼吸灯函数,通过定时调整 PWM 占空比实现 LED 的呼吸效果。

    该函数通过定时器周期性调用,动态调整 PWM 占空比,使 LED 亮度从暗到亮再到暗,形成呼吸效果。

    Args:
        t (machine.Timer): 定时器实例,用于触发呼吸灯效果。

    Returns:
        None
    '''
    # 声明全局变量
    global pwm_duty,value,LED

    # 调整LED的占空比,占空比为value / 65535
    value = int(abs(32000- pwm_duty*1000))
    LED.duty_u16(value)

    # 根据当前占空比计算下一个占空比
    pwm_duty = (pwm_duty + 1) % 64

    # 打印输出PWM占空比
    print('PWM duty :', LED.duty_u16() / 65535)

# ======================================== 自定义类 ============================================

# ======================================== 初始化配置 ==========================================

# 上电延时3s
time.sleep(3)
# 打印调试信息
print('FreakStudio:Using PWM to control LED')

# 配置呼吸LED引脚
LED = PWM(Pin(25), freq=100, duty_u16=0)

# 创建一个定时器对象
timer = Timer(-1)
# 定时器初始化:周期运行,回调函数为breathing,过100ms执行一次
timer.init(period=100, mode=Timer.PERIODIC, callback=breathing)

# ========================================  主程序  ============================================

# 主程序
while True:
    pass
As you can see, we use a timer to call the breathing function every 100ms, and this function continuously adjusts the duty cycle of the LED to achieve the effect of a breathing lamp. It should be noted here that we have declared a global variable in the breathing method to ensure normal operation:
# 声明全局变量
global pwm_duty,value,LED
After burning the code, we can see that the LED realizes the breathing light effect, gradually dimming from bright to dark and then brightening from dark to bright:
Meanwhile, the terminal outputs the data of PWM duty cycle:
 
Documents
Comments Write