news 2026/4/16 14:18:25

ESP32-S3(3) : 点亮WS2812 RGB

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
ESP32-S3(3) : 点亮WS2812 RGB

1.说明

开发框架 : ESP-IDF, 版本: 5.5.0

开发版图片(图中左边typec接口上面一点的白色小方块就是WS2812 RGB) :

2.代码

main/CMakeLists.txt

# 主程序组件 idf_component_register(SRCS "main.c" INCLUDE_DIRS ".")

CMakeLists.txt

# The following five lines of boilerplate have to be in your project's # CMakeLists in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.16) include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(0_kai_fa_ban)

main/main.c

rmt.h过时了, 但是不影响功能

#include <stdio.h> #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "driver/rmt.h" #include "esp_log.h" static const char *TAG = "WS2812"; #define WS2812_GPIO_PIN GPIO_NUM_48 #define LED_NUM 1 #define RMT_TX_CHANNEL RMT_CHANNEL_0 // WS2812 timing parameters (in nanoseconds) #define T0H 350 // 0 bit high time #define T0L 900 // 0 bit low time #define T1H 900 // 1 bit high time #define T1L 350 // 1 bit low time #define RESET 50000 // Reset time // Convert time to RMT ticks (80MHz / 4 = 20MHz, 1 tick = 50ns) #define NS_TO_TICKS(ns) ((ns) / 50) void ws2812_init(void) { rmt_config_t config = RMT_DEFAULT_CONFIG_TX(WS2812_GPIO_PIN, RMT_TX_CHANNEL); config.clk_div = 4; // 80MHz / 4 = 20MHz config.mem_block_num = 1; config.tx_config.loop_en = false; config.tx_config.carrier_en = false; config.tx_config.idle_output_en = true; config.tx_config.idle_level = RMT_IDLE_LEVEL_LOW; ESP_ERROR_CHECK(rmt_config(&config)); ESP_ERROR_CHECK(rmt_driver_install(config.channel, 0, 0)); ESP_LOGI(TAG, "WS2812 initialized on GPIO %d", WS2812_GPIO_PIN); } void set_led_color(uint8_t red, uint8_t green, uint8_t blue) { uint8_t color[3] = {green, red, blue}; // WS2812 uses GRB order rmt_item32_t items[24]; // 3 bytes * 8 bits // Convert each bit to RMT items for (int i = 0; i < 3; i++) { for (int j = 0; j < 8; j++) { int bit_index = (i * 8) + j; if (color[i] & (1 << (7 - j))) { // Bit 1 items[bit_index].level0 = 1; items[bit_index].duration0 = NS_TO_TICKS(T1H); items[bit_index].level1 = 0; items[bit_index].duration1 = NS_TO_TICKS(T1L); } else { // Bit 0 items[bit_index].level0 = 1; items[bit_index].duration0 = NS_TO_TICKS(T0H); items[bit_index].level1 = 0; items[bit_index].duration1 = NS_TO_TICKS(T0L); } } } // Send data ESP_ERROR_CHECK(rmt_write_items(RMT_TX_CHANNEL, items, 24, true)); // Reset vTaskDelay(pdMS_TO_TICKS(1)); } void app_main(void) { ESP_LOGI(TAG, "Starting WS2812 Demo"); ws2812_init(); vTaskDelay(pdMS_TO_TICKS(1000)); while (1) { ESP_LOGI(TAG, "Red"); set_led_color(255, 0, 0); vTaskDelay(pdMS_TO_TICKS(1000)); ESP_LOGI(TAG, "Green"); set_led_color(0, 255, 0); vTaskDelay(pdMS_TO_TICKS(1000)); ESP_LOGI(TAG, "Blue"); set_led_color(0, 0, 255); vTaskDelay(pdMS_TO_TICKS(1000)); ESP_LOGI(TAG, "White"); set_led_color(255, 255, 255); vTaskDelay(pdMS_TO_TICKS(1000)); ESP_LOGI(TAG, "Off"); set_led_color(0, 0, 0); vTaskDelay(pdMS_TO_TICKS(1000)); } }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/16 13:04:56

Windows 11右键菜单个性化改造:打造专属高效工作流

Windows 11右键菜单个性化改造&#xff1a;打造专属高效工作流 【免费下载链接】ContextMenuForWindows11 Add Custom Context Menu For Windows11 项目地址: https://gitcode.com/gh_mirrors/co/ContextMenuForWindows11 还在为Windows 11那层层嵌套的右键菜单感到困扰…

作者头像 李华
网站建设 2026/4/16 13:06:51

Betaflight飞控实战指南:三步调参法与性能优化完整方案

Betaflight飞控实战指南&#xff1a;三步调参法与性能优化完整方案 【免费下载链接】betaflight Open Source Flight Controller Firmware 项目地址: https://gitcode.com/gh_mirrors/be/betaflight 作为开源飞控系统的标杆&#xff0c;Betaflight项目为无人机爱好者提供…

作者头像 李华
网站建设 2026/4/16 12:57:31

DINOv2 Vision Transformer 部署配置终极指南

DINOv2 Vision Transformer 部署配置终极指南 【免费下载链接】dinov2 PyTorch code and models for the DINOv2 self-supervised learning method. 项目地址: https://gitcode.com/GitHub_Trending/di/dinov2 部署痛点分析与解决方案 在将DINOv2预训练模型应用于实际项…

作者头像 李华
网站建设 2026/4/8 17:57:12

模型状态提示帮助用户判断是否需要重新加载或重启服务

模型状态提示&#xff1a;让 AI 服务“会说话” 在本地部署大模型的实践中&#xff0c;你是否遇到过这样的场景&#xff1f; 刚启动语音识别系统&#xff0c;点击“开始识别”却毫无反应&#xff1b;连续处理几个长音频后&#xff0c;突然弹出 CUDA out of memory 错误&#xf…

作者头像 李华
网站建设 2026/4/16 12:39:34

Audio Slicer音频智能分割解决方案:高效处理语音录音的实战指南

Audio Slicer音频智能分割解决方案&#xff1a;高效处理语音录音的实战指南 【免费下载链接】audio-slicer 项目地址: https://gitcode.com/gh_mirrors/aud/audio-slicer 还在为手动剪辑音频文件而耗费大量时间吗&#xff1f;Audio Slicer作为一款基于静音检测的智能音…

作者头像 李华
网站建设 2026/4/16 12:42:20

Markdown目录TOC点击跳转语音提示

Markdown目录TOC点击跳转语音提示 在如今AI工具日益普及的背景下&#xff0c;语音识别系统早已不再局限于“能听懂人话”这么简单。真正的挑战在于——如何让这些强大的技术真正融入用户的使用习惯&#xff0c;尤其是当面对一份上千行的手册文档时&#xff0c;怎样才能既快又准…

作者头像 李华