当前位置: 首页> 房产> 政策 > 【ESP32 IDF】WS2812B灯驱动

【ESP32 IDF】WS2812B灯驱动

时间:2025/7/11 1:20:56来源:https://blog.csdn.net/EAyayaya/article/details/141861796 浏览次数:0次

WS2812B灯驱动

  • 1. 简单描述
  • 2. 驱动过程
  • 3.主函数添加驱动程序

1. 简单描述

  1. 开发环境为 IDF5.2.2
  2. 采用乐鑫官方组件库
  • 组件库地址 :
    https://components.espressif.com/components/espressif/led_strip/versions/2.5.5

2. 驱动过程

  • 复制led_strip组件命令

在这里插入图片描述

  • 在自己项目里面添加led_strip组件

在这里插入图片描述

  • 编译生成managed_components 和 espressif__led_strip

在这里插入图片描述

3.主函数添加驱动程序

/** SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD** SPDX-License-Identifier: Unlicense OR CC0-1.0*/
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "led_strip.h"
#include "esp_log.h"
#include "esp_err.h"// GPIO assignment
#define LED_STRIP_BLINK_GPIO  GPIO_NUM_48
// Numbers of the LED in the strip
#define LED_STRIP_LED_NUMBERS 24static const char *TAG = "example";led_strip_handle_t configure_led(void)
{// LED strip general initialization, according to your led board designled_strip_config_t strip_config = {.strip_gpio_num = LED_STRIP_BLINK_GPIO,   // The GPIO that connected to the LED strip's data line.max_leds = LED_STRIP_LED_NUMBERS,        // The number of LEDs in the strip,.led_pixel_format = LED_PIXEL_FORMAT_GRB, // Pixel format of your LED strip.led_model = LED_MODEL_WS2812,            // LED strip model.flags.invert_out = false,                // whether to invert the output signal};// LED strip backend configuration: SPIled_strip_spi_config_t spi_config = {.clk_src = SPI_CLK_SRC_DEFAULT, // different clock source can lead to different power consumption.flags.with_dma = true,         // Using DMA can improve performance and help drive more LEDs.spi_bus = SPI2_HOST,           // SPI bus ID};// LED Strip object handleled_strip_handle_t led_strip;ESP_ERROR_CHECK(led_strip_new_spi_device(&strip_config, &spi_config, &led_strip));ESP_LOGI(TAG, "Created LED strip object with SPI backend");return led_strip;
}void app_main(void)
{led_strip_handle_t led_strip = configure_led();bool led_on_off = false;ESP_LOGI(TAG, "Start blinking LED strip");while (1) {if (led_on_off) {/* Set the LED pixel using RGB from 0 (0%) to 255 (100%) for each color */for (int i = 0; i < LED_STRIP_LED_NUMBERS; i++) {ESP_ERROR_CHECK(led_strip_set_pixel(led_strip, i, 255, 0, 0));}/* Refresh the strip to send data */ESP_ERROR_CHECK(led_strip_refresh(led_strip));ESP_LOGI(TAG, "LED ON!");} else {/* Set all LED off to clear all pixels */ESP_ERROR_CHECK(led_strip_clear(led_strip));ESP_LOGI(TAG, "LED OFF!");}led_on_off = !led_on_off;vTaskDelay(pdMS_TO_TICKS(500));}
}
关键字:【ESP32 IDF】WS2812B灯驱动

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: