0% found this document useful (0 votes)
6 views

Exp 4

Uploaded by

Armaan Armaan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Exp 4

Uploaded by

Armaan Armaan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 4
Student Name: Armaan UID: 22BCS10504
Branch: BE-CSE Section/Group: 607(B)
Semester: 5th Date of Performance: 18-08-24
Subject Name: IoT Architecture and Subject Code: 22CSP-329
its Protocols Lab

1. Aim:
To Formulate distance of an object using an ultrasonic sensor.

2. Objectives:
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.

3. Equipment Used:
• Arduino Uno R3 board
• Ultrasonic sensor (HC-SR04)
• Buzzer
• Jumper Wires

4. Procedure:
1. Circuit Setup:
• Connect the Echo pin of the HC-SR04 ultrasonic sensor to the D2 pin of
the Arduino Uno R3.
• Connect the Trig pin of the sensor to the D3 pin of the Arduino.
• Connect the VCC and GND pins of the sensor to the 5V and GND pins of
the Arduino, respectively.
• Connect the positive terminal of the buzzer to pin D11 of the Arduino, and
the negative terminal to the GND.
2. Code Upload:
• Open the Arduino IDE on your computer.
• Enter the provided code in the IDE.
• Navigate to Tools and select the appropriate board and port.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

• Verify and compile the code.


• Upload the code to the Arduino Uno R3 board.

5. Code:
const int trigPin = 3;
const int echoPin = 2;
const int buzzer = 11;
long duration;
int distance;
int safetyDistance;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzer, OUTPUT);
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;
safetyDistance = distance;
if (safetyDistance <= 5){
digitalWrite(buzzer, HIGH);
}
else{
digitalWrite(buzzer, LOW);
}
Serial.print("Distance: ");
Serial.println(distance);
}

6. Connections:
• Connect the Echo pin of the sensor to the D2 pin of the Arduino.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

• Connect the Trig pin of the sensor to the D3 pin of the Arduino.
• Navigate to Tools and select board and port.
• Verify and compile the code, then upload the code to the Arduino Uno R3
board.
• Monitor the output in the Serial monitor (Set the baud rate as 9600). To open
Serial monitor Tools>Serial Monitor or (Ctrl+Shift+M).

7. Result:

8. Learning Outcomes:
• Understand the working principle of an ultrasonic sensor.
• Learn how to interface an ultrasonic sensor with an Arduino.
• Develop skills in writing and uploading code to the Arduino board.
• Gain practical experience in measuring distances using sound waves.
• Learn how to trigger actions based on sensor input, such as activating a buzzer
when an object is too close.

9. Conclusion:
By successfully interfacing the ultrasonic sensor with the Arduino Uno R3, we
can measure the distance of an object without physical contact. The sensor
accurately detects distances and triggers a buzzer when the object is within a
specified safety distance, demonstrating the practical application of ultrasonic
sensors in real-world scenarios such as obstacle detection and collision avoidance
systems.

You might also like