Obstacle Avoider Robot using Raspberry Pi

Author: Gunja

Are you looking to learn Raspberry Pi and don't know where to start? Are you looking for Raspberry Pi demo projects? Are you looking for learning Raspberry Pi programming with python? This blog will introduce you to the world of Raspberry Pi & Python by demonstrating a simple use case of Obstacle Avoider Robot using Raspberry Pi.


Obstacle Avoider Robot using Raspberry Pi

The Raspberry Pi is a credit card sized single-board computers that can be plugged with a monitor, keyword (along with other computing peripheral device), motors, sensors, etc. to function as a robot. I will demonstrate a simple use case on Raspberry Pi. I have built a car robot enabled with motors and sensors to detect obstacle coming its way, and avoid those obstacle by reversing and making a turn. To try out this use case, you need Raspberry Pi (along with peripheral components) and laptop/desktop and you will need to write code in python.


This article will assist you implement the python program to avoid obstacle in path of your robot, controlling motors and access sensor data. You will need to build a simple car robot with 2 motors, and 1 IR-photo diode sensor. Robot’s function is to move forward, and if detects any object in front, it will turn, and again move forward. This is a repetitive task, and robot will keep exploring the surrounding to detect an object.


Hardware need?

  • Raspberry Pi B/B+ or 2 and basic peripherals: Monitor, Keyboard and Mouse
  • L293D Motor driver board
  • BO DC motors
  • IR sensor module
  • Caster wheel
  • Robot chassis and wheels
  • Double side tape
  • Female to female jumpers
  • 9V Battery and connectors
  • Power bank (5v and 2 amp)
  • Wi-Fi adaptor


Know Important Components -

L293D board is used to drive motors as Raspberry pi or any other microcontroller or processor gives low power logical output. Thus the motors cannot be driven by directly connecting with Raspberry pi. L293D motor driver module converts these low energy logical outputs to motor drivable logics.

BO DC motors are simple DC motors where BO means “Battery Operated”. These are used for actuation of car robot.


IR sensor module consist of IR – Photodiode pair. IR works as transmitter to transmit IR light and Photodiode receives the IR light. Whenever any object comes in IR transmitting range, light gets reflected and received to the by the photodiode. Photodiode works as transducer and coverts light signal to the electronic signal. This electronic signal is converted into switching signal by a transistor and hence an logical output is generated.


Robot chassis means body of the robot which is easily available in the market.


Wi-Fi Adaptor is a USB based wireless LAN device. You can attach it to any of the USB ports provided on your raspberry pi board. It requires device driver and that varies for different OS versions installed on Pi.


You need to get remote desktop connection of your Raspberry pi from your laptop over the same network. You can do it easily by following my previous article “Setup Remote desktop connection from windows machine to Raspberry Pi”.


Steps to make your robot

  • Attach BO motors and caster wheel to the Chassis
  • Attach wheels to the motors
  • Connect sensor to Rpi board. Sensor connections (Rpi pins - Sensor pins)-
  1. - Pin 2 (5v) - Vcc
  2. - Pin 6 (Ground) - Ground
  3. - Pin 5 (GPIO 3) - Vou
  • Connect L293D driver board to Rpi board
  • Connect 9v battery power supply. Power supply connections (Rpi pins - L293D driver board pins+ 9v battery)-
  1. - Pin 29 (GPIO 5) - IN1 (Left motor input1)
  2. - Pin 26 (GPIO 7) - IN2 (Left motor input2)
  3. - Pin 23 (GPIO 11) - IN1 (Right motor input1)
  4. - Pin 33 (GPIO 13) – IN2 (Right motor input2)
  5. - Pin 9 (Ground) - GND
  6. - Pin 4 (5v) – Vcc, Vs/12v Pin to 9v battery’s +ve
  7. - Pin 14(Ground) - 9v battery’s –ve


Once you make all the connections, you can remove monitor and other peripherals and use power bank to power up the Raspberry pi.


Logical Flow of Program

  • Import Libraries for GPIO and time
  • Define output and input pins for  motors and sensor respectively
  • Loop to move robot forward
  • Loop to turn robot if object is detected
  • Sample program iterates through infinitely


How Execute Your Program?

  • Access GUI of Rpi from your PC
  • Develop the code using text editor and save with .py extension on your favorite place (in my case it is “/home/pi/codes/obs_rbt.py”)
  • To run the program – open LX terminal -> file path (cd /home/pi/codes/) and hit enter -> type file name ( sudo python obs_rbt.py) and hit enter
  • Press CTRL+C to stop the program




import RPi.GPIO as GPIO
import time


GPIO.setup(3, GPIO.IN) #IR sensor module
GPIO.setup(5,GPIO.OUT) #Left motor control
GPIO.setup(7,GPIO.OUT) #Left motor control
GPIO.setup(11,GPIO.OUT) #Right motor control
GPIO.setup(13,GPIO.OUT) #Right motor control

#Motor stop/brake

while True:
    i=GPIO.input(3) #Listening for output from IR sensor
        if i==1: #Obstacle detected on IR sensor
            print "Obstacle detected"
            #Move in reverse direction
            GPIO.output(5,1) #Left motor turns clockwise
            GPIO.output(11,1) #Right motor turns clockwise

            #Turn robot left
            GPIO.output(5,0) #Left motor turns clockwise
            GPIO.output(11,1) #Right motor turns anticlockwise
        else:    #No obstacles, robot moves forward
            print "No obstacles"
            #Robot moves forward




We have connected all the pins and IR sensors. IR sensor is able to detect the obstacle but our robot is unable to move after the obstacle is detected. Can you please tell what might be the reason?

Post a comment