Creating an Obstacle-Avoiding Robot with Raspberry Pi 5: Step by Step Guide

Explore robotics with our beginner-friendly guide to building an obstacle-avoiding robot using Raspberry Pi 5. This comprehensive tutorial covers each step, from chassis assembly and motor wiring to programming in Python, enabling the robot to detect and avoid obstacles autonomously. Perfect for enthusiasts, this project introduces essential robotics skills using affordable components and hands-on learning.

 

The Raspberry Pi 5 opens up a world of possibilities in robotics, enabling makers to create complex and responsive robots on a budget. One of the most accessible projects for beginners in robotics is building an obstacle-avoiding robot. This project introduces essential concepts such as motor control, sensor integration, and basic programming logic, making it an excellent choice for anyone looking to start in robotics.

This guide will walk through each step of creating an obstacle-avoiding robot with the Raspberry Pi 5, detailing components, wiring, programming, testing, and troubleshooting. By the end of this tutorial, you’ll have a fully functional robot capable of navigating around objects autonomously.

Why Use the Raspberry Pi 5 for Robotics?

The Raspberry Pi 5 is a versatile and affordable microcomputer perfect for robotics applications. Its key features include:

  • Improved Processing Power: The Raspberry Pi 5 can handle real-time data from sensors and control motors simultaneously without slowing down.
  • GPIO Pins: The Pi’s GPIO (General Purpose Input/Output) pins enable easy integration with various electronic components, such as sensors and motors, which are essential in robotics.
  • Affordable and Compact: The Pi 5 is relatively inexpensive, making it ideal for experimenting with projects that involve potential trial and error.

 

Essential Components for Building the Obstacle-Avoiding Robot

  1. Raspberry Pi 5: Acts as the main controller of the robot.
  2. Motor Driver Board (L298N): Allows the Raspberry Pi to control the motors safely, as the Pi’s GPIO pins can’t directly handle the power required.
  3. DC Motors: These power the wheels, enabling the robot to move.
  4. Ultrasonic Sensor (HC-SR04): Detects obstacles by emitting and receiving sound waves, helping the robot navigate.
  5. Wheels and chassis: the robot’s body, where all components are mounted.
  6. Battery Pack: Provides power to the motors and the Raspberry Pi.
  7. Jumper Wires: Used to connect components to the GPIO pins on the Pi.

 

Step 1: Assembling the Chassis and Mounting Components

  1. Prepare the Chassis:
    • Position the chassis on a clean surface. Identify the front and back based on the location of wheels or slots for motors.
    • Secure the wheels onto the DC motors, ensuring they’re tightly fitted for smooth movement.
  2. Attach motors to the chassis:
    • Align each DC motor with the mounting slots or holes on the chassis.
    • Use screws or brackets to fasten each motor securely. Ensure the wheels face forward and allow for free rotation.
  3. Place the Battery Pack:
    • Position the battery pack at the center or back of the chassis to balance the weight.
    • Secure it with adhesive tape or Velcro strips to prevent movement while the robot is in action.
  4. Mount the Raspberry Pi and Motor Driver Board:
    • Place the Raspberry Pi 5 on the chassis, using spacers or screws to secure it.
    • Mount the motor driver board near the motors, ensuring the output pins face the motors for easy wiring.

 

Step 2: Wiring the Motors to the Motor Driver

  1. Connect Motors to Motor Driver:
    • Identify the two terminals on each motor.
    • Connect one motor to the OUT1 and OUT2 terminals on the motor driver, and the other motor to OUT3 and OUT4.
    • Use jumper wires or screw terminals to ensure connections are secure.
  2. Powering the Motor Driver:
    • Connect the battery pack’s positive (+) and negative (-) leads to the +12V and GND inputs on the motor driver.
    • If the motor driver has a 5V output, use it to power the Raspberry Pi by connecting it to the Pi’s 5V and GND GPIO pins.
  3. Connecting Motor Driver to Raspberry Pi GPIO:
    • Use jumper wires to connect the motor driver’s IN1, IN2, IN3, and IN4 pins to the GPIO pins on the Pi. These inputs will control the forward and backward motion of each motor.

Step 3: Setting Up the Ultrasonic Sensor for Obstacle Detection

