This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

RGB LED with NRF5340

Hi,

I need your help to know "How to control RGB LED using PWM in nrf5340?". I have already tried the "RGB_led" sample in NRFconnect SDK v1.5.0 

But can't able to control my led. Kindly Support me to understand the Project and also to get output.

Thanks & Regards 

Navin Chakravarthy K

Parents
  • Hi,

    I am looking into it and will get back to you within a day or two. Also, by controlling the LED, could you be more specific as to how you wish to do so?

    Regards,

    Priyanka

  • Hi Priyanka,

     I want to use via PWM individual mode of nrf5340 or SDK already has RGB LED samples  I need to know how to use that sample for nrf5340

    Thanks & regards

    Navin

  • Hi Navin,

    Since the nrf5340 has only 4 green LEDS, and does not have dedicated red or blue LEDs, while running the ''rgb_led'' exampleyou will have to consider any two LEDs to be red and blue respectively (for eg: led0 -> green, led1-> blue, led2 -> red) 

    Making any changes in the main.c file may cause errors so it would be better if this change is reflected in the .dts file. For which, you don't have to modify that, but you can create a .overlay file inside the ''rgb_led'' folder. Here, you can mention the changes that you want to see in the .dts file (the LEDs you wish to set). 

    This will help you build and flash the program onto the DK. 

    Also, I would recommend you to use the latest version of nrf Connect.

    Hope this helps Slight smile

    Kind Regards,

    Priyanka

  • Hi Priyanka,

    Basically, RGB led means multicolor controllable led. For your reference "www.circuitbread.com/.../how-rgb-leds-work-and-how-to-control-color"

    And that can be controlled via PWM. Kindly, Check that link and suggest a way to control multicolor led with nrf5340

    Thanks & Regards

    Navin Chakravarthy K

  • Hi Navin,

    I guess there was a bit of a confusion in what I mentioned yesterday. I get that an RGB Led is a multicolour LED but there is no such LED on nrf5340 DK. It has just 4 green LEDs.

    But if you need to run the 'rgb_led' example with an RGB Led, then you will have to use an external analog RGB Led and also, create a  .overlay file with the necessary pwm leds initialised. You can do it like I mentioned yesterday.

    Priyanka said:
    Making any changes in the main.c file may cause errors so it would be better if this change is reflected in the .dts file. For which, you don't have to modify that, but you can create a .overlay file inside the ''rgb_led'' folder. Here, you can mention the changes that you want to see in the .dts file (the LEDs you wish to set). 

    I will attach my .overlay file for your reference. Also, make sure your RGB led pins are connected to the proper pins and you can calculate the resistance required by using your voltage supply and current limit from the datasheet of the RGB Led.

    As for controlling the LED, you could try making changes in the main.c of the 'rgb_led' example. At present, it runs as per a for loop with a specific count that is defined. You could try changing this. In case you need to control them via buttons, you may have to again define them in the .overlay file.

    5305.nrf5340dk_nrf5340_cpuapp.overlay

    Hope this helps.

    Kind Regards,

    Priyanka

Reply
  • Hi Navin,

    I guess there was a bit of a confusion in what I mentioned yesterday. I get that an RGB Led is a multicolour LED but there is no such LED on nrf5340 DK. It has just 4 green LEDs.

    But if you need to run the 'rgb_led' example with an RGB Led, then you will have to use an external analog RGB Led and also, create a  .overlay file with the necessary pwm leds initialised. You can do it like I mentioned yesterday.

    Priyanka said:
    Making any changes in the main.c file may cause errors so it would be better if this change is reflected in the .dts file. For which, you don't have to modify that, but you can create a .overlay file inside the ''rgb_led'' folder. Here, you can mention the changes that you want to see in the .dts file (the LEDs you wish to set). 

    I will attach my .overlay file for your reference. Also, make sure your RGB led pins are connected to the proper pins and you can calculate the resistance required by using your voltage supply and current limit from the datasheet of the RGB Led.

    As for controlling the LED, you could try making changes in the main.c of the 'rgb_led' example. At present, it runs as per a for loop with a specific count that is defined. You could try changing this. In case you need to control them via buttons, you may have to again define them in the .overlay file.

    5305.nrf5340dk_nrf5340_cpuapp.overlay

    Hope this helps.

    Kind Regards,

    Priyanka

