Raspberry Pi Pico
PWM - RGB LED
An RGB LED is a nice way to test using PWM in CircuitPython. The photograph shows one connected to GP10, GP11 and GP12. A 220Ohm resistor is used in series with each of those connections. The long leg of the package is connected to GND.
This Fritzing shows how I connected up my circuit.
For the first program, I am only using the red LED in the package. You could do this with a single LED connected to GP10. The program fades the LED on and off.
import board from time import sleep from pwmio import PWMOut red = PWMOut(board.GP10, frequency=1000) while True: for i in range(0,65536,100): red.duty_cycle = i sleep(0.01) for i in range(65535, -1, -100): red.duty_cycle = i sleep(0.01)
The next program includes a procedure for setting an RGB colour on the RGB LED using values from 0 to 255 for each of the LEDs. In the program I turn the LEDs on individually. Mess around with the arguments of the procedure calls and you can mix your own colours.
import board from time import sleep from pwmio import PWMOut red = PWMOut(board.GP10, frequency=1000) green = PWMOut(board.GP11, frequency=1000) blue = PWMOut(board.GP12, frequency=1000) def rgb(r,g,b): red.duty_cycle= r * 256 green.duty_cycle= g * 256 blue.duty_cycle= b * 256 while True: rgb(255,0,0) sleep(1) rgb(0,255,0) sleep(1) rgb(0,0,255) sleep(1) rgb(0,255,255) sleep(1) rgb(255,0,255) sleep(1)