0% found this document useful (0 votes)
17 views8 pages

Exp-4 HISI

The document describes an Arduino code that uses an ultrasonic sensor to measure distance. It sends a pulse through one pin, listens for the echo on another pin, and calculates distance based on the time for the pulse to return. If the duration exceeds a threshold it prints that it is out of range, otherwise it prints the calculated distance.
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)
17 views8 pages

Exp-4 HISI

The document describes an Arduino code that uses an ultrasonic sensor to measure distance. It sends a pulse through one pin, listens for the echo on another pin, and calculates distance based on the time for the pulse to return. If the duration exceeds a threshold it prints that it is out of range, otherwise it prints the calculated distance.
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/ 8

Date of Conduction-31/01/2024

Date of Submission-25/04/2024
Name-SHIVPRIYA GUPTA
PRN-22070123111

Entc-B2 Experiment-4

Title:

Ultrasonic Distance Sensor Measurement with Arduino

AIM:
To measure the distance using an ultrasonic sensor and output the distance reading via the
Serial monitor, while indicating "Out range" if the measured distance exceeds a
certain threshold.

SOFTWARE AND HARDWARE REQUIRED:


Software: Arduino IDE
Hardware: Arduino board, HC-SR04 ultrasonic sensor

THEORY:
Function Used:
This Arduino code is designed to measure distance using an ultrasonic sensor (specifically,
an HC-SR04 ultrasonic sensor). Here's a breakdown of the functions used in the code:
1. setup() Function:
● Configures the trigPin as an output pin to send ultrasonic waves and the
echoPin as an input pin to receive the reflected waves.
● Initializes the Serial communication at a baud rate of 9600.
2. loop() Function:
● Sends a short LOW pulse to the trigPin to ensure a clean signal.
● Delays for 2 microseconds to ensure stability.
● Sends a HIGH pulse to the trigPin for 10 microseconds to trigger the ultrasonic
sensor to send out a sound wave.
● Sends another LOW pulse to the trigPin to complete the signal.
● Uses the pulseIn() function to measure the duration it takes for the ultrasonic
wave to travel to the object and back.
● Calculates the distance using the formula:
● Distance = (Duration of the pulse * Speed of sound in air) / 2
● Speed of sound in air is approximately 0.034 cm per microsecond
(or 34,000 cm/s).
● If the calculated distance is greater than or equal to 38000 microseconds,
it prints "Out range" on the Serial monitor.
● Otherwise, it prints the calculated distance on the Serial monitor.
This code allows continuous measurement of distance using the ultrasonic sensor. It uses the
pulse duration to calculate the distance based on the speed of sound in air and the time it takes
for the sound wave to travel to the object and back. The distance is then displayed on the Serial
monitor.