Children
  • Hi Priyanka,

    Sorry for the delay, Now I am facing some issues because of nrf connect to update. Afterward, I will contact you

    Regards

    Navin

  • Hi Priyanka,

    The above solution was working. I need to know one more thing. Is there any chance to use the below code for nrf5340.

    /**
     */
    
    #include <stdio.h>
    #include <string.h>
    #include "nrf_drv_pwm.h"
    #include "app_util_platform.h"
    #include "app_error.h"
    #include "boards.h"
    #include "bsp.h"
    #include "app_timer.h"
    #include "nrf_drv_clock.h"
    #include "nrf_delay.h"
    
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    #include "nrf_log_default_backends.h"
    
    
    #define m_top     100
    
    #define  led_red    22
    #define  led_green  23
    #define  led_blue   24
    
    
    
    // Create an instance of pwm0
    static nrfx_pwm_t m_pwm0 = NRFX_PWM_INSTANCE(0);
    
    
    
    // initialize the sequence values to 0; we will update them later in the code
    nrf_pwm_values_individual_t seq_values[] = {0, 0, 0, 0};
    
    
    // Create a sequence struct which points to sequence values along with the other properties
    nrf_pwm_sequence_t const seq0 =
    {
      .values.p_individual = seq_values, // use individual here remaining code is similar to previous examples
      .length              = NRF_PWM_VALUES_LENGTH(seq_values),
      .repeats             = 0,
      .end_delay           = 0
    
    };
    
    
    
    
    
    
    // We will create a simple function which will change the sequence values;
    // what ever values we assign it will continue to play those values unles we call this function to change the values
    // in this example we will just simply use single values for all the channels and use the loop flag to play the same 
    // values making a constant duty cycle, which can be increased or decreased by updating these values
    // we use these duty cycle values to change the brightness of the led and by setting different brightness levels
    // we can make any color using RGB led
    static void pwm_update_duty_cycle(uint8_t duty_red, uint8_t duty_green, uint8_t duty_blue)
    {
    
      if(duty_red >= 100) // check if duty passed is greater then 100 set it to 100
      {
        seq_values->channel_0 = 100; // Set the value for channel 0
      
      }
    
      else
      {
        seq_values->channel_0 = duty_red; // Set the value for channel 0
      }
    
    
      if(duty_green >= 100)// check if duty passed is greater then 100 set it to 100
      {
        seq_values->channel_1 = 100; // Set the value for channel 1
      
      }
    
      else
      {
        seq_values->channel_1 = duty_green; // Set the value for channel 1
      }
    
    
    
    
      if(duty_blue >= 100)// check if duty passed is greater then 100 set it to 100
      {
        seq_values->channel_2 = 100; // Set the value for channel 2
      
      }
    
      else
      {
        seq_values->channel_2 = duty_blue; // Set the value for channel 2
      }
    
    
    
      //this funtion play's the sequence values, we pass it the seuqence but we have updated the values 
      // for the specific channels so the updated values will be played
      (void)nrfx_pwm_simple_playback(&m_pwm0, &seq0, 1, NRFX_PWM_FLAG_LOOP);
    
    
    }
    
    
    
    // initilzation is similar to previous codes
    
    
    static void pwm_init(void)
    {
    
    
        nrfx_pwm_config_t const config0 = 
        {
          .output_pins = 
          {
              led_red,
              led_green,
              led_blue,
              NRFX_PWM_PIN_NOT_USED
          },
          .irq_priority = APP_IRQ_PRIORITY_LOWEST,
          .base_clock   = NRF_PWM_CLK_1MHz,
          .count_mode   = NRF_PWM_MODE_UP,
          .top_value    = m_top,
          .load_mode    = NRF_PWM_LOAD_INDIVIDUAL, // selected INDIVIDUAL 
          .step_mode    = NRF_PWM_STEP_AUTO
    
        };
    // initialize the pwm0
        APP_ERROR_CHECK(nrfx_pwm_init(&m_pwm0, &config0, NULL));
    }
    
    
    
    // initialize the log
    
    static void log_init(void)
    {
      ret_code_t err_code = NRF_LOG_INIT(NULL);
      APP_ERROR_CHECK(err_code);
    
      NRF_LOG_DEFAULT_BACKENDS_INIT();
    
      }
    
    
    
    // Main Function
    
    
    int main(void)
    {
    
    log_init();
    
    bsp_board_init(BSP_INIT_LEDS);
    
    pwm_init();
    
      
      NRF_LOG_INFO("PWM application started!!");
    
      while(true)
      {
    
    // Here we call this update function 
    // we can send any value b/w 0 to 100
    // this value will refelct the duty cycle
    // Note: if you want to change the frequency of waves then change the m_top VALUE, BUT you will have to adjust the duty cycle values accordingly
    // here we are simply changing duty cycles to increase or decrease the brightness of each colors
    // using this we can make any color from RGB led 
         pwm_update_duty_cycle(0, 0, 0);
         nrf_delay_ms(1000);
    
         pwm_update_duty_cycle(100, 0, 0);
         nrf_delay_ms(1000);
    
         pwm_update_duty_cycle(0, 100, 0);
         nrf_delay_ms(1000);
    
         pwm_update_duty_cycle(0, 0, 100);
         nrf_delay_ms(1000);
    
         pwm_update_duty_cycle(50, 70, 0);
         nrf_delay_ms(1000);
    
         pwm_update_duty_cycle(0, 40, 60);
         nrf_delay_ms(1000);
    
         pwm_update_duty_cycle(70, 0, 60);
         nrf_delay_ms(1000);
      
         
      }
       
    }
    
    
    /** @} */
    

    Kindly, Help me I need to glow red for some time and green for some time based on certain conditions

    Thanks & Regards

    Navin Chakravarthy K

  • Hi Navin,

    There are basically two SDKs. The nrf5 SDK (which was used for all the previous SoC) and there is the nRF Connect SDK (which is new, more commonly used now and is recommended). The nRF5340 DK has a dual core Bluetooth SoC and this does not support the nrf5 SDK. You have to use the nRF Connect SDK for this. You can read about this here.

    Also, the file that you shared is written for nrf5 SDK and cannot be used for the nRF5340 DK. Like I mentioned earlier, if you need to glow R, G & B at different conditions, you could try modifying the main.c of the rgb_led program.

    Hope this helps !

    Regards,

    Priyanka

  • Hi Priyanka,

    I have tried that sample but, it didn't work.  I can able to glow the same LED by using Arduino. But, with nrf5340 it was not working. The LED was a common cathode type. The common pin was directly connected to the ground. The RGB pins are connected with respective I/O pins of nrf5340 DK as mentioned in the Overlay file through 220-ohm resistance. Kindly,  help me to glow the RGB LED from nrf5340

    Thanks & Regards

    Navin Chakravarthy KI

  • Hi Navin,

    I would like to know whether you are using the nRF5340 DK or nRF5340 PDK?

    Also, your connection seems correct. I will mention my steps below:

    1. Connect the R, G and B pins respectively to the pins mentioned in the overlay file. You can choose the PINS that you wish to configure by verifying the pin numbers written behind the DK that you are using and then mention them correctly in the overlay file. Make sure you save the overlay file in the rgb_led folder.

    2. While building using the SEGGER, the nrf5340dk_nrf5340_cpuapp was chosen as the board variant. 

    3. Build and flash using SEGGER.

    These worked well. Kindly verify these with yours. Also make sure you have the latest version of nRF Connect SDK.

    Regards,

    Priyanka

Related