【问题标题】:Digital read pin in ArduinoArduino中的数字读取引脚
【发布时间】:2015-10-07 17:47:30
【问题描述】:

我使用下面的代码来数字读取引脚输入,看看它是高还是低。

问题是 Arduino 给出了错误的值。即使引脚始终处于低电平,它也会给出一些时间 0 或一些时间高。

虽然引脚为高,但它也给出低或 0 作为答案,有没有人可以帮助我解决这个问题?

// digital pin 2 has a pushbutton attached to it. Give it a name:
int pushButton = 2;

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // make the pushbutton's pin an input:
  pinMode(pushButton, INPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  delay(1000);
  // read the input pin:
  int buttonState = digitalRead(pushButton);
  // print out the state of the button:
  Serial.println(buttonState);
  delay(1000); // delay in between reads for stability
}

【问题讨论】:

    标签: arduino arduino-uno arduino-ide


    【解决方案1】:

    您必须使用下拉 (3) 或上拉 (4) 电阻:

    像这样使用内部上拉电阻:

    pinMode(pin, INPUT);           // set pin to input
    digitalWrite(pin, HIGH);       // activate internal pull-up resistor
    

    也应该这样做。

    【讨论】:

    • 你好!我想使用下拉电阻,但事实是我必须使用外部 10 k 电阻或 digitalWrite(pin, LOW) 对我有用;?请尽快回复。 digitalWrite(pin, LOW) 没有我想象的那么好。
    • 抱歉 digitalWrite(pin, LOW) 不会像你想要的那样工作。您的控制器仅支持上拉电阻。因此,对于下拉,您必须添加一个外部电阻器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多