实验材料
材料 | 数量 |
---|---|
Wemos D1 mini | 1块 |
按钮 | 1 |
LED | 1块(可选,利用板载灯也可以) |
杜邦线 | 若干 |
硬件连接图
源代码
/*
Button
Turns on and off a light emitting diode(LED) connected to digital pin 13,
when pressing a pushbutton attached to pin 2.
The circuit:
- LED attached from pin 13 to ground
- pushbutton attached to pin 2 from +5V
- 10K resistor attached to pin 2 from ground
- Note: on most Arduinos there is already an LED on the board
attached to pin 13.
created 2005
by DojoDave <http://www.0j0.org>
modified 30 Aug 2011
by Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/Button
*/
// constants won't change. They're used here to set pin numbers:
const int buttonPin = D4; // 按钮信息读取引脚D4
const int ledPin = LED_BUILTIN; // 选择板载灯
// variables will change:
int buttonState = 0; // 按钮开关状态标志位
void setup() {
// 板载灯引脚设置为输出
pinMode(ledPin, OUTPUT);
// 按钮引脚设置为输出
pinMode(buttonPin, INPUT);
}
void loop() {
// 读取当前按钮状态标志位
buttonState = digitalRead(buttonPin);
// 判断按钮是否按下. 如果按下,按钮应为HIGH(取决于硬件电路的连接方式)
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}