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工程

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章