Wemos D1mini(ESP8266)基础实验------按钮控制板载等亮灭

wemos D1mini(ESP8266)基础实验------按钮控制板载等亮灭

实验材料

材料数量
Wemos D1 mini1块
按钮1
LED1块(可选,利用板载灯也可以)
杜邦线若干

硬件连接图

在这里插入图片描述

源代码

/*
  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);
  }
}
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值