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

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