Building a Simple Autonomous Robot Using Raspberry Pi and Sensors

Building a simple autonomous robot can be an exciting project for students and hobbyists interested in robotics and programming. Using a Raspberry Pi along with sensors, you can create a robot that navigates its environment without human control. This guide introduces the basic steps to assemble and program such a robot.

Materials Needed

  • Raspberry Pi (any model with GPIO pins)
  • Motor driver (e.g., L298N)
  • DC motors with wheels
  • Ultrasonic distance sensor (e.g., HC-SR04)
  • Breadboard and jumper wires
  • Power supply (battery pack)
  • Chassis or frame to hold components

Assembly Steps

First, assemble the chassis and attach the DC motors with wheels. Connect the motors to the motor driver, which will control their movement. Then, connect the ultrasonic sensor to the GPIO pins of the Raspberry Pi, ensuring proper wiring for trigger and echo pins. Finally, power the Raspberry Pi with an appropriate battery pack.

Connecting the Hardware

Wire the ultrasonic sensor’s trigger and echo pins to GPIO pins on the Raspberry Pi. Connect the motor driver’s input pins to GPIO pins to control motor direction and speed. Make sure the power supply can handle all components safely.

Programming the Robot

Use Python to program the robot’s behavior. Start by importing necessary libraries such as RPi.GPIO for GPIO control and time for delays. Write functions to read distance from the ultrasonic sensor and to control motor movement. Implement a simple algorithm: if an obstacle is detected within a certain range, the robot turns; otherwise, it moves forward.

Sample Code Snippet

Here’s a basic example:

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)

# Motor pins
motor_left_forward = 17
motor_left_backward = 18
motor_right_forward = 22
motor_right_backward = 23

# Ultrasonic sensor pins
TRIG = 5
ECHO = 6

GPIO.setup([motor_left_forward, motor_left_backward, motor_right_forward, motor_right_backward], GPIO.OUT)
GPIO.setup(TRIG, GPIO.OUT)
GPIO.setup(ECHO, GPIO.IN)

def measure_distance():
    GPIO.output(TRIG, False)
    time.sleep(0.5)
    GPIO.output(TRIG, True)
    time.sleep(0.00001)
    GPIO.output(TRIG, False)

    while GPIO.input(ECHO) == 0:
        pulse_start = time.time()

    while GPIO.input(ECHO) == 1:
        pulse_end = time.time()

    pulse_duration = pulse_end - pulse_start
    distance = pulse_duration * 17150
    return distance

def move_forward():
    GPIO.output([motor_left_forward, motor_right_forward], GPIO.HIGH)
    GPIO.output([motor_left_backward, motor_right_backward], GPIO.LOW)

def turn_left():
    GPIO.output(motor_left_forward, GPIO.LOW)
    GPIO.output(motor_right_forward, GPIO.HIGH)

try:
    while True:
        dist = measure_distance()
        if dist < 20:
            turn_left()
            time.sleep(1)
        else:
            move_forward()
        time.sleep(0.1)
except KeyboardInterrupt:
    GPIO.cleanup()

Testing and Improvements

Test your robot in a controlled environment. Observe how it responds to obstacles and adjust the sensor range and movement logic as needed. You can improve the robot by adding more sensors, using better motors, or implementing more complex navigation algorithms like mapping or path planning.

Building an autonomous robot with Raspberry Pi and sensors is a rewarding project that introduces key concepts in robotics, programming, and electronics. With experimentation and creativity, you can expand its capabilities and develop more sophisticated robots.