3.PWM输出测试
该教程演示如何通过开发板的GPIO实现PWM输出。
一、准备工作
- 准备1块BearPi-Pico RP2040开发板
二、编写代码
点击加号新建文件。
编写PWM输出的程序,将以下代码复制到新建的文件中。
# Example using PWM to fade an LED. import time from machine import Pin, PWM # Construct PWM object, with LED on Pin(25). pwm = PWM(Pin(25)) # Set the PWM frequency. pwm.freq(1000) # Fade the LED in and out a few times. duty = 0 direction = 1 while True: for _ in range(8 * 256): duty += direction if duty > 255: duty = 255 direction = -1 elif duty < 0: duty = 0 direction = 1 pwm.duty_u16(duty * duty) time.sleep(0.001)
"ctrl+s"将文件保存到电脑中。
三、测试
点击运行,开发板的LED灯出现呼吸灯的效果 。