如何从 Arduino 上的红外传感器接收高电平或低电平信号?

How do I receive a HIGH or LOW signal from an IR sensor on an Arduino?

我正在尝试将红外传感器与我的 Arduino Uno 一起使用,并且只需要高信号或低信号而不解码,使任何红外信号将状态变为 1 或 0。还有一个运动传感器,但该代码已被删除。

int ledPin = 13; // choose the pin for the LED
int inputPin = 2; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
int relayPin = 4; //PIN FOR RELAY OPERATION
int irPin = 7; //IR Sensor pin
int lightState = 0;
int irVal = 0;

void setup() {
  pinMode(ledPin, OUTPUT); // declare LED as output
  pinMode(inputPin, INPUT); // declare sensor as input
  pinMode(relayPin, OUTPUT);
  pinMode(irPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  irVal = digitalRead(irPin);
  if (irVal == HIGH) {
    lightState = 1;
    Serial.println("IR received");
    while(irVal == HIGH) {
      irVal = digitalRead(irPin);
      if(irVal == HIGH) {
        irVal = LOW;
      } else {
        irVal = HIGH;
    }
  }
}

您是说输入不正常吗?也许在设置循环中尝试 INPUT_PULLUP 而不是 INPUT。

例如: pinMode(inputPin, INPUT_PULLUP); 您可以在此处找到有关此原理的信息: https://www.arduino.cc/en/Tutorial/InputPullupSerial