这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 嵌入式开发 » 国产MCU » 【ESP32S31】3、blink点灯——呼吸灯

共1条 1/1 1 跳转至

【ESP32S31】3、blink点灯——呼吸灯

高工
2026-06-14 09:22:26     打赏

【前言】

本篇体验如何实现花式点灯。

开发板上板载了一个RGB灯,用的芯片是ws2812b。开发板的RGB灯原理图如下:

image.png

接入的IO为37号引脚:

image.png

在核心脚上也有介绍:

image.png


【实现步骤】

1、复制example\get_start下面的blink文件夹到app目录下面。

2、使用idf的端终进入这个目录。

3、使用idf 配置为esp32s31

idf.py --preview set-target esp32s31

4、进入menucofig确认blink的引脚 

idf.py menuconfig

image.png

在这里我们还可以配置闪灯间隔时间、以及驱动总线的方式(LED strip),由开发板的RGB为ws2812b因此需要确定strip方式。如果是普通的gpio就需要修改为GPIO方式。

最后保存退出menuconfig。

5、编译

idf.py --preview build

6、下载

按照我前面的编译介绍,按住boot键不放,然后按reset键,松开reset键,然后松开boot按键,进入下载模式。输入命令:

idf.py -p COM21 flash

7、观察RGB灯,发现周期闪烁了。同时日志在打印:

image.png

【进阶】

blink的例程是亮与灭,为了实现更好的效果我把他修改为呼吸灯效果。代码如下:

void app_main(void)
{
    /* Configure the peripheral according to the LED type */
    configure_led();

    /* Breathing parameters
       - period_ms : full inhale + exhale duration (taken from BLINK_PERIOD
                     in menuconfig, so the cycle length stays in sync with
                     the rest of the project's configuration)
       - step_ms   : refresh interval (smaller = smoother, more CPU traffic) */
    const int period_ms = CONFIG_BLINK_PERIOD;
    const int step_ms   = 10;
    const int steps     = period_ms / step_ms;
    const float two_pi  = 6.28318530717958647692f;

    ESP_LOGI(TAG, "Breathing light started: period=%d ms, step=%d ms",
             period_ms, step_ms);

    while (1) {
        for (int i = 0; i < steps; i++) {
            /* phase sweeps 0 -> 1 across the full cycle */
            float phase = (float)i / (float)steps;
            /* cosine curve: 0 at phase=0, 1 at phase=0.5, 0 at phase=1.
               Looks like a smooth breath rather than a linear ramp. */
            float brightness = (1.0f - cosf(phase * two_pi)) * 0.5f;
            /* Gamma correction - human eyes are roughly logarithmic, so a raw
               0..255 ramp looks like the LED stays dim too long then jumps
               to bright. Raising to 2.2 makes the perceptual change linear. */
            brightness = powf(brightness, 2.2f);

            uint8_t level = (uint8_t)(brightness * 255.0f);
            set_led_brightness(level);

            vTaskDelay(step_ms / portTICK_PERIOD_MS);
        }
    }
}

最后的效果如下:

blink.gif




关键词: ESP32S31     blink    

共1条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]