Puru 4 IOT
Puru 4 IOT
Experiment 4
Student Name: Purushottam Kumar UID:22BCS10794
Branch: BE-CSE Section/Group: 608/B
th
Semester: 5 Date of Performance: 16/8/24
Subject Name: IOT Architecture and Protocol Lab
Subject Code: 22CSP-329
2. Objective:
• Learn about Distance Calculations using Sensor
• Learn about IOT programming
3. Equipment Used:
• Breadboard
• Tinker Cad
• Ultrasonic sensor (HC-SR04)
• Arduino Uno R3
• Jumper Wires
4. Theory:
Ultrasonic Sensor: It 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.
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 from the sender.
5. Procedure:
• Connect the Ultrasonic Sensor to the Arduino:
• Connect the VCC pin of the ultrasonic sensor to the 5V pin on the Arduino.
• Connect the GND pin of the ultrasonic sensor to the GND pin on the
Arduino.
• Connect the Trig pin (Pin 5 in code) of the sensor to digital pin 5 on the
Arduino.
• Connect the Echo pin (Pin 4 in code) of the sensor to digital pin 4 on the
Arduino.
• The Arduino will calculate the distance based on the time and display it on the
Serial Monitor in centimeters.
6. Code:
# define echoPin 4
# define trigPin 5
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");
delay(1000);
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
7. Result-
This demonstrates that the connections between the Arduino and the Ultrasonic
Sensor are correct and the Arduino code is functioning as intended.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
8. Learning Outcomes:
9. Conclusion: