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.
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.
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
- Soil sensor
- Gas Sensor
- PIR Motion Sensor
- HC-SR04 ultrasonic sensor
- Magnetic Switch / Reed Relay
- Infrared distance meter
- RFID-RC522 – Inductive RFID card reader
- 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:
Let us discuss some of the common applications of the Ultrasonic sensor to make its importance
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.
You can copy and paste the following code in the Python IDLE.
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) TRIG = 23 ECHO = 24 print "Distance Measurement In Progress" GPIO.setup(TRIG,GPIO.OUT) GPIO.setup(ECHO,GPIO.IN) GPIO.output(TRIG, False) print "Waiting For Sensor To Settle" time.sleep(2) GPIO.output(TRIG, True) time.sleep(0.00001) 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.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
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.