/* * ============================================================ * IR Obstacle Sensor (ADIY with Potentiometer) — Sample Sketch * Tishvi Electronics | guide.tishvi.com * ============================================================ * Wiring: * IR Sensor VCC → Arduino 5V * IR Sensor GND → Arduino GND * IR Sensor OUT → Arduino Pin 7 * * What this sketch does: * 1. Reads the digital OUT pin of the IR sensor * 2. OUT = LOW → Object detected (obstacle in range) * 3. OUT = HIGH → No object detected (path is clear) * 4. Prints status to Serial Monitor * 5. Blinks the built-in LED (pin 13) when an object is detected * * Detection range: 2–10 cm (adjust the blue potentiometer on the sensor) * No external libraries needed. * ============================================================ */ // ── Pin Definitions ────────────────────────────────────────── const int IR_OUT_PIN = 7; // IR sensor OUT → Arduino digital pin 7 const int LED_PIN = 13; // Built-in LED for visual feedback // ── Variables ──────────────────────────────────────────────── bool lastState = HIGH; // Track previous sensor state to detect changes void setup() { pinMode(IR_OUT_PIN, INPUT); pinMode(LED_PIN, OUTPUT); Serial.begin(9600); Serial.println("IR Obstacle Sensor — Tishvi Sample Sketch"); Serial.println("------------------------------------------"); Serial.println("Open Serial Monitor at 9600 baud."); Serial.println("Adjust the blue potentiometer to set detection distance."); Serial.println(); } void loop() { // ── Read sensor ────────────────────────────────────────── // LM393 comparator output: LOW = obstacle detected, HIGH = clear bool sensorValue = digitalRead(IR_OUT_PIN); // ── React to state ────────────────────────────────────── if (sensorValue == LOW) { // Object detected digitalWrite(LED_PIN, HIGH); // Turn on built-in LED // Only print when state changes (avoids spamming Serial Monitor) if (lastState != LOW) { Serial.println("🔴 OBSTACLE DETECTED — OUT pin is LOW"); } } else { // No object in range digitalWrite(LED_PIN, LOW); // Turn off built-in LED if (lastState != HIGH) { Serial.println("🟢 Path clear — OUT pin is HIGH"); } } lastState = sensorValue; // Remember current state for next loop delay(50); // 50 ms polling rate — fast enough for most applications }