Welcome to Raspberry Pi Pico 2 repository
5. PWM pin
5. PWM pin
1. Learning objectives
- Learn to control LED lights with the Raspberry Pi Pico 2.
- Understand the PWM output of the Raspberry Pi Pico 2.
2. Hardware construction
This course.
LED light*1
220Ω resistor*1
3. Program analysis
Thonny programming
For the use of ThonnyIDE, please refer to the previous environment construction related courses.
xxxxxxxxxximport machineimport utimeled = machine.PWM(machine.Pin(15))led.freq(1000)while True:for i in range(65535):led.duty_u16(i)utime.sleep(0.0005)
import machine
The machine library contains all the instructions MicroPython needs to communicate with Pico and other MicroPython-compatible devices, extending the language of physical computing.
import utime
The "utime" library. This library handles everything to do with time, from measuring it to inserting delays into your program. The units are in seconds.
led = machine.PWM(machine.Pin(15))
Set IO15 to PWM output, that is, use the B output of PWM slice 7.
led.freq(1000)
Set the frequency to 1000 Hz - 1000 cycles per second.
led.duty_u16(i)
Set the duty cycle.
Through the While loop, modify the PWM duty cycle value through the for loop.
4. Experimental phenomenon
After the program is downloaded, we can see the process of the LED light slowly turning on from off.