What is Ultrasonic Sensor
An ultrasonic sensor uses ultrasonic sound waves to detect the distance to an object.
An ultrasonic sensor uses a transducer to emit and receive ultrasonic pulses, which provide information about an object's proximity.
High-frequency sound waves reflect from boundaries to produce distinct echo patterns.
Take a look at our straightforward blog on connecting ultrasonic sensor with Arduino. Whether you're new to this or already have some experience, this guide will make things clear for you to understand ultrasonic sensor interfacing with Arduino.
Also, read our blog on Ultrasonic Sensor Working Principle detailing the principles and workings of these sensors, their applications, and how are ultrasonic sensors used.
How does an Ultrasonic Distance Sensor work?
The ultrasonic sensor (or transducer) works on the same principles as a radar system. An ultrasonic sensor can convert electrical energy into acoustic waves and vice versa.
The acoustic wave signal is an ultrasonic wave traveling at a frequency above 18kHz. The HC-SR04 ultrasonic sensor generates ultrasonic waves at 40kHz frequency, which is much higher than the audible range of human hearing.
The generated ultrasonic waves travel in the air to the target and reflect back to the ultrasonic sensor.
Typically, a microcontroller like Arduino is used for communication with an ultrasonic sensor. To measure the distance to an object, the microcontroller sends a trigger signal to the ultrasonic sensor.
The duty cycle of this trigger signal is 10”S for the HC-SR04 ultrasonic sensor. When triggered, the ultrasonic sensor generates eight acoustic (ultrasonic) wave bursts and initiates a time counter. The transmitted sound wave travels in the air and reaches the target object.
The sound waves are reflected back to the ultrasonic sensorâs receiver part. As soon as the reflected (echo) signal is received, the timer stops. The output of the ultrasonic sensor is a high pulse with the same duration as the time difference between transmitted ultrasonic bursts and the received echo signal.
Â
Read our blog smart dustbin project using Arduino detailing Arduino code for making a smart dustbin, the required components, and the circuit diagram for making an automatic dustbin project.
read more :Â Arduino Sensor types and Applications
Â
Hardware required:
- Arduino - 1 (You can use any Arduino board for this project)
- HC-SR04 Ultrasonic sensor - Â 1
- Jumper cables - As required
Arduino Uno Pin Diagram:
Ultrasonic Sensor Pin Diagram:
Connection Diagram to interfacing ultrasonic sensor with Arduino
Below are the connection diagram and the code for interfacing the Ultrasonic sensor with Arduino and measuring the distance.
Connections:
Arduino side | Â Ultrasonic sensor side |
5V pin | VCC pin |
GND pin | GND pin |
GPIO 6 | ECHO pin |
GPIO 7 | TRIG pin |
Â
Make all the connections as per the diagram above to interfacing ultrasonic sensor with Arduino.
Â
read more :Â Top 10 Arduino Projects to Get Started With
Â
Code
#define echoPin 6
#define trigPin 7
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);
distance = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
}
The above code is used to measure distance using Ultrasoinc sensor and Arduino. This code prints the distance on the serial monitor.
Steps to Interface the Ultrasonic Sensor with Arduino UNO
Step 1: Make connections as per the circuit diagram given above.
Step 2:Â Open Arduino IDE and type the above code.
Step 3: Using a USB cable, connect your Arduino board to the computer.
Step 4: Select the correct port and the board you are using.
For selecting a Board: Go to Tools >> Boards >> Arduino AVR Boards >> Arduino UNO
For selecting Port: Go to Tools >> Ports >> port to which the Arduino is connected. You can make sure by checking in the device manager.
read more :Â Arduino Uno Pin Diagram: A Complete Guide
Step 5: Once, the board and the Port are selected, click on the upload icon. Once the code gets uploaded successfully, you can see the distance measured by the ultrasonic sensor on the serial monitor.
Â
Code explanation
#define echoPin
#define trigPin
long duration;
int distance;
First, we used 2 preprocessor components for assigning the pins of Arduino to connect the Ultrasonic sensor.
GPIO pin 6 is assigned to read the ECHO signal from the ultrasonic sensor and the GPIO 7 pin is assigned to give a triggering signal to the ultrasonic sensor.
And we have made 2 variables called âdurationâ and the âdistanceâ of long and int datatype respectively, which hold the duration of the pulse and the distance calculated.
void setup()
{
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
Next, we have a âvoid setup()â function, this function executes only once in our program.
In this function, we are configuring the TRIG and ECHO pin as OUTPUT and INPUT respectively using a âpinMode();â function.
void loop()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
}
Next, we have the âvoid loop()â function. This function executes infinite times, which means executes forever unless purposefully interrupted.
In this function we make the TRIG pin HIGH for 10 microseconds and LOW for 2 microseconds by using the function âdigitalWrite();â function and the âdelayMicroseconds();â function.
The duration is measured by using a âpulseIn();â function which measures the duration of the ECHO pulse. Next, the distance is measured using the below equation and stored in a âdistanceâ variable.
distance = duration * 0.034 / 2;
Then the distance measured in centimeters is printed on the serial monitor. By using a âSerial.print();â function.
Â
read more :Â IR Sensor Interfacing with Arduino
Conclusion
This blog post shows how to connect an ultrasonic sensor to an Arduino and use it to identify obstacles and compute distance.
These sensors employ inaudible sound waves to accurately measure distances and provide data for a range of applications.
Ultrasonic sensors are gaining popularity among professionals and amateurs because of their low cost, precision, and durability.
By following the directions in this article, you may quickly connect an Arduino Uno to an ultrasonic sensor using the provided code and begin building your own projects.
So why do you wait? Take immediate advantage of ultrasonic sensors to maximize the potential of your Arduino creations!
Â
If you appreciate our work don't forget to share this post and leave your opinion in the comment box.
Â
Please do check out other blog posts about Interfacing ACS712 with Arduino , LED Interfacing with Arduino , Interfacing GSM Module with Arduino , Interfacing MAX30100 Pulse Oximeter with Arduino , IR Sensor Interfacing with Arduino , How to connect ZMPT101B to Arduino and  How to use Buzzer with Arduino.
Â
Make sure you check out our wide range of products and collections (we offer some exciting deals!)