NodeMCU-32S-开发学习-LED控制

NodeMCU-32S-开发学习-LED控制

前言

ESP32的开发环境搭建完成了,运行一个最简答的hello_world成功之后,下面就可以测试和学习各种demo了。本文就介绍一个驱动LED的demo。

NodeMCU-32S原理图

在这里插入图片描述
从原理出中能够看出来,NodeMCU-32S 上面包含有两个LED,其中红色LED用于电源指示,蓝色LED连接到了GPIO2上面,由GPIO2驱动,下面就来说明一下如何用ESP-IDF中的demo驱动GPIO2上的LED。

ESP-IDF提供的示例

ESP-IDF中提供了驱动LED的示例,如下的blink工程。

root@qiao-virtual-machine:/esp32/sources/esp-idf/examples/get-started# ls -alh
total 20K
drwxr-xr-x  4 root root 4.0K 12月  5 14:42 .
drwxr-xr-x 12 root root 4.0K 12月  5 14:42 ..
drwxr-xr-x  3 root root 4.0K 12月  4 12:46 blink
drwxr-xr-x  3 root root 4.0K 12月  5 14:42 hello_world
-rw-r--r--  1 root root  202 12月  5 14:42 README.md

需要说明的是 blink/main/blink.c 提供的源文件中定义的宏 BLINK_GPIO 并没有指定哪一个 GPIO,需要我们自己设定,根据上面的原理图,我们这里设定为 GPIO2 。将下面这个宏加入到 blink.c 源码中 。

#define CONFIG_BLINK_GPIO 2

源码

/* Blink Example

   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "sdkconfig.h"

/* Can run 'make menuconfig' to choose the GPIO to blink,
   or you can edit the following line and set a number here.
*/
#define CONFIG_BLINK_GPIO 2                                     //add GPIO PIN
#define BLINK_GPIO CONFIG_BLINK_GPIO

void blink_task(void *pvParameter)
{
/* Configure the IOMUX register for pad BLINK_GPIO (some pads are
   muxed to GPIO on reset already, but some default to other
   functions and need to be switched to GPIO. Consult the
   Technical Reference for a list of pads and their default
   functions.)
*/
gpio_pad_select_gpio(BLINK_GPIO);
/* Set the GPIO as a push/pull output */
gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
while(1) {
/* Blink off (output low) */
gpio_set_level(BLINK_GPIO, 0);
vTaskDelay(1000 / portTICK_PERIOD_MS);
/* Blink on (output high) */
gpio_set_level(BLINK_GPIO, 1);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}

void app_main()
{
xTaskCreate(&blink_task, "blink_task", configMINIMAL_STACK_SIZE, NULL, 5, NULL);
}

编译调试

进入到 blink 工程中,执行 make menuconfig 配置完工程,保存退出,然后编译

  cd /esp32/demos/blink/
  make menuconfig
  make flash monitor

烧写完到NodeMCU-32S 中,观察现象,发现板载的蓝色LED开始以2s的周期闪烁了。

参考链接

NodeMCU-32S 核心开发板Wiki

ESP32S-github工程

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飘雪冰峰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值