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

Final Notes For Ultrasonic Sensor

This document describes interfacing an ultrasonic sensor with an Arduino Uno board to measure distance. It explains how ultrasonic sensors work using sound waves to determine object proximity. The hardware connections between the sensor and Arduino are outlined. The code measures distance using pulse timing from the sensor's trigger and echo pins, displaying results on the serial monitor. An LCD can also be added to display readings.

Uploaded by

Liado Jiu
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)
109 views

Final Notes For Ultrasonic Sensor

This document describes interfacing an ultrasonic sensor with an Arduino Uno board to measure distance. It explains how ultrasonic sensors work using sound waves to determine object proximity. The hardware connections between the sensor and Arduino are outlined. The code measures distance using pulse timing from the sensor's trigger and echo pins, displaying results on the serial monitor. An LCD can also be added to display readings.

Uploaded by

Liado Jiu
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/ 13

Excellence | Service | Leadership and Good Governance | Innovation | Social Responsibility | Integrity | Professionalism | Sp irituality

CPE 315L – Microprocessor


(Final Term Coverage)

Interfacing Ultrasonic Sensor with Arduino Uno

Activity #8: Application of Ultrasonic Sensor

An ultrasonic sensor is an instrument that measures the distance to an object using ultrasonic
sound waves. An ultrasonic sensor uses a transducer to send and receive ultrasonic pulses
that relay back information about an object's proximity.

In this tutorial, we'll guide you through interfacing an Arduino with an ultrasonic sensor to
measure distance and show it on the Serial monitor. We'll start by explaining how to interface
the ultrasonic sensor, and then we'll add the I2C LCD display to the project at the end.
Excellence | Service | Leadership and Good Governance | Innovation | Social Responsibility | Integrity | Professionalism | Sp irituality

Hardware
(Arduino & Ultrasonic Sensor)

IR Sensor Pinout

The IR sensor has a 3-pin connector that interfaces it to the outside world. The connections
are as follows:

VCC - The supply pin of the HC-SR04 Module.

GND - This is the Ground pin of the module

Trig - This is the trigger pin of the HC-SR04 module

Echo - This is the echo pin of the HC-SR04 sensor module.


Excellence | Service | Leadership and Good Governance | Innovation | Social Responsibility | Integrity | Professionalism | Sp irituality

Working of Ultrasonic Sensor

An ultrasonic sensor comprises several essential components that work together to measure
distances or detect objects using high-frequency sound waves. At its core is the transducer,
typically made of a piezoelectric crystal, which both emits and receives ultrasonic waves.

The sensor's operation begins with the ultrasonic transmitter, which sends out bursts of high-
frequency sound waves into the environment.

On the back of the Ultrasonic sensor, we have the MAX232 sensor on right side, which
converts the electrical signal received from the trig pin. Converts it into ultrasonic pulse and
send it’s through the transmitter side.

These waves bounce off objects in their path and are detected by the ultrasonic receiver. The
two LM324 ICs on the left side take this ultrasonic pulse converts it into electrical signal and
sends it to the echo pin.

The receiver converts the reflected waves into electrical signals.


Excellence | Service | Leadership and Good Governance | Innovation | Social Responsibility | Integrity | Professionalism | Sp irituality
Excellence | Service | Leadership and Good Governance | Innovation | Social Responsibility | Integrity | Professionalism | Sp irituality
Excellence | Service | Leadership and Good Governance | Innovation | Social Responsibility | Integrity | Professionalism | Sp irituality

The time it takes for the sound waves to travel to the object and back is precisely measured
by signal processing circuitry within the sensor. This time measurement is crucial for
calculating distances and determining the presence of objects in proximity to the sensor. Now
to calculate the distance we will use the following formula.

Speed= Distance x Time

Now we know the speed of sound is 343.2 m/s. To calculate the distance we need to convert
the speed of sound to cm/us which will be 0.03432 cm/uS, and we also know the time value
received from the ultrasonic sensor which is 500 uS.

Distance= (0.03432 cm/us x 700uS) / 2 = 8.58 cm

Hence, we know that the object is 8.58 cm away from the sensor.
Excellence | Service | Leadership and Good Governance | Innovation | Social Responsibility | Integrity | Professionalism | Sp irituality

Commonly asked questions about Ultrasonic Sensor

What is the maximum range of an ultrasonic sensor?

The maximum range of an ultrasonic sensor can vary depending on the specific model and
design, but typical ranges for standard sensors are between 2 centimeters (0.79 inches) to
several meters (tens of feet). Specialized sensors can have longer ranges for specific
applications.

Are ultrasonic sensors affected by environmental conditions?

Yes, ultrasonic sensors can be affected by environmental factors like temperature, humidity,
and air pressure. Changes in these conditions can alter the speed of sound, potentially
affecting the accuracy of distance measurements.

Can ultrasonic sensors detect multiple objects simultaneously?

Yes, ultrasonic sensors can detect multiple objects as long as they are within the sensor's
detection range. However, they may struggle to distinguish between closely spaced objects if
their echoes overlap.

Circuit Diagram of Arduino Interfacing with Ultrasonic Sensor

