[otw_is sidebar=otw-sidebar-1]Raspberry Pi Based Obstacle Avoiding Robot with Ultrasonic Sensor,Raspberry Pi Based Ultrasonic Sensor,h bridgeIn this article I will discuss the Raspberry Pi Based Obstacle Avoiding Robot with Ultrasonic Sensor and Python language. In the previous article I have discussed the interface of Ultrasonic sensor and the Raspberry Pi using the Python language. So this post will be oriented around how to use this Ultrasonic sensor and Raspberry Pi to make the Hurdle avoiding car.
After reading this post you will be able to understand the basics of the circuit included in the Project and also you will learn about the important libraries of Python in the Raspbian (recommended operating system) and how to use these libraries to write the code that can perform task in the physical world. So sit back keep reading and enjoy learning.

[otw_is sidebar=otw-sidebar-2]

Equipment Required of project:

  1. You will need to have some of the equipment to be able to make this Project. The list of the important equipment needed for the Project is listed below:
  2. Raspberry Pi.
  3. Ultrasonic sensor.
  4. Motor driver.
  5. Jumper wires.
  6. Bread board or vero board as you wish.
  7. Car chassis.
[otw_is sidebar=otw-sidebar-3]

Project Overview:

The hurdle avoiding car we are considering in this post has the Raspberry Pi as its main controller. The ultrasonic connected to the Raspberry Pi will guide the autonomous vehicle to make turns and stop and reverse the direction etc. One of the very important components employed in the Project is the motor driver as we all know that the Raspberry Pi cannot supply as much current as required to drive the motor. So in order to be able to drive the motor using the Raspberry Pi the motor driver needs to be connected between the Raspberry PI and the motor to provide enough current to the motor to move the hurdle avoiding autonomous vehicle. So as a summary the hurdle avoiding autonomous vehicle based on the Raspberry Pi will be an important demonstration of the knowledge we have possessed in the previous posts. Without any delay let us dive deep into the Project.



Raspberry Pi, Ultrasonic sensor and the Python:

As described in the previous posts there are number of platforms for Programming the Raspberry Pi but the most popular is the Python IDLE which comes by default in the Raspbian for the Raspberry Pi. The library involved in the code for the Project is the GPIO.Rpi which is the Python library for programming the general purpose input / output pins of the Raspberry Pi. It comes installed by default in the newer versions of the Raspbian in case you do not have installed this library read my post on installing the library and then come here again. It is important that you have this library.
The ultrasonic sensor has four pins.


The Vcc will be connected to the power supply and GND will be connected to the ground. The other two pins will be connected to the GPIO pins of the Raspberry Pi. Notice here that the Raspberry Pi GPIO pins are not configured as either I2C or SPI. This is the advantage of using the Ultrasonic sensor for the beginners’ tutorial. The way these data pins are connected to the Raspberry Pi will be clear when we write the code using the Python. In short one of the two pins through which the Ultrasonic sensor is connected will be serving as the input and the other pin as the output more about this will be discussed in the sections to follow.

Hardware of the Project:

Let us discuss the circuit configuration required in the project. Here I am assuming that you have all the equipment required and have already installed the library. The block diagram of the overall project is shown in the following image:

Raspberry Pi Based Obstacle Avoiding Robot with Ultrasonic SensorAs can be seen in the above picture that the Raspberry PI is connected to the Ultrasonic sensor and then it is connected to the motor driver which in turn connects to the motors. The ultrasonic sensor gives the data about the surroundings of the vehicle to the Raspberry Pi. The raspberry Pi then operates on the data by making the decisions which are governed by the code we will be writing in the next section. After operating on the data provided by the Ultrasonic sensor then the Raspberry Pi will give the commands to the motor driver which in turns moves the motors to change the directions of the Hurdle avoiding autonomous vehicle.

Important note:
[otw_is sidebar=otw-sidebar-3]


