© 2022 - BYJU'S Future School. Please Don't Share, Download or Copy This File Without Permission
© 2022 - BYJU'S Future School. Please Don't Share, Download or Copy This File Without Permission
Class Kids will learn and implement how to use Ultrasonic sensor for
Description calculating the distance of an object
Class ADV-C282
Goals
1. Learn about Ultrasonic sensor
2. Design a circuit and write code for calculating the distance of an
object using an Ultrasonic sensor.
● Student Resources:
○ Use Gmail login credentials.
○ Laptop with camera
○ Earphone with mic (optional)
○ Notepad and Pen
1
Step 1: In the previous class 281, we learned
Warm up about creating the IoT based circuits
(5 mins) to run the codes by understanding the
functionality of components like
Arduino Uno, PIR sensor, Micro
Servo and LED
2
Well done!!
3
In the previous class we used PIR sensor
What is a sensor?
A sensor is a device that detects an event or change in its environment and sends the
information to other devices.
Sensors play an important role in creating solutions using IoT. Sensors are devices that
detect external information, and send signals to other connected devices.
Sensors are used to measure physical quantities such as temperature, light, pressure,
sound, humidity and distance. They send signals to the processor.
For example: a heat sensitive sensor can be used in high end computers. It is used to sense
the heat of the computer. If the temperature of the computer is very high then the heat
sensor gives the indication by warning alarm or emitting light.
4
What is the use of the PIR sensor ?
We used the PIR sensor for detecting motion of an object, after detection we activated
the servo motor and turned on the LED.
In today’s class we are going to learn about another sensor which is called an Ultrasonic
sensor.
Ultrasonic sensor:
In today’s class we are going to learn about ultrasonic sensors. This sensor is used to
measure the distance of the object from the sensor. Like how far an object is from the
Ultrasonic sensor.
An ultrasonic sensor is a device that measures the distance of an object using ultrasonic
sound waves.
5
Here the ultrasonic sensor sends the signal(waves) and waits for its reflection after hitting
the object. After hitting the object the sensor gives the time duration taken for the signal to
come back after hitting the object. This time duration is used with a formula for calculating
the distance.
6
Ultrasonic sensors are very useful in cars for various purposes. It is installed in the front and
back side of the car. It is used to detect distance between the car and other objects like walls
and inform the driver about the same.
7
Using Arduino and Ultrasonic components we will be doing the following tasks:
Open the url given Teacher Activity - 1 and complete the circuit by following the below
mentioned steps:
8
Note : Teacher-Activity-1, it has the complete code for Task 01.
You need to first explain and create the circuit of Task 01 (Be very careful while
making the connection). Then explain the complete code to the student.
Task 01:
Task 01 : Components
Here we want you to make a connection between the Arduino and Ultrasonic sensor to find
distance. Components we are going to use are:
a. Arduino Uno
b. HC-SR04 Ultrasonic Distance Sensor
● The Arduino Uno component is already there when you open the project.
9
For Task 01, we are going to use Gnd pin, 5V pin, 2 pin and 3 pin.
Gnd (Ground):
This Gnd(Ground) pin is used as a negative terminal of the Arduino.
© 2022 - BYJU’S Future School.
Note: This document is the original copyright of BYJU’S Future School.
Please don't share, download or copy this file without permission..
10
What is the use of Gnd pin in Arduino Uno?
Gnd pin is used for maintaining the current flow in the circuit.
D 2 pin:
This Digital 2 pin of Arduino is used here for controlling the digital output of the Ultrasonic
sensor. We are going to connect this with the Ultrasonic sensor’s trigger pin. We will see the
trigger pin while discussing the Ultrasonic sensor.
D 3 pin:
This Digital 3 pin of Arduino is used here getting the input of the Ultrasonic sensor. We are
going to connect this with the Ultrasonic sensor’s echo pin. We will see the echo pin while
discussing the Ultrasonic sensor.
5V pin:
This pin provides 5 Voltage power supply to the Ultrasonic sonic sensor. It acts as a positive
terminal of the Arduino.
11
● And type the component name ‘Ultrasonic distance sensor’ in the search area.
● Now click ‘HC-SR04 Ultrasonic Distance Sensor’ from the given option.
© 2022 - BYJU’S Future School.
Note: This document is the original copyright of BYJU’S Future School.
Please don't share, download or copy this file without permission..
12
● HC-SR04 Ultrasonic Distance Sensor will be added in our design area.
13
Ultrasonic Sensor:
ultrasonic1:VCC - It is the first terminal, which acts as a positive terminal of the ultrasonic
sensor which will get the power supply from Arduino.
Trigger pin:
14
ultrasonic1:TRIG - It is the second terminal, which is used to start the ultrasonic sensor for
detecting and finding the distance of the object
Echo pin:
ultrasonic1:ECHO - It is the third terminal of the ultrasonic sensor, this pin will give us the
reading from the ultrasonic sensor about the distance of the object.
Ground pin:
15
ultrasonic1:GND - It is the fourth terminal, which acts as a negative terminal of the
ultrasonic sensor used to maintain the flow of the current.
Working of ultrasonic sensor:
Sensor sends the ultrasonic sound waves and waits for the reflected waves once the
reflected waves are received, then this time duration is considered for calculation of the
distance between the sensor and the object.
● Now make a connection with the VCC pin of the Ultrasonic sensor to the 5 V pin of
the Arduino Uno.
Here we are connecting the VCC (Voltage Common connector) terminal of the
ultrasonic sensor to the 5 voltage terminal of the Arduino Uno to get power supply
from the arduino.
© 2022 - BYJU’S Future School.
Note: This document is the original copyright of BYJU’S Future School.
Please don't share, download or copy this file without permission..
16
● Now make a connection with the trigger pin of the ultrasonic sensor to the 3 pin of the
arduino.
Here we are connecting the trigger terminal of the ultrasonic sensor to the 3 pin of the
arduino. This connection will help to trigger the ultrasonic sensor to start functioning.
17
● Now make a connection with the Echo pin of the ultrasonic sensor to the 2 pin of the
arduino.
Here we are connecting the echo pin of the ultrasonic sensor to the 3 pin of the
arduino. This connection will help to get the distance between the ultrasonic sensor
and the object.
18
● Now make a connection with the Gnd pin of the ultrasonic sensor to the Gnd pin of
the arduino.
Here we are connecting the Gnd (Ground) terminal of the ultrasonic sensor to the
Gnd (Ground) terminal of the Arduino Uno so that the flow of the current can be
maintained between the arduino and ultrasonic sensor.
19
How does it work?
1. Arduino sends a signal to the ultrasonic sensor via trigpin to get started
2. Then the ultrasonic sensor starts emitting the waves.
3. The waves are reflected back to the ultrasonic sensor after hitting the object.
4. And then the echo pin sends the time duration value to the arduino. This time duration
is nothing but the time taken for the wave to come back after hitting the object.
20
There you can see how the waves are emitted from an ultrasonic sensor and then
reflected back to the ultrasonic sensor after hitting the object.
Task 01 : Coding
21
● Click on the sketch.ino to add the functionality of our design.
● First we will define the variables for holding the pin numbers and variables for holding
time and calculating distance values.
© 2022 - BYJU’S Future School.
Note: This document is the original copyright of BYJU’S Future School.
Please don't share, download or copy this file without permission..
22
○ At line no. 1 We are defining a
variable ‘echopin’ using datatype 'int' and will store value ‘2’.
■ ‘int’ stands for integer data type and it is used to store the numbers.
■ Here we are storing the value 2 inside the echopin variable. Because
we have connected the echo pin of the ultrasonic sensor with pin 2 of
the Arduino and this pin will help us to get the readings (time taken by
the wave to come back after hitting the object) from the trigger
ultrasonic sensor.
■ Here we are storing the value 3 inside the trigpin variable. Because we
connected the trigger pin of the ultrasonic sensor with the pin 3 of the
Arduino and this pin will help to trigger the ultrasonic sensor to start
functioning.
■ Here we are declaring a variable ‘Time’ for storing the travel time value.
23
■ Here we are declaring a variable ‘Distance’ for holding the calculated
distance between the sensor and the object.
● Inside the void set() function, we are going to add the following:
pinMode() function is used to set the mode of the pin as input or output.
24
● At line no. 10, We are defining the
Serial.begin() function inside this we are passing a value 9600.
- serial.begin() : It is the method which is actually used to transfer the data from
one device to the other connected device.
For example: Sending and receiving data Arduino Uno and the Ultrasonic
sensor.
- 9600 : It is the transfer rate(speed), means at 9600 rate transferring of data will
happen between the connected devices
We
are printing a message ‘Ultrasonic Distance:...”.
25
● Now we will write the logic of resetting the ultrasonic sensor, this will help in starting
the ultrasonic sensor. The logic of resetting ultrasonic sensor is:
○ We will stop the functioning of ultrasonic sensor
○ We will start the functioning of ultrasonic sensor
○ What is the use of the digitalWrite function and what parameters does it
take?
26
○ LOW - We are setting the power supply as LOW, as we want the ultrasonic
sensor trigger pin connection to turn OFF and to stop receiving signal from the
arduino
We are turning OFF the trigger pin of the ultrasonic sensor, so that the
ultrasonic sensor stops working.
● At line no. 19
We will write a pulseIn() function to get the time taken by the sound wave to reflect
back after hitting the object, and using this time value we will be calculating the
distance value. For now we will store this value inside the Time variable.
○ pulseIn() function is used to read the signal coming from an ultrasonic sensor.
It takes 2 parameters.
■ The pin on which the component is connected.
■ The action needs to be taken on those components.
27
● At line no. 20, We
are calculating the distance between the sensor and the object using the distance
formula.
Distance Formula:
Distance = Time * 0.0172
- Here Time is the duration taken but the ultrasonic waves hit the object and
come back.
- 0.0172 is the number which we need to use for finding the distance of any
object. This formula will be constant whenever you need to find the distance
you can use this formula.
28
delay() function is used to delay the program and it takes 1 parameter in
milliseconds.
- Serial.println() : This function helps to print the next statement in the next line.
cm : This message will be displayed on the console.
29
- 1000 - It is the time in milliseconds for which the program will be delayed i.e
1000 means for 1 second.
● Click on button which is located on the top left side of the design area.
Output:
30
Now click on the ultrasonic sensor and you will get a small box of ‘Editing Ultrasonic
Distance Sensor’.
31
As we adjust the distance of the ultrasonic sensor, we can see the updated distance and
time value on the console.
NOTE: Make the student understand about the correlation of distance and
duration it is very important:
If I change the value of distance the value of duration is also changed and we can see in this
console such that:
If the distance is higher the duration is higher and vice versa.
Q: What can be the reason for this correlation between distance and duration?
A: It’s simple as we know the distance is calculated using the duration value(time taken by
the waves to reflect back after hitting the object), hence we can see this correlation.
● Ask the Student to press the ESC key to come back to the panel.
© 2022 - BYJU’S Future School.
Note: This document is the original copyright of BYJU’S Future School.
Please don't share, download or copy this file without permission..
32
● Guide the Student to start Screen Share.
● The Teacher gets into Fullscreen.
Login to Wokwi:
4. As we are going to work with the Arduino Uno board, click on the Arduino Uno board.
33
5. As you click on Arduino Uno, You will get the interface as shown below.
1. Add the HC-SR04 ultrasonic sensor by clicking button in the design area of
the wokwi project.
2. Make the Connections between Arduino Uno and HC-SR04 ultrasonic sensor.
34
Task 02: Write a program to calculate the distance of an object using an Ultrasonic
sensor:
3. Add the codes in sketch.ino file.
35
4. Simulate the circuit by changing the distance of the ultrasonic sensor and we can see
the measurement of an object i.e time and distance of the ultrasonic sensor in the
bottom of the simulation.
Output:
© 2022 - BYJU’S Future School.
Note: This document is the original copyright of BYJU’S Future School.
Please don't share, download or copy this file without permission..
36
Submitting the Activity - 1 Link:
As you complete the Circuit creation and Testing now to share the project link you need
to perform the below mentioned task.
37
d. As you copy the link, you will get the Wokwi link which contains the created
circuit design.
e. Now share and submit this copied link in the Student Dashboard panel against the
correct class number.
Step 4: You did great today as well. (Give at least 2 hats offs)
Wrap-Up Great! You have two hats off. Press the Hats Off Icon for
(5 mins) Creatively Solving Activities.
.
Press the Hats Off Icon for Great
Why did we use an ultrasonic Question.
sensor today?
38
We used an ultrasonic sensor
for finding the distance between the
sensor and object.
What is the formula to calculate Press the Hats Off Icon for
“Strong Concentration”.
distance?
39
Teacher Initiates Screen Share
Step 5: RGB LED WITH SENSOR Open the Project Solution link
Project and showcase the output.prin
Pointers and Goals of the Project:
Cues In today’s class we learned to
(5 mins) program for detecting the distance
between the sensor and the object
using an ultrasonic sensor.
Story:
Magnum lights is a light based
project company. They are doing
innovative works with lights for
homes, festivals, TV shows and so
on. They are in the research and
development stage and planning to
add an ultrasonic sensor on their
RGB LEDs such that when a person
is near to the RGB LED, RGB LEDs
should glow automatically. Can you
help them to create a circuit design
with code using ultrasonic sensor
and RGB LED ?
40
Good Luck!
For the solution of all the Additional Activity, open Teacher-Activity-3 and navigate to
class number C282
Additional Activity 1 -
Run Additional-Activity-1 from the panel.
The TASK and HINTS are mentioned on the website itself.
Additional Activity 2 -
Run Additional-Activity-2 from the panel.
The TASK and HINTS are mentioned on the website itself.
Additional Activity 3 -
Run Additional-Activity-3 from the panel.
The TASK and HINTS are mentioned on the website itself.
Additional Activity 4 -
Run Additional-Activity-4 from the panel.
The TASK and HINTS are mentioned on the website itself.
Additional Activity 5 -
Run Additional-Activity-5 from the panel.
The TASK and HINTS are mentioned on the website itself.
Teacher Clicks
41
xUMAmzkpICkqNqTjpd/pub
42
© 2022 - BYJU’S Future School.
Note: This document is the original copyright of BYJU’S Future School.
Please don't share, download or copy this file without permission..
43