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.

Reply
  • 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.

Children
Related