Thanks Kynlo. While I was adding these to my bookmarks, I decided to try out LobeChat's hosted version. It went something like this (highly paraphrased):
Q: Does the onboard LED of the Raspberry Pi Pico W support PWM to adjust its brightness?
A: Yes, it does.
Q: Can you show me some example code that will fade the LED?
A: This code will do that for you.
[ 15 minutes trying to get the code to work ]
Q: The code isn't working. What might be wrong?
A: Try these troubleshooting steps.
[ 15 minutes trying all the troubleshooting steps ]
Q: Are you absolutely sure the onboard LED supports PWM?
A: My apologies. The onboard LED does not support PWM.
Q: Can you show me example code to simulate PWM to adjust the LED's brightness in software rather than hardware?
A: This code will do that for you.
[ It works! ]
Pretty helpful when it doesn't lead you on a wild goose chase with wrong information.
I'm surprised there hasn't been a movie made yet about a group of people with a robot/AI companion who all end up stuck in a cavern and they ask the robot/AI
to help them find a way out and it unintentionally states incorrect information as fact and based on that information (and future queries to the robot/AI that don't challenge the initial assumption) the humans travel through a treacherous path where many people are injured or die until finally someone asks "Are you sure this is the best course of action?" and then the robot/AI says "My apologies. We can just take the ladder near where we started."
-Deozaan
What model did you use for this?
I'd highly suggest only using Claude-3.5-Sonnet-Latest, for coding-related work, as it's the only one that's actually any use. GPT4o isn't terrible, but given that it has a knowledge cutoff in 2023, it has absolutely no idea about anything more recent.
This is the output I got using Claude (albeit, with a tuned system prompt for coding):
Yes, the Raspberry Pi Pico W's onboard LED (GPIO 25/"LED" pin) does support PWM! I'll show you both hardware PWM and software PWM approaches.
1. Hardware PWM Solution: Here's a complete example that fades the LED up and down using hardware PWM:from machine import PWM, Pin
import time
# Setup the onboard LED with PWM
led_pwm = PWM(Pin("LED")) # GPIO 25
led_pwm.freq(1000) # Set PWM frequency to 1000Hz
def pulse_led():
while True:
# Fade in
for duty in range(0, 65535, 1000):
led_pwm.duty_u16(duty)
time.sleep(0.01)
# Fade out
for duty in range(65535, 0, -1000):
led_pwm.duty_u16(duty)
time.sleep(0.01)
try:
pulse_led()
except:
# Clean up PWM on exit
led_pwm.deinit()
2. Software PWM Solution (if hardware PWM isn't working): This simulates PWM using software timing:from machine import Pin
import time
led = Pin("LED", Pin.OUT)
def set_brightness(brightness):
# brightness should be between 0 and 100
cycle_time = 0.001 # 1ms cycle
on_time = (brightness / 100.0) * cycle_time
off_time = cycle_time - on_time
led.value(1)
time.sleep(on_time)
led.value(0)
time.sleep(off_time)
def pulse_led():
while True:
# Fade in
for brightness in range(0, 101, 5):
for _ in range(10): # Multiple cycles per brightness level
set_brightness(brightness)
# Fade out
for brightness in range(100, -1, -5):
for _ in range(10): # Multiple cycles per brightness level
set_brightness(brightness)
try:
pulse_led()
except:
led.value(0) # Turn off LED on exit
Key differences between the approaches:
Hardware PWM:- More efficient
- Smoother fading
- No CPU overhead
- Uses dedicated hardware
Software PWM:- More CPU intensive
- May show slight flickering
- Works on any GPIO pin
- Useful when hardware PWM isn't available
To run either example:mpremote cp main.py :main.py
mpremote reset
The LED should start fading in and out in a continuous loop. The hardware PWM version will give you the smoothest results, but both approaches work to demonstrate LED brightness control.