Sensor Information:
An ultrasonic sensor is a device that utilizes ultrasonic sound waves to measure distance to an
object. Here's some detailed information about ultrasonic sensors:
1. Principle of Operation:
● Ultrasonic sensors work on the principle of emitting ultrasonic sound waves
and measuring the time it takes for the sound waves to bounce back after
hitting an object.
● They typically consist of a transmitter and a receiver.
● The transmitter emits ultrasonic waves, which travel through the air.
● When these waves encounter an object, they bounce back to the
sensor's receiver.
● The sensor calculates the time taken for the waves to return and uses it
to determine the distance to the object.
2. Components:
● Transmitter: Emits ultrasonic sound waves.
● Receiver: Detects the reflected sound waves.
● Control Circuitry: Processes the received signal and calculates the distance.
3. Key Features:
● Non-contact Measurement: Ultrasonic sensors measure distance without
physical contact with the object, making them suitable for various
applications where contact sensors might not be feasible.
● Wide Range: They can typically measure distances ranging from a few
centimeters to several meters, depending on the model and specifications.
● High Accuracy: Ultrasonic sensors offer high accuracy in distance
measurements, especially in controlled environments.
● Easy Integration: They are relatively easy to integrate with microcontrollers
like Arduino and Raspberry Pi, often requiring minimal external components.
4. Applications:
● Distance Measurement: Used in robotics, industrial automation, and
automotive applications for obstacle detection, collision avoidance, and
navigation.
● Liquid Level Measurement: Ultrasonic sensors can measure the level of liquids
in tanks and containers.
● Parking Assistance Systems: They are used in automotive parking assistance
systems to detect obstacles during parking maneuvers.
● Object Detection: Used in security systems and access control systems to
detect the presence of objects or individuals.
5. Considerations:
● Environmental Factors: Environmental conditions such as temperature,
humidity, and air turbulence can affect the performance of ultrasonic sensors.
● Angle of Detection: Ultrasonic sensors typically have a cone-shaped
detection area, and the angle of detection can affect their accuracy and
range.
● Material and Surface: The material and surface characteristics of the
object being detected can influence the accuracy of distance
measurements.
Overall, ultrasonic sensors are versatile devices widely used in various industries for
distance measurement and object detection applications due to their reliability, accuracy,
and ease of use.
Signal Conditioning Circuit for ULTRASONIC Sensor:
Signal conditioning refers to the process of manipulating an input signal to produce a desired
output signal that meets specific criteria, such as noise reduction, amplification, filtering, or
transformation. In the context of the provided code, which is for an ultrasonic distance sensor,
the signal conditioning steps involve preparing the signal from the sensor for accurate distance
measurement.
Here's a breakdown of the signal conditioning steps in the code:
1. Triggering the Sensor (Trig Pin):
● The trigPin (pin 9) is set as an output pin. When the ultrasonic sensor needs to
take a distance measurement, it sends out a pulse by setting the trigPin to
HIGH for a brief period (10 microseconds), then sets it back to LOW.
2. Receiving the Echo (Echo Pin):
● The echoPin (pin 10) is set as an input pin. This pin receives the echo pulse
from the ultrasonic sensor after it has transmitted the signal and it has bounced
back from an object.
3. Measuring the Duration of the Echo:
● After triggering the sensor, the Arduino waits for the echo to return.
● The pulseIn() function is used to measure the duration (in microseconds) for
which the echoPin remains HIGH. This duration corresponds to the time it
takes for the ultrasonic pulse to travel to the object and back.
4. Calculating Distance:
The duration value obtained from pulseIn() represents the time taken for the pulse to travel to
the object and back.
Since the speed of sound in air is approximately 343 meters per second (or 0.034
cm/microsecond), the distance to the object can be calculated using the formula: distance =
(duration * 0.034) / 2.
The division by 2 is because the pulse travels to the object and back, so the total distance is half
of the actual distance traveled by the pulse.
Output Display:
If the duration exceeds a certain threshold (38,000 microseconds), it is considered out of range,
and "Out of range" is printed to the Serial monitor.
Otherwise, the calculated distance is printed to the Serial monitor.
In summary, the signal conditioning in this code involves accurately measuring the duration of
the ultrasonic pulse's round trip and converting it into a distance measurement using the speed
of sound constant. The code also includes a condition to handle out-of-range situations.

SOURCE CODE:

const int trigPin = 9;


const int echoPin = 10;
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);
if(duration>=38000)
{
Serial.println("Out range");
}
else
{
distance = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.println(distance);
}
}
INPUT SCREENSHOT:
OUTPUT:
RESULT:

CONCLUSION:
This Arduino code utilizes an ultrasonic sensor to measure distance. It sends a pulse
through the trigPin, listens for the return echo on the echoPin, and calculates the distance
based on the time taken for the pulse to return. If the duration exceeds a certain threshold
(38,000 microseconds), it indicates that the measured distance is out of range. Otherwise,
it calculates the distance using the formula and prints it to the Serial monitor. The code
provides a simple way to interface with ultrasonic sensors for distance measurement
applications.

You might also like