Build a Raspberry Pi Obstacle‑Avoiding Robot – A Beginner’s Guide
If you’re new to Raspberry Pi and want a hands‑on project that blends software and hardware, this guide walks you through building a Python‑controlled robot that detects and avoids obstacles using infrared sensors.
Obstacle‑avoiding robots are popular for learning robotics and are surprisingly straightforward to assemble. By following this tutorial, you’ll add reliable object‑avoidance to your robot or simply explore Python and the Raspberry Pi’s GPIO ecosystem.
What you’ll need
- Raspberry Pi B, B+, 2, 3 or 4 plus basic peripherals (SD card, keyboard, mouse, power supply).
- IR distance‑sensor modules (e.g., the popular 4‑channel IR sensor).
- Geared DC motors with suitable torque for your chassis.
- L293D motor‑driver board.
- Robot chassis and wheels.
- Caster wheel for stability.
- Breadboard and double‑sided tape.
- Male‑to‑male / female‑to‑male jumper wires.
- 9 V battery and appropriate connectors.
- Push button and 220 Ω resistor (for power‑on control).
How it works
The robot’s core functionality relies on two IR sensors that detect objects within a 5‑6 cm range. When an object is present, the sensor’s output pin goes LOW (0 V). When no object is detected, it stays HIGH (5 V). The Raspberry Pi reads these digital signals via its GPIO pins, and the attached Python script directs the motors accordingly.
IR (infrared) modules consist of an IR LED transmitter and a photodiode receiver. An object reflects a portion of the emitted IR light back to the receiver, causing a LOW signal that signals proximity. These signals are routed to GPIO inputs on the Pi.
When the robot starts—triggered by pressing the push button—the Pi drives the DC motors forward through the L293D driver. If an IR sensor detects an obstacle, the script first reverses the motors briefly, then executes a turn to clear the obstacle.
Step 1: Wiring the IR Sensors
Power the sensors with 5 V and GND from the Raspberry Pi, and connect the output pins to GPIO pins 3 and 16 (using the official GPIO pinout diagram). The board’s GPIO.BOARD mode numbers pins as they appear on the header.
Save the following script as irtest.py:
import RPi.GPIO as GPIO
import time
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
# Right sensor (GPIO 3)
GPIO.setup(3, GPIO.IN)
# Left sensor (GPIO 16) with pull‑up
GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_UP)
while True:
right = GPIO.input(3)
left = GPIO.input(16)
if right == 0:
print "Obstacle detected on Right", right
time.sleep(0.1)
elif left == 0:
print "Obstacle detected on Left", left
time.sleep(0.1)
Run it with sudo python irtest.py. When you block a sensor with your hand, the console will output a message indicating which side detected the obstacle.
Step 2: Connecting the Motors via the L293D Driver
Power the L293D with the 9 V battery (positive to the + pin, negative to the – pin). Connect the board’s ground to the Raspberry Pi’s GND. Wire the motor outputs to the driver’s output pins and the control pins to additional GPIOs (consult the L293D datasheet for pin definitions). With the motors wired, the script can command forward, reverse, and turn movements.
Once the hardware is assembled, power up the Raspberry Pi, press the push button, and watch the robot navigate while avoiding obstacles.
For additional learning resources, download our free eBook on Raspberry Pi and Arduino to deepen your skills.
Manufacturing process
- Build a DIY Infrared Motion Sensor for Raspberry Pi – Step‑by‑Step Guide
- Build a Raspberry Pi Geiger Counter – Open‑Hardware Radiation Sensor Tutorial
- Top Accessories to Unlock Your Raspberry Pi's Full Potential
- Raspberry Pi Light Sensor Tutorial – Wire an LDR and Read Light Levels with Python
- Build a Raspberry Pi Home Temperature Monitor with MCP9808, InfluxDB & Grafana
- Master Raspberry Pi GPIO: Interfacing a PIR Motion Sensor on B+/Model 2
- Build a Raspberry Pi Webcam Robot – Step‑by‑Step Video Streaming Guide
- Master PowerShell: Beginner's Guide to Powerful Scripting
- Master C Programming: Comprehensive PDF Tutorial for Beginners
- Mastering Groovy: A Beginner’s Guide to Scripting on the Java Platform