实验二:Blink、按键实验

实验介绍

本实验中,首先完成让 ESP8266 芯片右上角的蓝色LED灯(对应ESP8266上的GPIO2引脚)以1s为时间间隔进行闪烁的任务,再实现按键控制LED灯亮灭的功能,其中,按键对应引脚为LinkNode D1上的数字口D2,当按键按下时灯亮。

实验目的

  1. 掌握LinkNode D1开发板的硬件接口及具体应用

  2. 熟悉Arduino IDE的开发环境及Arduino语言风格,并学会简单编程

  3. 提高动手实践能力

软硬件清单

软件:

  • Arduino IDE

硬件:

  • LinkNode D1 x 1

  • Button x 1

  • 杜邦线若干

实验步骤

  1. 将LinkNode D1与电脑相连。

  2. 打开Arduino IDE,检查开发板的配置,注意开发板和端口选择,开发板选择"WeMos D1"。

  3. 新建一个文件,命名为Blink.ino,编写代码,编译并上传到开发板(上传时ESP8266芯片上LED灯快闪),查看LED灯是否闪烁?

  4. 连接一个按键,具体接口如下:

    • Button模块的 GND 接 LinkNode D1的 GND
    • Button模块的 VCC 接 LinkNode D1的 5V
    • Button模块的 SIG 接 LinkNode D1的 D2

硬件连接电路图如图所示:

图1.2.1 按键实验硬件连接图
图1.2.1 按键实验硬件连接图

重新新建一个文件,命名为Button.ino,编写代码,编译并上传,按一按按键,查看LED灯的亮灭情况。

实验源码

Blink.ino

void setup() {
  // initialize digital pin GPIO2/D9 as an output.
   pinMode(BUILTIN_LED, OUTPUT);
 }

 // the loop function runs over and over again forever
void loop() {
  digitalWrite(BUILTIN_LED, HIGH);  // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  digitalWrite(BUILTIN_LED, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);              // wait for a second
}

Button.ino

const int buttonPin = D2;     // the number of the pushbutton pin
const int ledPin =  2;      // the number of the LED pin
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH)
  {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  } 
  else 
  {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}

参考文献

[1]Blink:http://www.linksprite.com/wiki/index.php5?title=LinkNode_D1

[2]Button:http://linksprite.com/wiki/index.php5?title=Button_Module

results matching ""

    No results matching ""