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

How to Drive RGB LED with PWM

Hi Nordic Team, i am working on nrg52 to glow a RGB LED with white and Yellow colours. configured the RGB pins to three different Gpio (A2,A3,A4).

for this i am using PWM library example in which i can only configure 2 channel(2 pins) to drive PWM to Gpio pins. if i configure red and green pins using PWM i unable to get yellow color.

i am getting mixed colour of red,green, yellow. at wt fre quency(time period) i should operate PWM to get pure Yellow colour, and also how i can configure 3 pins to drive PWM using PWM library example.

here is my code.

APP_PWM_INSTANCE(pwm_library,1);                   // Create the instance "PWM1" using TIMER1.

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

void pwm_ready(uint32_t pwm_id)    // PWM callback function
{
    ready_flag = true;
}
uint32_t time_period = 2000L;
int main(void)
{

	ret_code_t err_code;
	 printf("VIBRATION:\r\n");
	    /* 2-channel PWM, 200Hz, output on DK LED pins. */
	    app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_2CH(time_period, ARDUINO_2_PIN,ARDUINO_A4_PIN);

	    /* Switch the polarity of the second channel. */
	    pwm1_cfg.pin_polarity[1] = APP_PWM_POLARITY_ACTIVE_HIGH;
	    pwm1_cfg.pin_polarity[0] = APP_PWM_POLARITY_ACTIVE_HIGH;
	    /* Initialize and enable PWM. */
	    err_code = app_pwm_init(&pwm_library,&pwm1_cfg,pwm_ready);
	    APP_ERROR_CHECK(err_code);
	    app_pwm_enable(&pwm_library);

	    while(true){

for(int i = 0; i<= 100; i++){

	    app_pwm_channel_duty_set(&pwm_library, 0, i);

	    nrf_delay_ms(100);

}

}

}
Parents
  • Hi,

    You will need to create a new PWM instance to get a third channel with the PWM library. However, since you are using the nRF52832 I would recommend using the PWM driver instead(nrf_drv_pwm). 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. Take a look at e.g. this devzone answer where a user configured the PWM driver for a RGB LED.

    To get a yellow light you need to drive the red and green pin. You should be able to find the appropriate frequency in the datasheet for the RGB LED you are using.

  • The pins will be set to 0 voltage after you call stop_LED_glow(), but if your RGB LED is active low, the RGB LED will still glow. You can try to set the pins high in the stop_LED_glow function to test this:

    void stop_LED_glow(){
    
        nrf_drv_pwm_stop(&m_pwm0,false);
        nrf_drv_pwm_uninit(&m_pwm0);
        
        nrf_gpio_cfg_output(gLEDR);
        nrf_gpio_pin_set(gLEDR);
        
        nrf_gpio_cfg_output(gLEDG);
        nrf_gpio_pin_set(gLEDG);
        
        nrf_gpio_cfg_output(gLEDB);
        nrf_gpio_pin_set(gLEDB);
    }
    
Reply
  • The pins will be set to 0 voltage after you call stop_LED_glow(), but if your RGB LED is active low, the RGB LED will still glow. You can try to set the pins high in the stop_LED_glow function to test this:

    void stop_LED_glow(){
    
        nrf_drv_pwm_stop(&m_pwm0,false);
        nrf_drv_pwm_uninit(&m_pwm0);
        
        nrf_gpio_cfg_output(gLEDR);
        nrf_gpio_pin_set(gLEDR);
        
        nrf_gpio_cfg_output(gLEDG);
        nrf_gpio_pin_set(gLEDG);
        
        nrf_gpio_cfg_output(gLEDB);
        nrf_gpio_pin_set(gLEDB);
    }
    
Children
No Data
Related