top of page

IoT - Distance measurement using Ultrasonic Sensor & Raspberry Pi

Updated: Apr 5

Internet of Things (IoT) refers to connecting all physical devices to the internet, to monitor and control it from any distant place. In this article, let us learn how to interface an Ultrasonic sensor with Raspberry pi for distance measurement.


An Ultrasonic Sensor is used to measures the distance of an object or obstacle via Ultrasonic waves (that travel faster than the speed of audible sound), and it converts the reflected ultrasonic waved into electric signals. It is commonly used in obstacle avoiding robots, blind Walking stick, and automation projects.


First, let us start with the requirements for the circuit connection.


Hardware Requirements:

The image shows the list of components needed. Let's have a quick look at each of them.


Hardware Components
Hardware Components

1. HC-SR04 Ultrasonic Sensor


HC-SR04 Ultrasonic Sensor
HC-SR04 Ultrasonic Sensor

The HC-SR04 Ultrasonic Sensor is used for obstacle detection. An ultrasonic sensor contains two circular structures: one is the #transmitter and the other is a #receiver. The transmitter transmits the ultrasonic sound while the receiver receives the reflected signal. It consists of four pins. They are supply pin (Vcc), Trigger pin (Trig), Echo pin (Echo), and Ground pin (Gnd).


Buy an HC-SR04 Ultrasonic Sensor from here.


2. Raspberry pi


Raspberry pi
Raspberry pi

Unlike #Arduino, Raspberry Pi works with its own Operating System, Linux. So, Raspberry Pi can be called a single-board computer. It does not have any peripherals like a keyboard, mouse, etc. but works when connected to a system.


Features of Raspberry Pi:

In this project, the Raspberry Pi Model B/B+ can be used, which have a 1.2GHz/1.4GHz processor. It has inbuilt WiFi and also Bluetooth capabilities.


Buy Raspberry Pi from here.


3. MicroSD Card


MicroSD Card
MicroSD Card

The MicroSD card used here needs to be loaded with Raspbian OS which is a zip file containing the code needed to run the #program on Raspberry Pi and allows interfacing between the hardware and software.


Buy a MicroSD Card from here.


4. 1K Ohm Resistors


1K Ohm Resistors
1K Ohm Resistors

Resistors are passive devices that restrict the flow of current or divide the voltage through the circuit. The input power passes through these resistors and then to the sensors to avoid damage.


Buy Resistors from here.


5. Breadboard


Breadboard
Breadboard

A breadboard is a rectangular board with small holes in which elements are connected. The connections are not fixed and can be varied. In a breadboard, the holes in one horizontal line are in series connection with each other. Breadboards are made from plastic and come in all shapes, sizes, and even different colors, the most commonly used types are "full-size," "half-size," and "mini" breadboards.


Buy the Breadboard from here.


6. USB Cable


USB Cable
USB Cable

This cable is used to connect the RaspberryPi to the power supply. It also helps in the transmission of data.


Buy USB Cable from here.


7. Jumper Wires


Jumper Wires
Jumper Wires

Jumper wires are used to establish a connection between different elements in the circuit and the #Raspberrypi board. There are two types of wire, male and female. The male wires have ends that can be connected to other ends and the female wires are the ones that have the receiving end.


Buy Jumper Wires from here.


Software Requirements:


1. ThinkSpeak


ThinkSpeak
ThinkSpeak

ThinkSpeak cloud is an open-source IoT application that provides API to store and retrieve data from the data given through the device connected to it. It uses HTTP and MQTT protocols. ThinkSpeak is a cross-platform which is used for data analysis( by plotting real-time graphs) and has a grown relation with Map-labs. It also has a numerical computing software MatLab for map-works.


2. Raspberry pi Imager

Raspberry pi Imager LOGO
Raspberry pi Imager LOGO

The software is used to allow the interfacing of the hardware devices and software applications. This software needs to be downloaded into the system for the Raspberry Pi Chip to be recognizable.


3. Python IDLE IDE


Python IDLE  IDE
Python IDLE IDE

The main software application is a #Python IDE for this device to work. The code is written in the notepad and saved with a .py extension so that it becomes a python code file and can be interfaced with the device.


Circuit Connection:



The Circuit connection for this project is shown. As stated above, the Ultrasonic sensor has four pins.

  • The supply pin (Vcc) is connected to the 5Volt pin of the Raspberry pi.

  • The Trigger pin (Trig) is used to generate the sound waves and the Trigger pin is connected to pin 7 in the raspberry pi board.

  • The Echo pin is used to receive the reflected sound waves based on the sound signal transmitted. The transmitted sound signal hits an object and bounces back from the object is received by the receiver. The time taken by the signal to reach the receiver after hitting the object is recorded, by which the distance is calculated between the object and sensor.

  • The Echo pins are connected to the 2 resistors - 1K ohm and 2K ohm resistors. 1K ohm is connected to the echo pin and on another side, 2K ohm is connected by using the breadboard. And another end of the 2K ohm is connected to the negative terminal of the breadboard.

  • The resistors are connected to prevent the damages caused while connecting to the pins of the raspberry pi.

  • Between the two resistors, a divider line is created by which the echo pin is connected to pin 16 of the raspberry pi.

  • The Ground pin of the sensor and the Raspberry pi is commonly connected to the negative terminal of the breadboard to make a common ground connection.

The Raspberry Pi board is then connected to the computer through the #USB cable where the software is interfaced with the hardware device.


Download the code from here



Working:

The following images show how the circuit looks when connected and assembled.


Connected, Unpowered Circuit
Connected, Unpowered Circuit

Once the circuit connections are completed and the code is written in the #IDE, connect the hardware device to the computer and interface the code to the hardware.


Python Code File
Python Code File

The code is written in a notepad file and saved as a python file which creates the type of document required for the code execution.


Terminal Window
Terminal Window

After the file with the code is saved under the given extension, the #SSH (Secure Socket Shell) Terminal is opened.


Terminal Output
Terminal Output

The saved file name is entered and is then opened in the terminal. The output of which is shown above in the image.


Hardware, Powered up
Hardware, Powered up

By the time the terminal with the file and code is opened, the Thinkspeak cloud application must be opened and an account is created. Create a new channel for the project and add widgets and configure the parameters. Here the field is chosen in Distance in centimeter-scale with Numeric display.


The below image displays the output implementation when the hand is kept has an obstruction in front of the sensor, the output display shows the distance of the object.


Output with Obstruction
Output with Obstruction

When there is no obstruction in front of the sensor the output is displayed as:

Output Without Obstruction
Output Without Obstruction

A further detailed explanation of the circuit building, coding part, and the working with the terminal is explained clearly in the video.


Video By - Sai Parimal

#Interfacing Ultrasonic sensor for IoT #applications helps in creating basic automation projects. It is a basic project to be known for creating or applying it to design various project applications and in huge automation systems.

 

See also:

2,006 views2 comments

Related Posts

See All
bottom of page