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

PWM glitch/duty inversion when rapidly changing duty cycle

I need to create a backlight fade-in/out effect. For this I'm using the latest nrf_pwm lib form here together with the S110 Softdevice. Using the scheduler and an application timer I'm updating every ~31.25ms the duty cycle using nrf_pwm_set_value() in order to create the fade effect. This basically works, however once in a while during a fade I'm seeing glitches and often the duty cycle seems to get inverted and screws up the fading.

Searching this forum I came along these questions that seem to address the same issue, but I still don't know how to solve this problem: Question,Qeustion,Question

I then tried using the functions provided in nrf_pwm_noglitch.h (also from here). Here the problem with that appears that it doesn't update the duty cycly with every call to nrf_pwm_set_value() and the resulting fade isn't looking smooth anymore.

I need some hints how to solve this problem or what I'm doing wrong ...

This how I'm initializing the pwm lib:

   nrf_pwm_config_t pwm_config = PWM_DEFAULT_CONFIG;
   pwm_config.mode             = PWM_MODE_LED_1000;
   pwm_config.num_channels     = 1;
   pwm_config.gpio_num[0]      = LIGHT_DIM;

   // Initialize the PWM library
   nrf_pwm_init(&pwm_config);
   nrf_pwm_set_value(0,0);
  • Hi Michael

    Which SDK and SoftDevice version are you using?

    Also, are you able to share your code project? If yes, please just zip the project folder, including source and project files.

    Regards Torbjørn

  • Hi Torbjorn, I'm using sdk version 6.1.0 and SoftDevice s110 7.1.0. I've created a support case in which I attached source for the nRF6310+PCA10004 DK that demonstrate the issue by fading LED 1 on the board in and out.

  • Hi All,

    I have a similar problem. I have a nRF51 dongle, using SDK 7.1.0 without SD. I'm using two channels. One seems fine, the other one sometimes seems to miss an event, causing the signal to change polarity. I.e. when I set 15% duty it is actually 85%

    I've also tried the nrf_pwm_noglitch driver. However, then only one channel is working. The one that is working, seems to work fine.

    This is my code:

    #include <stdbool.h>
    #include <stdint.h>
    #include "nrf.h"
    #include "nrf_gpio.h"
    #include "nrf_delay.h"
    #include "nrf_pwm.h"
    #include "boards.h"
        
    int main(void)
    {
        uint32_t counter = 0;
        
        nrf_pwm_config_t pwm_config = PWM_DEFAULT_CONFIG;
    
    	 /* This config will generate a frequency of 20kHz */
        pwm_config.mode             = PWM_MODE_MTR_100;
        pwm_config.num_channels     = 2;
        pwm_config.gpio_num[0]      = 15;
        pwm_config.gpio_num[1]      = 16;
        
        nrf_pwm_init(&pwm_config);
    	
    	 /* Change the resolution from 100 to 1000, resulting in a 
    	 * PWM frequency of 2kHz with a resolution from 0-1000 */
    	 nrf_pwm_set_max_value(1000);
    
        while (true)
        {
    	    nrf_pwm_set_value(0, counter);
            nrf_pwm_set_value(1, counter);
    			
            counter = (counter + 10) % 1000;
            
            nrf_delay_us(1000);
        }
    }
    
Related