#include "pico/stdlib.h"
#include "hardware/pwm.h"
#define LED_PIN 22 // Change to GPIO where the LED is connected
int main() {
// Initialize the GPIO
gpio_set_function(LED_PIN, GPIO_FUNC_PWM); // Set GPIO to PWM function
// Get the PWM slice number for the specified GPIO
uint slice_num = pwm_gpio_to_slice_num(LED_PIN);
// Set the PWM frequency
pwm_set_wrap(slice_num, 255); // Set TOP value for 8-bit resolution (0-255)
pwm_set_clkdiv(slice_num, 4.0f); // Set the clock divider to control frequency
// Enable PWM on the specified slice
pwm_set_enabled(slice_num, true);
while (true) {
// Gradually increase brightness
for (int i = 0; i <= 255; i++) {
pwm_set_gpio_level(LED_PIN, i); // Set PWM duty cycle
sleep_ms(10); // Wait to see the brightness change
}
// Gradually decrease brightness
for (int i = 255; i >= 0; i--) {
pwm_set_gpio_level(LED_PIN, i); // Set PWM duty cycle
sleep_ms(10); // Wait to see the brightness change
}
}
return 0;
}