// load in the Zephyr library#include <zephyr/drivers/pwm.h>// define structs based on DT aliasesstaticconststruct pwm_dt_spec pwm1 = PWM_DT_SPEC_GET(DT_ALIAS(pwm1));staticconststruct pwm_dt_spec pwm2 = PWM_DT_SPEC_GET(DT_ALIAS(pwm2));// check that the PWM controller is readyif(!device_is_ready(pwm1.dev)){ LOG_ERR("PWM device %s is not ready.", pwm1.dev->name);return-1;}// set the PWM duty cycle (pulse length)err = pwm_set_pulse_dt(pwm1, pwm1->period/2);// 50% duty cycleif(err){ LOG_ERR("Could not set pwm1 driver.");}
Changing PWM Duty Cycle
The pulse length (duty cycle) can be changed “on the fly”, but only changes at the next period.
Can be done with a timer or event-driven.
How to Sinusoidally Module LED Brightness Using a PWM
Use a timer to change the duty cycle of the PWM signal. How often?