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

Why am I having problems driving an RGB LED with PWM from NRF52??!

Hello Nordic Community.

I'm having issues trying to control a simple RGB LED circuit with the NRF52. I have set up 2 timers and PWM instances Red and Green on PWM1 and Blue on PWM2 but it's just not working. What am I doing wrong?? Here is my code (modified from the NORDIC PWM example)

#include <stdbool.h>
#include <stdint.h>
#include "nrf.h"
#include "app_error.h"
#include "bsp.h"
#include "nrf_delay.h"
#include "app_pwm.h"

static volatile bool ready_flag;            // A flag indicating PWM status.

bool enablePWM = true;
bool pausePWM = false;

void pwm_ready_callback(uint32_t pwm_id)    // PWM callback function
{
    ready_flag = true;
}


int main(void)
{
  uint32_t err_code;

    // Create the instance "PWM1" using TIMER1.
    APP_PWM_INSTANCE(PWM1,1);      
    // Create the instance "PWM2" using TIMER2.
    APP_PWM_INSTANCE(PWM2,2);             

    // RGB LED pins
    // (Common cathode)
    uint32_t pinR = 12;
    uint32_t pinG = 14;
    uint32_t pinB = 15;
   
    // 2-channel PWM, 200Hz
    app_pwm_config_t pwm1_cfg = 
      APP_PWM_DEFAULT_CONFIG_2CH(5000L, pinR, pinG);

    /* Initialize and enable PWM. */
    err_code = app_pwm_init(&PWM1,&pwm1_cfg,pwm_ready_callback);
    APP_ERROR_CHECK(err_code);
    app_pwm_enable(&PWM1);     
 
    // 1-channel PWM, 200Hz
    app_pwm_config_t pwm2_cfg = 
      APP_PWM_DEFAULT_CONFIG_1CH(5000L, pinB);

    /* Initialize and enable PWM. */
    err_code = app_pwm_init(&PWM2,&pwm2_cfg,pwm_ready_callback);
    APP_ERROR_CHECK(err_code);
    app_pwm_enable(&PWM2);

    uint32_t value;
    while(1) {

        for (uint8_t i = 0; i < 40; ++i)
        {
            value = (i < 20) ? (i * 5) : (100 - (i - 20) * 5);

            //ready_flag = false;
            /* Set the duty cycle - keep trying until PWM is ready... */
            while (app_pwm_channel_duty_set(&PWM1, 0, value) == NRF_ERROR_BUSY);

            nrf_delay_ms(25);
        }

         for (uint8_t i = 0; i < 40; ++i)
        {
            value = (i < 20) ? (i * 5) : (100 - (i - 20) * 5);

            //ready_flag = false;
            /* Set the duty cycle - keep trying until PWM is ready... */
            while (app_pwm_channel_duty_set(&PWM1, 1, value) == NRF_ERROR_BUSY);

            nrf_delay_ms(25);
        }

         for (uint8_t i = 0; i < 40; ++i)
        {
            value = (i < 20) ? (i * 5) : (100 - (i - 20) * 5);

            //ready_flag = false;
            /* Set the duty cycle - keep trying until PWM is ready... */
            while (app_pwm_channel_duty_set(&PWM2, 0, value) == NRF_ERROR_BUSY);

            nrf_delay_ms(25);
        }

    }

}
Parents
  • Since you are using nRF52 you should consider using the PWM driver, nrf_drv_pwm instead. This uses the PWM peripheral on the chip. Using the PWM library, app_pwm, on nRF52 you are wasting resources like timers and ppi and gpiote channels. Here is a snippet of code that sets the pwm driver to one duty cycle: devzone.nordicsemi.com/.../

    If you want to find out what is the problem with the code you are running now with app_pwm, you should go into debug mode and check that all the error codes are NRF_SUCCESS.

Reply
  • Since you are using nRF52 you should consider using the PWM driver, nrf_drv_pwm instead. This uses the PWM peripheral on the chip. Using the PWM library, app_pwm, on nRF52 you are wasting resources like timers and ppi and gpiote channels. Here is a snippet of code that sets the pwm driver to one duty cycle: devzone.nordicsemi.com/.../

    If you want to find out what is the problem with the code you are running now with app_pwm, you should go into debug mode and check that all the error codes are NRF_SUCCESS.

Children
No Data
Related