0% found this document useful (0 votes)
14 views4 pages

Abhi 4

The document outlines an experiment to measure distance using an ultrasonic sensor with an Arduino setup. It details the aim, objectives, equipment, procedure, code, and results, demonstrating real-time distance measurement and display on an LCD. The experiment effectively integrates feedback mechanisms, including a proximity indicator and serial output.

Uploaded by

abhijeet16828
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views4 pages

Abhi 4

The document outlines an experiment to measure distance using an ultrasonic sensor with an Arduino setup. It details the aim, objectives, equipment, procedure, code, and results, demonstrating real-time distance measurement and display on an LCD. The experiment effectively integrates feedback mechanisms, including a proximity indicator and serial output.

Uploaded by

abhijeet16828
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

DEPARTMENT OF

COMPUTERSCIENCE&ENGINEERING

Experiment 4

Student Name:Harsh UID: 22BCS16864


Branch: BE-CSE Section/Group: 22BCS-IOT-642(A)
Semester: 5th Date of Performance: 22-08-24
Subject Name: IOT LAB Subject Code: 22CSP-329

1. Aim: To Formulate distance using ultrasonic sensor.

2. Objective: The objective of this experiment is to measure the distance to an object using an ultrasonic
sensor by emitting sound waves and calculating the time taken for the echo to return.

3. Input/Equipment Used:

a. 1×ArduinoUnoR3
b. 1×Ultrasonic Sensor (HC-SR04)
c. 16×2 LCD 12C Display
d. Jumper Wires

4. Procedure:

i. Connect Sensor: Attach VCC, GND, Trig, and Echo pins of the ultrasonic sensor to the
microcontroller.
ii. Trigger Pulse: Send a 10 µs pulse to the Trig pin to emit an ultrasonic wave. iii.
Measure Echo: Capture the time between the Echo pin going HIGH and LOW.
iv. Calculate Distance: Use the formula Distance=Time×3432\text{Distance} =
\frac{\text{Time} \times 343}{2}Distance=2Time×343 to determine the distance.
v. Display Result: Output the calculated distance to the desired interface. vi. Repeat:
Continuously repeat the measurement for real-time distance monitoring.

5. Connections:

Step 1: Power Connections

• Connect the VCC pin of the ultrasonic sensor to the 5V pin on the microcontroller.
• Connect the GND pin of the sensor to the GND pin on the microcontroller.

Step 2: Trigger Pin Connection

• Connect the Trig pin of the sensor to a digital output pin on the microcontroller (e.g., D2).
Step 3: Echo Pin Connection
DEPARTMENT OF
COMPUTERSCIENCE&ENGINEERING
• Connect the Echo pin of the sensor to a digital input pin on the microcontroller (e.g., D3).

Step 4: Verify Connections

• Double-check all connections to ensure they are secure and correct according to your
microcontroller’s pin configuration.

6. CODE:
#include <LiquidCrystal.h>

#define TRIG_PIN 18 // Trigger pin connected to digital pin 18


#define ECHO_PIN 19 // Echo pin connected to digital pin 19
#define LED_PIN LED_BUILTIN // Built-in LED

LiquidCrystal lcd(2, 3, 4, 5, 6, 7); // Initialize the LCD with the appropriate pins

void setup() {
Serial.begin(115200);
lcd.begin(16, 2);

pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);

lcd.print("Ultra sonic");
lcd.setCursor(0, 1);
lcd.print("Distance Meter");
delay(2000); lcd.clear();
lcd.print(" Circuit Digest");
delay(2000); lcd.clear();
}

float readDistanceCM() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);

float duration = pulseIn(ECHO_PIN,


HIGH); float distanceCM = duration * 0.034
/ 2; return distanceCM;
}
DEPARTMENT OF
COMPUTERSCIENCE&ENGINEERING
void loop() { float distanceCM =
readDistanceCM();
bool isNearby = distanceCM < 100;

// LED indication
digitalWrite(LED_PIN, isNearby);

// Serial Monitor output


Serial.print("Target Distance: ");
Serial.print(distanceCM);
Serial.println(" cm");

// LCD display output


lcd.clear();
lcd.print("Distance: ");
lcd.print(distanceCM);
lcd.print(" cm");

lcd.setCursor(0, 1);
lcd.print("Distance: ");
lcd.print(distanceCM / 100);
lcd.print(" m");

delay(1000); // Wait 1 second before the next


measurement }

7. Result:

8. Conclusion:The experiment effectively demonstrates distance measurement using an ultrasonic sensor.


By integrating an LCD display and Serial Monitor output, it provides clear and immediate feedback on
distance in both centimeters and meters. The built-in LED serves as a proximity indicator, highlighting
DEPARTMENT OF
COMPUTERSCIENCE&ENGINEERING
when an object is within 100 cm. This setup showcases the ability to measure and display distance in
real-time, making it suitable for various applications in monitoring and automation.

You might also like