【问题标题】:How to turn led ON when pushing the boot button on an esp32按下 esp32 上的启动按钮时如何打开 LED
【发布时间】:2017-11-06 00:42:31
【问题描述】:

当我按下 esp32 上的启动按钮时,我正试图打开 LED(引脚 2)!这是我的代码!知道为什么这不起作用吗?

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 0;     // the number of the pushbutton pin
const int ledPin =  2;      // the number of the LED pin

// variables will change:
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);
  }
}

【问题讨论】:

    标签: arduino esp32


    【解决方案1】:

    您的设计在多个方面存在致命缺陷,主要是在芯片启动时,i/o 引脚不受您的代码控制,一旦检测到该信号,您的程序就会停止运行。重新启动会恢复其默认上电状态。再加上按钮输入肯定由中断服务,您的轮询循环永远不会看到它变高。

    如果您希望 LED 在芯片启动时保持亮起,则需要另一个组件,例如 JFET。将一个 i/o 引脚连接到 JFET 的栅极,其源极和漏极引脚与 LED 的电源或接地引脚串联,然后在程序运行时将该 i/o 引脚设置为高电平,以偏置 JFET,因此保持源极和漏极之间的开路。一旦门变低,电路就会关闭,你的 LED 就会打开。当引导程序执行您的程序时,它再次将该引脚设置为高电平,LED 熄灭。作为奖励,您无需浪费任何计算轮询 i/o。赢:赢!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-22
      • 2014-01-27
      相关资源
      最近更新 更多