
Arduino Interfacing with Ultrasonic Sensor
What isΒ 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. Β
High-frequency sound waves reflect from boundaries to produce distinct echo patterns.
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 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
Β
Connection Diagram to interface the Ultrasonic sensor with Arduino UNO
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.
Β
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 6
#defineΒ trigPin 7
Β
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
In this blog post, we've learned that ultrasonic sensors are an essential component in distance measurement and obstacle detection. By using sound waves beyond the range of human hearing, these sensors can accurately detect distances and provide data for a wide range of applications. Thanks to their reliability, accuracy, and affordability, ultrasonic sensors are becoming increasingly popular among hobbyists and professionals alike. By following the steps we've outlined in this article and using the provided code, you'll be able to easily interface an ultrasonic sensor with an Arduino Uno and start building your own projects. So, what are you waiting for? Get started with ultrasonic sensors today and unlock the potential of your Arduino projects!
Β
Β
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!)
Frequently Asked Questions
1. How does the ultrasonic sensor communicate with Arduino?
The Ultrasonic sensor sends out a pulse to Arduino. Using the duration of this pulse, the distance is calculated.
read more : Interfacing MAX30100 Pulse Oximeter with Arduino
2. How does Arduino code connect to the ultrasonic sensor?
In Arduino code, we give instructions to Arduino to perform some tasks. The Arduino code for the Ultrasonics sensor is used to tell Arduino to read the ultrasonic sensor output and calculate the distance.
3. Does the ultrasonic sensor use PWM?
No, the ultrasonic sensor does not use PWM. It just gives a pulse with a particular time duration.
read more : How 433MHz RF Module Works & Interfacing With Arduino
4. What is the maximum range of the ultrasonic sensor?
The ultrasonic sensor measures the distance anywhere between 2 cm to 400 cm.
5. Do ultrasonic sensors interfere with each other?
It depends on how the ultrasonic sensor is placed. If the ultrasonic sensors are placed opposite to each other, then they may interfere.
6. What can ultrasonic sensors be used for?
The ultrasonic sensors are used to measure the distance to an object ranging from 2 cm to 400 cms.
read more : Interfacing Proximity Sensors with Arduino
Components and Supplies
Frequently Asked Questions
1. How does the ultrasonic sensor communicate with Arduino?
The Ultrasonic sensor sends out a pulse to Arduino. Using the duration of this pulse, the distance is calculated.
read more : Interfacing MAX30100 Pulse Oximeter with Arduino
2. How does Arduino code connect to the ultrasonic sensor?
In Arduino code, we give instructions to Arduino to perform some tasks. The Arduino code for the Ultrasonics sensor is used to tell Arduino to read the ultrasonic sensor output and calculate the distance.
3. Does the ultrasonic sensor use PWM?
No, the ultrasonic sensor does not use PWM. It just gives a pulse with a particular time duration.
read more : How 433MHz RF Module Works & Interfacing With Arduino
4. What is the maximum range of the ultrasonic sensor?
The ultrasonic sensor measures the distance anywhere between 2 cm to 400 cm.
5. Do ultrasonic sensors interfere with each other?
It depends on how the ultrasonic sensor is placed. If the ultrasonic sensors are placed opposite to each other, then they may interfere.
6. What can ultrasonic sensors be used for?
The ultrasonic sensors are used to measure the distance to an object ranging from 2 cm to 400 cms.
read more : Interfacing Proximity Sensors with Arduino