news 2026/7/12 13:27:22

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/7/1 20:16:15

如何一键将B站缓存视频转为通用MP4格式:m4s-converter完整指南

如何一键将B站缓存视频转为通用MP4格式&#xff1a;m4s-converter完整指南 【免费下载链接】m4s-converter 将bilibili缓存的m4s转成mp4(读PC端缓存目录) 项目地址: https://gitcode.com/gh_mirrors/m4/m4s-converter 还在为B站下载的m4s格式视频无法在其他播放器打开而…

作者头像 李华
网站建设 2026/7/8 13:26:52

搜索功能支持按文件名或识别内容查找,快速定位目标记录

搜索功能支持按文件名或识别内容查找&#xff0c;快速定位目标记录 在语音数据爆炸式增长的当下&#xff0c;我们每天都在生成会议录音、客户对话、课堂讲解等大量音频内容。尽管语音识别技术已经能将这些声音高效转为文字&#xff0c;但真正困扰用户的&#xff0c;往往不是“…

作者头像 李华
网站建设 2026/7/10 7:02:55

I2C总线仲裁机制与冲突处理深度剖析

I2C总线仲裁机制深度解析&#xff1a;从原理到实战的无冲突通信设计 在嵌入式系统中&#xff0c;当多个主控芯片试图“抢着说话”时&#xff0c;如何避免总线变成一场混乱的争吵&#xff1f;答案就藏在IC协议那看似简单的两根线上——它不仅支持多主架构&#xff0c;还自带一套…

作者头像 李华
网站建设 2026/7/8 14:43:58

岛屿规划的3个关键突破:从新手到专家的进阶指南

还在为岛屿设计无从下手而烦恼吗&#xff1f;地形复杂、布局混乱、建筑位置难以抉择&#xff0c;这些问题Happy Island Designer都能帮你轻松解决。这款专业的岛屿规划设计工具&#xff0c;让每个玩家都能成为自己的岛屿设计师&#xff0c;轻松实现从概念到现实的完美转化。&am…

作者头像 李华
网站建设 2026/7/8 14:43:10

Python自动化工具实现网易云音乐高效批量下载

Python自动化工具实现网易云音乐高效批量下载 【免费下载链接】netease-cloud-music-dl Netease cloud music song downloader, with full ID3 metadata, eg: front cover image, artist name, album name, song title and so on. 项目地址: https://gitcode.com/gh_mirrors/…

作者头像 李华
网站建设 2026/7/7 22:54:22

Mac用户也能跑Fun-ASR!MPS设备支持Apple Silicon GPU加速

Mac用户也能跑Fun-ASR&#xff01;MPS设备支持Apple Silicon GPU加速 在远程办公、在线教育和内容创作日益普及的今天&#xff0c;语音识别已经从“锦上添花”变成了生产力工具中的刚需。无论是会议纪要自动生成&#xff0c;还是播客字幕快速输出&#xff0c;人们越来越希望语音…

作者头像 李华