DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Experiment - 4
Student Name: Tauheed Ansari UID: 22BCS15608
Branch: BE CSE Section/Group: 621 -B
Semester: 5th Date of Performance: 20/8/24
Subject Name: IOT LAB Subject Code: 22CSP-329
1. Aim
To Formulate distance of an object using an ultrasonic sensor.
2. Objective
a. Learn about interfacing.
b. Learn about IoT programming.
3. Hardware Required:
1 × Arduino Uno R3
1 × Ultrasonic Sensor (HC-SR04)
4 × Jumper Wires
4. Introduction:
Arduino:
It is an open-source electronics platform. It consists ATmega328 8-bit Micro controller. It
can be able to read inputs from different sensors & we can send instructions to the micro
controller in the Arduino. It provides Arduino IDE to write code & connect the hardware
devices like Arduino boards & sensors.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Ultrasonic Sensor:
An ultrasonic Sensor is a device used to measure the distance between the sensor and an
object without physical contact. This device works based on time-to-distance conversion.
Working Principle of Ultrasonic Sensor:
Ultrasonic sensors measure distance by sending and receiving the ultrasonic wave. The
ultrasonic sensor has a sender to emit the ultrasonic waves and a receiver to receive the
ultrasonic waves. The transmitted ultrasonic wave travels through the air and is reflected by
hitting the Object. Arduino calculates the time taken by the ultrasonic pulse wave to reach the
receiver fromthe sender.
We know that the speed of sound in air is nearly 344 m/s,
So, the known parameters are time and speed (constant). Using these parameters, we can
calculate the distance traveled by the sound wave.
Formula: Distance = Speed * Time
In the code, the “duration” variable stores the time taken by the sound wave traveling from
the emitter to the receiver. That is double the time to reach the object, whereas the sensor
returns thetotal time including sender to object and object to receiver. Then, the time taken to
reach the object is half of the time taken to reach the receiver.
So, we can write the expression as,
Distance = Speed of Sound in Air * (Time Taken / 2)
Note: Speed of sound in air = 344 m/s.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Circuit Diagram:
Figure: Ultrasonic Distance measurement circuit
Setup:
1. Connect the Echo pin of the sensor to the D4 pin of the Arduino.
2. Connect the Trig pin of the sensor to the D2 pin of the Arduino.
3. Navigate to Tools and select board and port.
4. Verify and compile the code, then upload the code to the coding area.
5. Monitor the output in the Serial monitor (Set the baud rate as 9600). To open Serial
monitor:Code Serial Monitor arrow on the bottom of page.
5. Implementation/ Code
# define echoPin 4
# define trigPin 2
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
long duration;
int distance;
void setup(){
pinMode(trigPin,OUTPUT);
pinMode(echoPin,INPUT);
Serial.begin(9600);
}
void loop(){
digitalWrite(trigPin,LOW);
delayMicroseconds(2);
digitalWrite(trigPin,HIGH);
delayMicroseconds(10);
digitalWrite(trigPin,LOW);
duration=pulseIn(echoPin,HIGH);
distance = duration * 0.034/2;
Serial.print("Distance ");
Serial.print(distance);
Serial.println(" cm");
}
Figure: Distance Measurement
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
6. Learning Outcomes:
Circuit Design
Simulation Proficiency
Usage of Ultrasonic Sensor