IR Obstacle Sensor Raspberry Pi

 ðŸŸ¦ 1. Introduction

An IR (Infrared) obstacle sensor is used to detect objects without physical contact. It works by emitting infrared light and detecting reflections from nearby objects. This sensor is widely used in robotics and automation projects.


🟦 2. Components

  • Raspberry Pi
  • IR Obstacle Sensor Module
  • LED
  • 220Ω Resistor
  • Breadboard
  • Jumper Wires

🟦 3. Circuit and Connections

Raspbery Pi
Raspberry Pi Pinout Diagram

Pinout Diagram IR Sensor Module
Pinout Diagram IR Sensor Module

🔌 Connections:

  • VCC → 3.3V (Pin 1)
  • GND → GND (Pin 6)
  • OUT → GPIO17 (Pin 11)
  • LED + → GPIO18 (Pin 12)
  • LED – → GND (via 220Ω resistor)

🟦 4. Detailed Step By Step Circuit Working

  1. The IR sensor continuously emits infrared light.
  2. When an object comes in front, IR light reflects back.
  3. The receiver detects this reflection.
  4. The sensor output changes (LOW or HIGH).
  5. Raspberry Pi reads this signal via GPIO17.
  6. When obstacle is detected → LED turns ON.
  7. When no obstacle → LED remains OFF.

🟦 5. Libraries to be Included

import RPi.GPIO as GPIO
import time

🟦 6. Code

import RPi.GPIO as GPIO
import time

sensor = 17
led = 18

GPIO.setmode(GPIO.BCM)
GPIO.setup(sensor, GPIO.IN)
GPIO.setup(led, GPIO.OUT)

while True:
if GPIO.input(sensor) == 0: # obstacle detected
GPIO.output(led, True)
print("Obstacle Detected")
else:
GPIO.output(led, False)
time.sleep(0.1)

🟦 7. Detailed Step By Step Code Working

  1. import RPi.GPIO as GPIO → Import GPIO library
  2. import time → Import delay function
  3. sensor = 17 → Define sensor pin
  4. led = 18 → Define LED pin
  5. GPIO.setmode(GPIO.BCM) → Use GPIO numbering
  6. GPIO.setup(sensor, GPIO.IN) → Sensor as input
  7. GPIO.setup(led, GPIO.OUT) → LED as output
  8. GPIO.input(sensor) → Read sensor value
  9. If value = 0 → obstacle detected → LED ON
  10. Else → LED OFF
  11. time.sleep(0.1) → Small delay

🟦 8. Tips

  • Adjust sensor sensitivity using onboard potentiometer
  • Use proper power supply (3.3V recommended)
  • Avoid direct sunlight interference
  • Keep sensor clean for better accuracy

🟦 9. Uses

  • Obstacle avoidance robots
  • Line follower robots
  • Smart parking systems
  • Security detection systems

🟦 10. Conclusion

The IR obstacle sensor is a simple and effective way to detect nearby objects. This project helps beginners understand sensor interfacing and basic automation using Raspberry Pi.

à´¤ുà´Ÿà´•്à´•à´•്à´•ാർക്à´•ാà´¯ുà´³്à´³ ഇലക്à´Ÿ്à´°ോà´£ിà´•്à´¸് ലളിതമാà´¯ി പഠിà´•്à´•ാം.

Empowering students in Kerala with hands-on technical skills.