The ultrasonic sensor allows the robot to detect objects in its path, helping it avoid collisions. The sensor has four pins: VCC, GND, Trig, and Echo.

  1. Connect Power and Ground:
    • Connect the VCC pin to the Raspberry Pi’s 5V GPIO pin.
    • Connect the GND pin to one of the ground (GND) pins on the Pi.
  2. Connect Trig and Echo Pins:
    • Connect the Trig pin to GPIO 23 on the Raspberry Pi.
    • Connect the Echo pin to GPIO 24.
    • The Trig pin triggers the sensor to send out a sound wave, while the Echo pin receives the reflected wave to measure distance.

Step 4: Programming the Robot to Avoid Obstacles

With the hardware setup complete, it’s time to program the Raspberry Pi to control the motors and read data from the ultrasonic sensor. Python is commonly used for this purpose.

1. Import Required Libraries:

  • Open a Python editor (like Thonny) and start with importing libraries for GPIO control and timing.
Python
import RPi.GPIO as GPIO

import time

2. Define GPIO Pins:

  • Assign GPIO pins for the motor inputs and ultrasonic sensor.
Python
motor_left_forward = 17
motor_left_backward = 27
motor_right_forward = 22
motor_right_backward = 23
trig_pin = 23
echo_pin = 24

3. Initialize GPIO Mode:

  • Set up GPIO mode and configure each pin as an input or output.
Python
GPIO.setmode(GPIO.BCM)
GPIO.setup([motor_left_forward, motor_left_backward, motor_right_forward, motor_right_backward], GPIO.OUT)
GPIO.setup(trig_pin, GPIO.OUT)
GPIO.setup(echo_pin, GPIO.IN)

4. Define the Distance Measurement Function:

  • Create a function to calculate distance based on ultrasonic sensor readings.
Python
def measure_distance():
GPIO.output(trig_pin, True)
time.sleep(0.00001)
GPIO.output(trig_pin, False)
start_time = time.time()
stop_time = time.time()

while GPIO.input(echo_pin) == 0:
start_time = time.time()
while GPIO.input(echo_pin) == 1:
stop_time = time.time()

elapsed = stop_time – start_time
distance = (elapsed * 34300) / 2
return distance

5. Define Movement Functions:

  • Define functions to control forward, backward, and stopping movements.
Python
def move_forward():

GPIO.output(motor_left_forward, True)


GPIO.output(motor_right_forward, True)

def move_backward():
GPIO.output(motor_left_backward, True)
GPIO.output(motor_right_backward, True)

def stop():
GPIO.output(motor_left_forward, False)
GPIO.output(motor_right_forward, False)
GPIO.output(motor_left_backward, False)
GPIO.output(motor_right_backward, False)

6. Main Program Loop:

  • Use a loop to constantly check the distance and adjust movement based on the sensor reading.
Python
try:

while True:


distance = measure_distance()


if distance < 20:


stop()


move_backward()


time.sleep(1)


stop()


else:


move_forward()


except KeyboardInterrupt:


GPIO.cleanup()

 

Step 5: Testing and Troubleshooting the Robot

  1. Testing Movement:
    • Place the robot on a flat surface with obstacles nearby. Run the code and observe how it reacts. It should stop and reverse when an object is detected within 20 cm.
  2. Adjusting Sensor Sensitivity:
    • If the robot reacts too late or too early, adjust the distance threshold in the code. You may also reposition the ultrasonic sensor if it’s not facing forward accurately.
  3. Power Issues:
    • If motors slow down or the Raspberry Pi restarts, check the battery pack’s charge and connections. Motors may require a higher-capacity power source.

Enhancing the Obstacle-Avoiding Robot

Once the basic robot is working, consider adding these features:

  • Additional Sensors: Add infrared or other ultrasonic sensors to improve detection accuracy.
  • Bluetooth or Wi-Fi Control: Enable manual control using a smartphone app or web interface.
  • Advanced Movements: Implement turns or path-following algorithms for more complex navigation.

Building an obstacle-avoiding robot with the Raspberry Pi 5 is a rewarding introduction to robotics. This project combines hardware assembly, wiring, and programming to create a responsive, autonomous robot. With this foundation, you can expand your robot’s capabilities, from adding sensors to developing more advanced navigation logic. Whether for learning or for fun, robotics with the Raspberry Pi offers limitless opportunities to experiment and innovate.

 

 

Skip to content