Вернуться на страницу библиотеки модулей

// KY-032 IR Obstacle Avoidance Sensor
// Lights the built-in LED and prints to the Serial Monitor when an obstacle
// is detected. The sensor output is active-LOW: LOW = obstacle, HIGH = clear.

const int sensorPin = 3;            // KY-032 S (signal) pin
const int ledPin    = LED_BUILTIN;  // on-board LED (pin 13 on an Uno)
// const int enablePin = 2;         // KY-032 EN pin (remove module jumper to use)

int lastState = HIGH;               // remember the previous reading

void setup() {
  Serial.begin(9600);
  pinMode(sensorPin, INPUT);
  pinMode(ledPin, OUTPUT);
  // pinMode(enablePin, OUTPUT);
  // digitalWrite(enablePin, HIGH);  // HIGH = sensor enabled
}

void loop() {
  int state = digitalRead(sensorPin);   // LOW = obstacle, HIGH = clear

  if (state == LOW) {
    digitalWrite(ledPin, HIGH);          // obstacle: LED on
  } else {
    digitalWrite(ledPin, LOW);           // clear: LED off
  }

  // Print only when the reading changes, so the Serial Monitor stays readable
  if (state != lastState) {
    Serial.println(state == LOW ? "Obstacle detected" : "Path clear");
    lastState = state;
  }
}

Рекомендуемые ссылки:
Arduino IR Obstacle Avoidance Sensor Module KY-032 (HW-488) — Wiring & Code
KY-032 Obstacle detector – SensorKit
Using the Obstacle Avoidance Sensor KY-032 with Arduino – Phipps Electronics
Ky-032 IR obstacle sensor module
IR Sensor Obstacle Avoidance Keyes KY 032

Вернуться на страницу библиотеки модулей