Search form

Robots are machines which reduce the human efforts in heavy works by automating the tasks in industries, factories, hospitals etc. Most of the robots are run by using some control unit or components like a push button, remote, joystick, PC, gestures and by executing some command by using controller or processor. But today we are here with a Automatic Robot which moves autonomously without any external events avoiding all the obstacle in its path, yes we talking about Obstacle Avoiding Robot. In this project, we have used Raspberry Pi and Motor driver to drive the robot and Ultrasonic sensor for detecting objects in the path of Robot.

Previously we have covered many useful Robots, you can find them in our Robotics projects section.

Components Required:

Raspberry Pi

Ultrasonic Sensor Module HC-SR04

ROBOT Chassis complete with screw

DC Motors

L293D IC

Wheels

Bread Board

Resistor (1k)

Capacitor (100nF)

Connecting wires

Power supply or Power bank

Ultrasonic Sensor Module:

An Obstacle Avoider Robot is an Automated Robot and it doesn’t need to be controlled using any remote. These types of automated robots have some ‘sixth sense’ sensors like obstacle detectors, sound detector, heat detector or metal detectors. Here we have done Obstacle Detection using Ultrasound Signals. For this purpose, we have used Ultrasonic Sensor Module.

Ultrasonic sensor HC-SR04 is used to measure distance in the range of 2cm-400cm with an accuracy of 3mm. The sensor module consists of an ultrasonic transmitter, receiver, and the control circuit. Ultrasonic Sensor consists of two circular eyes out of which one is used to transmit the ultrasonic wave and the other to receive it.

We can calculate the distance of the object based on the time taken by ultrasonic wave to return back to the sensor. Since the time and speed of sound is known we can calculate the distance by the following formulae.

Distance= (Time x Speed of Sound in Air (343 m/s))/2.

The value is divided by two since the wave travels forward and backward covering the same distance.Thus the time to reach obstacle is just half the total time taken.

So we have calculated the distance (in centimetre) from the obstacle like below:

Where pulse_duration is the time between sending and receiving ultrasonic signal.

Circuit Explanation:

Circuit is very simple for this Obstacle Avoiding Robot using Raspberry Pi. An Ultrasonic Sensor module, used for detecting objects, is connected at GPIO pin 17 and 27 of Raspberry Pi. A Motor Driver IC L293D is connected to Raspberry Pi 3 for driving robot’s motors. Motor driver’s input pins 2, 7, 10 and 15 are connected to Raspberry Pi GPIO pin number 12, 16, 20 and 21 respectively. Here we have used two DC motors to drive the robot in which one motor is connected to the output pin 3 & 6 of motor driver IC and another motor is connected at Pin 11 & 14 of motor driver IC.

How it works:

Working of this Autonomous Robot is very easy. When the Robot is powered on and starts running, Raspberry Pi measures the distances of objects, in front of it, by using Ultrasonic Sensor Module and stores in a variable. Then RPi compares this value with predefined values and take decisions accordingly to move the Robot Left, Right, Forward, or backward.

Here in this project, we have selected 15cm distance for taking any decision by Raspberry Pi. Now whenever Raspberry Pi gets less than the 15cm distance from any object then Raspberry Pi stops the robot and moves it back and then turns it left or right. Now before moving it forward again, Raspberry Pi again checks whether any obstacle is present within the range of 15 cm distance, if yes then again repeats the previous process, else move the robot forward until it will detect any obstacle or object again.

The programming part of this project plays a very important role to perform all the operations. First of all, we include required libraries, initialize variables and define pins for ultrasonic sensor, motor and components.

After it, we have created some functions def forward(), def back(), def left(), def right() to move robot in forward, backward, left or right direction respectively and def stop() to stop the robot, check the functions in Code given below.

Then, in the main program, we have initiated Ultrasonic Sensor and read time between transmission and reception of the signal and calculated the distance. Here we have repeated this process for 5 times for better accuracy. We have already explained the process of calculating the distance using Ultrasonic sensor.

Hi Saddam sir
I am Amal . I made a same prototype using arduino . Now I am very interested in making this using raspberry pi3
in my project, I used a servo motor on which ultrasonic sensor was attached which was used to scan distance and move accordingly so can u plzzz make the schematic of the one using servo motor and its code in raspberry pi3......plzzzzzzz

Hey guys, I ran the code successfully and everything is working fine. However, how do you stop the motors? If I stop the script with Ctrl + C, I get back to the terminal prompt but the motors keep running.