/*
** Raspberry Pi Pico SDK PWM Example
** Originally from Pico PMW Example, simplified
**
*/
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#include "hardware/pwm.h"
#define GPIO_LED 3
#define MAX_DUTY_CYCLE 100 // percent
int main() {
int change = 10;
int dutyPercent = 0;
float clkdiv = 255.0;
stdio_init_all(); // UART setup for both input and output
// PWM setup
gpio_set_function(GPIO_LED, GPIO_FUNC_PWM);
uint slice_num = pwm_gpio_to_slice_num(GPIO_LED);
pwm_set_clkdiv(slice_num, clkdiv);
pwm_set_wrap(slice_num, MAX_DUTY_CYCLE);
pwm_set_enabled(slice_num, true);
while (1) {
dutyPercent += change;
if (dutyPercent >= MAX_DUTY_CYCLE) {
dutyPercent = 0;
} // when at max, go directly back to zero
pwm_set_gpio_level(GPIO_LED, dutyPercent);
printf("LED is now %d\r\n", dutyPercent);
sleep_ms(200);
}
}