To Interface the Ultrasonic Sensor to the Arduino. You need to connect the sensor's TRIG
(trigger) pin to Arduino pin 4. The sensor's ECHO pin to Arduino pin 5. Sensor’s VCC (power)
pin to Arduino's 5V output and sensor's GND (ground) pin to Arduino's GND.
Excellence | Service | Leadership and Good Governance | Innovation | Social Responsibility | Integrity | Professionalism | Sp irituality

Programming
(Arduino IDE/C++)

Arduino Code for Interfacing Ultrasonic Sensor with Arduino

Here is the complete line by line code explanation for Interfacing Arduino with Ultrasonic
Sensor. The complete code can be found at the absolute bottom of the blog.

const int trig=4;


const int echo=5;

These lines define two constants, trigPin and echoPin, to specify the Arduino pins to which
the trigger and echo pins of the ultrasonic sensor are connected, respectively.

long duration;
long distance;

Here, two variables, duration (of type long) and distance (of type int), are declared. These
variables will be used to store the duration of the echo pulse and the calculated distance,
respectively.

void setup() {
Serial.begin(9600);
}

In the setup() function, the Arduino initializes serial communication at a baud rate of 9600 bits
per second. This is used to communicate with the Serial Monitor on your computer, where
you'll see the distance measurements displayed.

pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

These lines set the trigPin as an OUTPUT and the echoPin as an INPUT. This configuration
is necessary for controlling the ultrasonic sensor. The trigger pin (TRIG) sends a signal, while
the echo pin (ECHO) receives the returning signal.
Excellence | Service | Leadership and Good Governance | Innovation | Social Responsibility | Integrity | Professionalism | Sp irituality

void loop() {
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

Inside the loop() function, these lines initiate a measurement from the ultrasonic sensor. It first
sets the trigger pin (TRIG) to LOW, waits for a brief period using delayMicroseconds() to
ensure a clean trigger signal, then sets it to HIGH for 10 microseconds before setting it back
to LOW. This sequence triggers the ultrasonic sensor to send out an ultrasonic pulse.

duration = pulseIn(echoPin, HIGH);

This line uses the pulseIn() function to measure the duration (in microseconds) of the pulse
received on the echo pin (ECHO). It waits until the ECHO pin goes HIGH and then waits again
until it goes LOW, measuring the time between those events.

distance=duration*0.034/2;

Here, the code calculates the distance based on the duration of the echo pulse. The formula
used here is based on the speed of sound in air (approximately 0.034 centimeters per
microsecond) and the fact that the pulse travels to the object and back. Dividing by 2 accounts
for the two-way trip.

Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");

These lines print the calculated distance to the Serial Monitor with a descriptive label
("Distance: ") followed by the actual distance value and "cm" for centimeters.
Excellence | Service | Leadership and Good Governance | Innovation | Social Responsibility | Integrity | Professionalism | Sp irituality

delay(1000);

Finally, a delay of 1000 milliseconds (1 second) is added to control the rate at which distance
measurements are taken. You can adjust this delay to change the measurement frequency as
per your requirements.

Circuit Diagram for Arduino Interfacing with Ultrasonic Sensor and 16x2 LCD

Connect the ultrasonic sensor to the Arduino as mentioned earlier (TRIG to pin 4, ECHO to
pin 5).

Connect the I2C LCD to the Arduino:

SDA to A4 (for Arduino Uno)

SCL to A5 (for Arduino Uno)

VCC to 5V

GND to GND
Excellence | Service | Leadership and Good Governance | Innovation | Social Responsibility | Integrity | Professionalism | Sp irituality

Code for Interfacing Arduino with Ultrasonic Sensor and 16x2 LCD

Ensure you have the necessary libraries installed for the I2C LCD. You can use the
"LiquidCrystal_I2C" library for this purpose. You can install it via the Arduino Library Manager
too
Excellence | Service | Leadership and Good Governance | Innovation | Social Responsibility | Integrity | Professionalism | Sp irituality

Here’s the complete code for Arduino, Ultrasonic Sensor and LCD interfacing which you can
copy paste and upload on your Arduino Board.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
const int trig=4;
const int echo=5;
void setup() {
// put your setup code here, to run once:
lcd.begin();
lcd.backlight();
lcd.print("Distance : ");
pinMode(trig,OUTPUT);
pinMode(echo,INPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(trig,LOW);
delay(2);
digitalWrite(trig,HIGH);
delay(10);
digitalWrite(trig,LOW);
long duration = pulseIn(echo,HIGH);
long distance=duration*0.034/2; //speed of sound=340 m/s =0.034
cm/microsecond.
lcd.setCursor(10,0);
lcd.print(" ");
lcd.setCursor(10,0);
lcd.print(distance);
Serial.print("Distance : ");
Serial.println(distance);
Serial.print(" cm")
delay(10);
}
Excellence | Service | Leadership and Good Governance | Innovation | Social Responsibility | Integrity | Professionalism | Sp irituality

Reference

• https://circuitdigest.com/microcontroller-projects/interface-arduino-with-ultrasonic-sensor

You might also like