Course Content
📘 MODULE 11 – Edge Avoiding Robot
📦 MODULE 12 – Smart Multi-Function Robot (Mega Project)
Arduino Hands-On Programming and Robotics Course

📖 Lesson 4.3 – Obstacle Detection Code

🎯 Learning Objectives

After completing this lesson, students will be able to:

✅ Read obstacle detection signals from an IR sensor

✅ Understand obstacle detection logic

✅ Use conditional statements with sensors

✅ Control an LED based on obstacle detection

✅ Understand real-time decision making in Arduino

✅ Build the foundation for obstacle avoiding robots


1. Introduction

In the previous lesson, we connected the IR sensor to Arduino UNO and displayed sensor status on the Serial Monitor.

Now we will take the next step.

Instead of only displaying messages, Arduino will perform an action whenever an obstacle is detected.

Our goal:

Obstacle Detected

LED ON


No Obstacle

LED OFF

This is the basic principle behind:

  • Obstacle Avoiding Robots
  • Smart Dustbins
  • Automatic Doors
  • Automatic Water Dispensers

2. Project Overview

The system works as:

 
Obstacle

IR Sensor

Arduino UNO

Decision Making

LED ON / OFF
 

3. Components Required

Component Quantity
Arduino UNO 1
IR Obstacle Sensor Module 1
LED 1
220Ω Resistor 1
Breadboard 1
Jumper Wires As Required
USB Cable 1

4. Circuit Connections

IR Sensor Module

IR Sensor Arduino UNO
VCC 5V
GND GND
OUT D2

LED Connection

LED Arduino UNO
Anode (+) D13 through 220Ω resistor
Cathode (-) GND

Circuit Diagram

 
IR Sensor

VCC -----> 5V
GND -----> GND
OUT -----> D2

LED

D13 -----> Resistor -----> LED -----> GND
 

5. Understanding Detection Logic

Most IR sensor modules work as:

Condition Output
No Obstacle HIGH
Obstacle Detected LOW

Arduino continuously checks the sensor output.


Decision Logic

If:

 
LOW
 

Obstacle Present

LED ON


If:

 
HIGH
 

No Obstacle

LED OFF


6. Algorithm

Step 1

Read IR Sensor Output

Step 2

Is Obstacle Detected?

YES

Turn LED ON

NO

Turn LED OFF

Repeat Forever


7. Arduino Program

 
int sensorPin = 2;
int ledPin = 13;

void setup()
{
  pinMode(sensorPin, INPUT);
  
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  if(digitalRead(sensorPin) == LOW)
  {
    digitalWrite(ledPin, HIGH);
  }
  else
  {
    digitalWrite(ledPin, LOW);
  }
}

8. Program Explanation

Sensor Pin

 
int sensorPin = 2;
 

Stores the IR sensor pin number.


LED Pin

 
int ledPin = 13;
 

Stores the LED pin number.


Configure Sensor

 
pinMode(sensorPin, INPUT);
 

Sets D2 as input.


Configure LED

 
pinMode(ledPin, OUTPUT);
 

Sets D13 as output.


Detect Obstacle

 
digitalRead(sensorPin)
 

Reads sensor status.


LED ON

 
digitalWrite(ledPin, HIGH);
 

Obstacle detected.


LED OFF

 
digitalWrite(ledPin, LOW);
 

No obstacle detected.


9. Testing the Program

Test 1 – No Obstacle

Keep sensor facing open space.

Expected:

 
LED OFF
 

Test 2 – Place Hand in Front

Bring your hand within sensor range.

Expected:

 
LED ON
 

Test 3 – Remove Hand

Move hand away.

Expected:

 
LED OFF
 

10. Enhanced Program with Serial Monitor

 
int sensorPin = 2;
int ledPin = 13;

void setup()
{
pinMode(sensorPin, INPUT);

pinMode(ledPin, OUTPUT);

Serial.begin(9600);
}

void loop()
{
if(digitalRead(sensorPin) == LOW)
{
digitalWrite(ledPin, HIGH);

Serial.println("Obstacle Detected");
}
else
{
digitalWrite(ledPin, LOW);

Serial.println("No Obstacle");
}

delay(200);
}
 

Expected Output

Object Present

 
Obstacle Detected
 

Object Removed

 
No Obstacle
 

11. Understanding Real-Time Detection

Arduino continuously runs:

 
loop()
 

thousands of times every second.

This allows the sensor to detect obstacles almost instantly.


12. Testing Different Objects

Try detecting:

Hand

Mobile Phone

Notebook

White Paper

Water Bottle

Observe sensor response.


13. Practical Applications

Obstacle Avoiding Robot

Obstacle Detected

Robot Turns


Smart Dustbin

Hand Detected

Lid Opens


Automatic Water Dispenser

Hand Detected

Pump ON


Conveyor Belt

Object Detected

Counting System


Automatic Door

Person Detected

Door Opens


14. Common Beginner Mistakes

Mistake 1

Using analogRead()

Wrong:

 
analogRead(sensorPin);
 

Correct:

 
digitalRead(sensorPin);
 

Mistake 2

Wrong Sensor Pin

Verify:

 
OUT → D2
 

Mistake 3

LED Wiring Incorrect

Check polarity.


Mistake 4

Ignoring Sensor Calibration

Adjust potentiometer properly.


15. Troubleshooting

LED Never Turns ON

Check:

  • Sensor Wiring
  • Detection Distance
  • Potentiometer Adjustment

LED Always ON

Check:

  • Sensor Output Logic
  • Potentiometer Setting

No Detection

Check:

  • Power Supply
  • Sensor Alignment
  • Object Distance

Unstable Operation

Check:

  • Loose Wires
  • Strong Sunlight

16. Mini Challenge

Modify the program so that:

Obstacle Detected

LED Blinks

instead of remaining ON.

Hint:

Use:

 
digitalWrite();
delay();
 

📊 Summary

In this lesson, we learned:

✅ Obstacle Detection Logic

✅ IR Sensor Programming

✅ LED Control

✅ Conditional Statements

✅ Real-Time Detection

This lesson demonstrates how Arduino can make decisions and respond automatically when an obstacle is detected.


📖 Key Terms

Obstacle Detection

Detecting nearby objects.

Digital Input

HIGH or LOW signal received by Arduino.

Conditional Statement

Decision-making code using if-else.

Automation

Automatic response without human control.

Real-Time System

System that reacts immediately to changes.


🎯 Quiz

1. Which function reads the IR sensor?

A. analogRead()

B. digitalRead() ✅

C. digitalWrite()

D. Serial.print()


2. What happens when an obstacle is detected?

A. LED OFF

B. LED ON ✅

C. Arduino OFF

D. Sensor OFF


3. Which Arduino pin is connected to the IR sensor output?

A. A0

B. D13

C. D2 ✅

D. D8


4. Which statement is used for decision making?

A. for

B. while

C. if-else ✅

D. setup


5. Which application uses obstacle detection?

A. Smart Dustbin ✅

B. Calculator

C. Keyboard

D. Printer


🏠 Assignment

Task 1

Build the obstacle detection circuit and test it.

Task 2

Display obstacle status on Serial Monitor.

Task 3

Test the sensor with five different objects.

Task 4

Modify the program to blink the LED when an obstacle is detected.

Task 5

Explain how this logic can be used in an obstacle avoiding robot.

Scroll to Top