Control onboard LED of Raspberry Pi Pico 2/2W using MicroPython/CircuitPython
The onboard LED on Raspberry Pi Pico and Pico 2 is connected to GPIO25. On
Pico W and Pico 2 W, it's connected to the wireless chip.
Following
are exercise to control the onboard LED using MicroPython/CircuitPython,
tested on Pico 2 and Pico 2 W.
mpy_rp2_led.py
"""
MicroPython on Raspberry Pi Pico 2/2W
to control onboard LED
"""
import os, sys
import time
#import board
#from digitalio import DigitalInOut, Direction
sys_info_text = sys.implementation[0] + " " + os.uname()[3] +\
"\nrun on " + os.uname()[4]
print("=======================================")
print(sys_info_text)
print("=======================================")
led = machine.Pin("LED", machine.Pin.OUT)
print("led:", led)
# Control onboard led using led.on() and led.off()
while True:
led.on()
time.sleep(0.5)
led.off()
time.sleep(0.5)
"""
# Toggle onboard led using led.toggle()
while True:
led.toggle()
time.sleep(0.2)
"""
"""
# Toggle led in callback using Timer
from machine import Timer
timer = Timer()
def blink(timer):
led.toggle()
timer.init(freq=10, mode=Timer.PERIODIC, callback=blink)
while True:
pass
"""
"""
# using PWM
# Not work for 'W'
from machine import PWM
pwm = PWM(led)
print("pwm type", type(pwm))
pwm.freq(1000)
while True:
for brightness in range(0, 10000, 500):
pwm.duty_u16(brightness)
print("brightness:", brightness, end="\r")
time.sleep(0.1)
for brightness in range(10000, 0, -500):
pwm.duty_u16(brightness)
print("brightness:", brightness, end="\r")
time.sleep(0.1)
"""
cpy_rp2_led.py
"""
CircuitPython on Raspberry Pi Pico 2/2W
to control onboard LED
"""
import os, sys
import time
import board
from digitalio import DigitalInOut, Direction
sys_info_text = sys.implementation[0] + " " + os.uname()[3] +\
"\nrun on " + os.uname()[4]
print("=======================================")
print(sys_info_text)
print("=======================================")
led = DigitalInOut(board.LED)
led.direction = Direction.OUTPUT
print("board.LED:", type(board.LED))
# Turn onboard LED ON/OFF by value
while True:
led.value =True
time.sleep(0.5)
led.value = False
time.sleep(0.5)
"""
# Toggle onboard LED
while True:
led.value = not led.value
time.sleep(0.25)
"""
cpy_rp2_led_pwm.py
"""
CircuitPython on Raspberry Pi Pico 2/2W
to control onboard LED using PWM
"""
import os, sys
import board
import pwmio
import time
sys_info_text = sys.implementation[0] + " " + os.uname()[3] +\
"\nrun on " + os.uname()[4]
print("=======================================")
print(sys_info_text)
print("=======================================")
print("board.LED:", type(board.LED))
led = pwmio.PWMOut(board.LED, frequency=1000, duty_cycle=0)
# Control brightness using PWM
# not work on Raspberry Pi Pico 2 W
while True:
for brightness in range(0, 10000, 500):
led.duty_cycle=brightness
print("brightness:", brightness, end="\r")
time.sleep(0.1)
for brightness in range(10000, 0, -500):
led.duty_cycle=brightness
print("brightness:", brightness, end="\r")
time.sleep(0.1)
Comments
Post a Comment