Final Notes For Ultrasonic Sensor
Final Notes For 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:
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 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.
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.
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
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.
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.
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.
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++)
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.
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.
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).
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