How to properly use LEDC library for ws2812s RGB Leds?

ujurmsde
Posts: 38
Joined: Tue Jan 19, 2021 6:37 am

How to properly use LEDC library for ws2812s RGB Leds?

Postby ujurmsde » Sun Feb 13, 2022 5:08 am

I was trying to create an application to control the colors for ws2812s RGB led strips using the LEDC library provided by espressif.
There are several questions I have in mind
The ws2812s control uses the PWM data to control the rgb values. Can it be possible to switch(0 or 1) the duty cycle of PWM so fast that it can meet the timing requirements for the rgb leds?

I tried to use the software method to change the duty cycle. but it seems that there are issues.


https://docs.espressif.com/projects/esp ... re-channel

The following is the code example I am using. The bad part is that I am using for() loop for sending PWM bits, which could increase the overhead and send totally wrong values. How to deal with this?

Code: Select all


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "driver/spi_master.h"
#include "driver/gpio.h"
#include "driver/ledc.h"

#include "esp_log.h"

#define DUTY_1 (5.6)
#define DUTY_0 (2.8)


static const char TAG[] = "main";

void app_main(void){

  esp_err_t ret;
  ESP_LOGI(TAG, "Initializing the example");

  ledc_timer_config_t config = {
    .speed_mode = LEDC_LOW_SPEED_MODE,
    .timer_num = LEDC_TIMER_0,
    .duty_resolution = LEDC_TIMER_3_BIT,
    .freq_hz = 800000,
    .clk_cfg = LEDC_AUTO_CLK,
  };
  
  ESP_ERROR_CHECK(ledc_timer_config(&config));

  ledc_channel_config_t cfconfig = {
    .speed_mode = LEDC_LOW_SPEED_MODE,
    .channel = LEDC_CHANNEL_0,
    .timer_sel = LEDC_TIMER_0,
    .intr_type = LEDC_INTR_DISABLE,
    .gpio_num = 5,
    .duty = 0,
    .hpoint = 0
  };

    ESP_ERROR_CHECK(ledc_channel_config(&cfconfig));
    
    //red color

    for(uint8_t i=0 ; i<=7 ; i++){
      ESP_ERROR_CHECK(ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, DUTY_0));
      ESP_ERROR_CHECK(ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0));
    }
    for(uint8_t i=0 ; i<=7 ; i++){
      ESP_ERROR_CHECK(ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, DUTY_0));
      ESP_ERROR_CHECK(ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0));
    }
    for(uint8_t i=0 ; i<=7 ; i++){
      ESP_ERROR_CHECK(ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, DUTY_1));
      ESP_ERROR_CHECK(ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0));
    }
   


ESP_Sprite
Posts: 9617
Joined: Thu Nov 26, 2015 4:08 am

Re: How to properly use LEDC library for ws2812s RGB Leds?

Postby ESP_Sprite » Sun Feb 13, 2022 7:34 am

You can't control intelligent LEDs like those with 'dumb' PWM signals as generated by LEDC. Suggest instead you look at the blink example; the led_strip code there can control WS2812-type LEDs.

ujurmsde
Posts: 38
Joined: Tue Jan 19, 2021 6:37 am

Re: How to properly use LEDC library for ws2812s RGB Leds?

Postby ujurmsde » Sun Feb 13, 2022 12:07 pm

Thanks for reply. I think RMT is used for this purpose which can drive smart rgb leds.!

Who is online

Users browsing this forum: Baidu [Spider] and 77 guests