Before proceeding make sure which of your motor terminal polarity gives the clockwise rotation and vice versa.
Now let us connect the circuit like shown in the following picture.

Raspberry Pi Based Obstacle Avoiding Robot with Ultrasonic SensorThe Vcc pin of each of the component that is the Raspberry Pi, motor driver, ultrasonic sensor are connected to the common terminal, in the same way the ground of all the components are connected together. Now you need to configure the L293D motor controller IC in the way shown in the figure. If you are new using the L293D motor controller open its data sheet and notice which of the pin of the pin of the motor controller IC are connected respectively. Or you can see any of the tutorials on this IC. By the way it is not that difficult. The L293D motor controller IC will serve as the H-bridge topology in this circuit. Note that the pin number 11 and 13 are connected to the trigger pin and the Echo pin of the Ultrasonic sensor. The GPIO pins 32, 34, 36 38 and 40 are connected to the input pins of the motor driver IC. Then the output of the L93d motor controller IC are connected to the motors. The pin out of the L293D motor controller IC is shown in the following image to help you understand the above circuit.
Note that the above circuit is connected to the code written in the Python language. Now after you have wired the above diagram consider the code in the following section.

Python code for the Hurdle Avoiding car based on the Raspberry Pi:

In order to write the code for the Raspberry Pi in the python IDLE, you need to follow the following steps.


Open the Python IDLE:


[otw_is sidebar=otw-sidebar-3]



Now you can copy and paste the following code in the python IDLE:

import RPi.GPIO as GPIO
import time
TRIG = 17
ECHO = 27
led = 22
GPIO.output(led, 1)
def stop():
print "stop"
GPIO.output(m11, 0)
GPIO.output(m12, 0)
GPIO.output(m21, 0)
GPIO.output(m22, 0)
def forward():
GPIO.output(m11, 1)
GPIO.output(m12, 0)
GPIO.output(m21, 1)
GPIO.output(m22, 0)
print "Forward"
def back():
GPIO.output(m11, 0)
GPIO.output(m12, 1)
GPIO.output(m21, 0)
GPIO.output(m22, 1)
print "back"
def left():
GPIO.output(m11, 0)
GPIO.output(m12, 0)
GPIO.output(m21, 1)
GPIO.output(m22, 0)
print "left"
def right():
GPIO.output(m11, 1)
GPIO.output(m12, 0)
GPIO.output(m21, 0)
GPIO.output(m22, 0)
print "right"
while True:
for i in range(5):
GPIO.output(TRIG, False)                 #Set TRIG as LOW
time.sleep(0.1)                                   #Delay
GPIO.output(TRIG, True)                  #Set TRIG as HIGH
time.sleep(0.00001)                           #Delay of 0.00001 seconds
GPIO.output(TRIG, False)                 #Set TRIG as LOW
while GPIO.input(ECHO)==0:              #Check whether the ECHO is LOW
GPIO.output(led, False)
pulse_start = time.time()
while GPIO.input(ECHO)==1:              #Check whether the ECHO is HIGH
GPIO.output(led, False)
pulse_end = time.time()
pulse_duration = pulse_end - pulse_start #time to get back the pulse to sensor
distance = pulse_duration * 17150        #Multiply pulse duration by 17150 (34300/2) to get distance
distance = round(distance,2)                 #Round to two decimal points
print avgDistance
if avgDistance < 15:      #Check whether the distance is within 15 cm range
if (count%3 ==1) & (flag==0):

Notice the libraries we have imported in the above code. One is the GPIO.Rpi and the other library is the time. Here I am just summarizing the code mentioned above:


First I have imported the libraries, then I have declared the variables to be used in the rest of the code, then I have defined the forward, backward, right and left movements. Then after this the Ultrasonic sensor is given the pulse. And then the calculation is performed to measure the average distance and in the last if else structure is used to make the commands.

That is all for now. I hope this article would be helpful for you. In the next post I will discuss the web server using the Raspberry Pi. Till then stay connected, keep reading and enjoy learning.