This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

NRF52 Dim LED with PWM Driver

I am migrating from nrf51 to nrf52. Using SDK12.1

On NRF51, I used the PWM Library to use 2 buttons on the DK to dim up or down onboard LED1 (called OUTPUT_LED). It worked great (not sure if it did it properly or not). The code was fairly simple.

I set up the PWM

APP_PWM_INSTANCE(PWM1,1);

I then initialize PWM:

app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(5000L, OUTPUT_LED);

app_pwm_init(&PWM1,&pwm1_cfg,pwm_ready_callback);

app_pwm_enable(&PWM1);

while (app_pwm_channel_duty_set(&PWM1, 0, pwm_value) == NRF_ERROR_BUSY);

I then call the code in the button handler to dim up or down.

if (button_action == APP_BUTTON_PUSH)  { 

switch (pin_no)
{
    case DIM_UP_BUTTON:
        if (pwm_value<100)
        {
           ++pwm_value; 
        }
            
        while (app_pwm_channel_duty_set(&PWM1, 0, pwm_value) == NRF_ERROR_BUSY);
        break;

    case DIM_DOWN_BUTTON:
        if (pwm_value >1)
        {
            --pwm_value;    
        }
        
        while (app_pwm_channel_duty_set(&PWM1, 0, pwm_value) == NRF_ERROR_BUSY);
        
        break;
        
    default:
        APP_ERROR_HANDLER(pin_no);
        break;}

I am looking at the examples for using the hardware PWM driver that the nrf52 has. They look super confusing! So many parameters.I just want to dim an LED at a set frequency. Can someone help me with a code example for the PWD driver that will work similarly? I would like to do this using the nrf52 hardware PWM versus using the extra resources needed with the PWM library method. I appreciate the help!, Bryan

Related