Raspberry Pi interface with Ultrasonic sensor using Python

In this post I will discuss the interfacing of the Raspberry Pi with the Ultrasonic sensor. In the
previous posts I have discussed the Inter-integrated circuit (I2C) serial communication, Serial
Peripheral Interface (SPI) serial communication and GPIO programming using the Python
language. The purpose of using the Python language in these tutorials is as described earlier is
readily available online support for Python language for Raspberry Pi. So this post is aimed to
provide the solution for sensor communication either using the serial communication or using
General Purpose Input / Output pins. You can connect to the sensors either through serial
communication ports as described in the previous or by the simple data pins obviously it depend
the type of interface used in the particular sensor.

Raspberry Pi interface with Ultrasonic sensor using Python

In this post you will learn about the Ultrasonic sensor and how to use it with raspberry Pi to perform the task in hand I will also discuss the various applications in which the Ultrasonic
sensor can be used. So sit back, keep reading and enjoy learning.

Ultrasonic sensor:

The raspberry Pi can be connected to the number of analog and digital sensors to measure the
readings of the various quantities that the particular sensor is designed to provide. The list of
some of the common sensors that can be connected to the Raspberry Pi is given below:

Humidity and temperature sensor

Barometer

Soil sensor

Gas Sensor

PIR Motion Sensor

HC-SR04 ultrasonic sensor

Magnetic Switch / Reed Relay

Infrared distance meter

RFID-RC522 – Inductive RFID card reader

Gyroscope

HMC5883L / GY-271 Compass

Accelerometer and many more.

Notice that these sensors can either be based on some serial communication protocol employing
some serial communication bus or either these sensors can simply be connected to the General
Purpose Input / Output pins depending on the interface of the sensors. To make this point clear
please read my previous posts or just keep the post reading. Fortunately the task in hand employ
the Ultrasonic sensor which does not used either of the I2C or SPI communication that is use you
can simply connect this sensor to the GPIO pin of the Raspberry PI and then can program with
Python to read the readings. The sensor I am using is HC SR40 ultrasonic sensor which looks
like the image shown below:

Applications of the Ultrasonic Sensor:

Let us discuss some of the common applications of the Ultrasonic sensor to make its importance
clear:

In order to make some type of robot either mechanical or the Humanoid the engineers need to
provide some way to the system so that it would be able to perceive its environment. An
important thing for the robot to simulate itself is the knowledge of the distance between the
objects of its environment. So the Ultrasonic sensor provides the easy solution for it. Although
other ways like image processing and computer are also available but obviously the Ultrasonic
sensor provide the easy and cost efficient solution.

Raspberry Pi python code for Ultrasonic sensor:

Now let us see how to write the code for Raspberry Pi and ultrasonic sensor in the Python shell
in the Raspberry Pi. Here I will just discuss the method of receiving the readings of the
Ultrasonic sensor through the Raspberry Pi and display it in the terminal window. The use of
these readings to perform the particular task will be discussed in the next post.

Step1:

Open the Python shell in the Raspberry Pi.

Step2:

You can copy and paste the following code in the Python IDLE.

Python

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

importRPi.GPIO asGPIO

importtime

GPIO.setmode(GPIO.BCM)

TRIG=23

ECHO=24

print&quot;Distance Measurement InProgress&quot;

GPIO.setup(TRIG,GPIO.OUT)

GPIO.setup(ECHO,GPIO.IN)

GPIO.output(TRIG,False)

print&quot;Waiting ForSensor To Settle&quot;

time.sleep(2)

GPIO.output(TRIG,True)

time.sleep(0.00001)

GPIO.output(TRIG,False)

whileGPIO.input(ECHO)==0:

pulse_start=time.time()

whileGPIO.input(ECHO)==1:

pulse_end=time.time()

pulse_duration=pulse_end-pulse_start

distance=pulse_durationx17150

distance=round(distance,2)

print&quot;Distance:&quot;,distance,&quot;cm&quot;

GPIO.cleanup()

GPIO library for Python

Here I am assuming you have already installed the GPIO library for Python. If not please read

my previous post. There I have discussed the installation of the GPIO library for the Python.

Raspberry PI Ultrasonic sensor Hardware:

Connect the circuit of the Raspberry Pi and ultrasonic sensor like the one shown in the following
figure.

Connect each pin just like the circuit shown above if you are using the code that I have provided
in the above.
The simple schematic of the above circuit is shown in the image below:

After connecting the circuit save the file you have created in the Python shell. Do not forget to
write “.py” extension at the end of the name of your code. Now you can run the file directly from
the Python shell or by typing its name in the terminal window of the Raspberry Pi. You will see
the values display on the terminal window of the Raspberry Pi.
That is for all now. I hope this article will be very helpful for you. In the next article I will
discuss the hurdle avoiding robot using the Raspberry Pi and the Ultrasonic sensor. Till then stay
connected keep reading and enjoy learning.

Share this:

Related

Author: admin

I am Kashif Mirza, the founder of ProjectIOT123. I am an Embedded Engineer and working on Embedded Projects since 2003. I have worked on Arduino, Raspberry Pi, PIc Microcontroller, 8051 etc. and have designed both prototypes & industrial projects.