Raspberry Pi interface with Ultrasonic sensor using Python

Raspberry Pi interface with Ultrasonic sensor using Python 29

[otw_is sidebar=otw-sidebar-1]Raspberry Pi interface with Ultrasonic sensor using PythonIn this post I will discuss the interfacing of the Raspberry Pi with the Ultrasonic sensor. In the
previous posts (Raspberry Pi interface with Ultrasonic sensor using Python)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.

[otw_is sidebar=otw-sidebar-3]

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.

[otw_is sidebar=otw-sidebar-2]

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:

Ultrasonic Sensor,Raspberry pi Ultrasonic SensorApplications of the Ultrasonic Sensor:

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

[otw_is sidebar=otw-sidebar-3]

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.


Open the Python shell in the Raspberry Pi.

GPIO library for Python


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

import RPi.GPIO as GPIO
import time
TRIG = 23
ECHO = 24
print "Distance Measurement In Progress"
GPIO.output(TRIG, False)
print "Waiting For Sensor To Settle"
GPIO.output(TRIG, True)
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 x 17150
distance = round(distance, 2)
print "Distance:",distance,"cm"


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

Raspberry Pi interface with Ultrasonic sensor using PythonConnect 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:

[otw_is sidebar=otw-sidebar-3]

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.

Leave a Reply

Your email address will not be published. Required fields are marked *