Arduino Projects

Interface HC-SR04 Ultrasonic Sensor With Arduino


In this project, we will use an Ultrasonic Sensor With Arduino to measure the distance to an object. the ultrasonic sensor is a device that can measure the distance to an object by using sound waves. It is also known as a transducer or sonar. Ultrasonic sensors are used in a wide variety of applications, such as measuring the distance to a parking spot in a car or detecting objects in a factory.

In this project, we will use an ultrasonic sensor with Arduino to make an easy distance measurement device. This device will use the ultrasonic sensor to measure the distance to an object, and will then display the distance on the Serial monitor. Also, Check this  What is Sensor? Different Types of Sensor & Their Uses.

Thank You NextPCB:

This project is successfully finished because of the service and support from NextPCB. NextPCB is one of the most professional PCB manufacturers in Global and has specialized in the PCB and assembly industry for over 16 years. Not only could NextPCB deliver the most creative printed circuit boards and assembly technologies in the most elevated quality standards, but the fastest delivery turnaround was also as fast as 24 hours.

Guys if you have a PCB project, please visit their website and get exciting discounts and coupons. if you sign-up using this link you will get free coupons worth $100. Checkout out NextPCB now and get amazing discounts on your first order.


Required Components

To get started, you will need the following materials:

  • Arduino Uno
  • Ultrasonic sensor
  • Breadboard
  • Jumper wires
  • 9V power supply

What is an Ultrasonic Sensor?

HC_SR04 ultrasonic sensor working

An ultrasonic sensor is a device that uses sound waves to measure distance. It can be used to measure the distance between two objects or to measure the thickness of a material. Ultrasonic sensors are used in a variety of applications, including automotive collision avoidance systems, security systems, and industrial process control.


How does an Ultrasonic Sensor work?

ultrasonic sensor working

An ultrasonic sensor is a device that emits sound waves at a frequency too high. When these waves hit a thing, they bounce back and are detected by the sensor. The time it takes for the waves to bounce back is used to calculate the distance to the object.

used in devices such as parking sensors, level sensors, and flow sensors, including obstacle detection, They are also used in some medical applications, such as ultrasound imaging.

Learn more about Ultrasonic sensors:

HC-SR04 Ultrasonic Sensor

ultrasonic sensor

The HC-SR04 is an ultrasonic sensor that can be used to measure distance. It is often used in robotics and automation applications. The sensor works by emitting a sound wave and measuring the time it takes for the sound wave to bounce back. The distance to the object can then be calculated based on the time it took for the sound wave to return. The HC-SR04 sensor is fairly easy to use and is usually used in DIY projects. It is also relatively affordable, making it a popular choice for multiple applications. The sensor is rather accurate and can be used to calculate distances up to 400 cm.

Here are the complete HC-SR04 ultrasonic sensor specifications:

Operating Voltage DC 5V
Operating Current 15mA
Operating Frequency 40KHz
Max Range 4m
Min Range 2cm
Ranging Accuracy 3mm
Measuring Angle 15 degree
Trigger Input Signal 10µS TTL pulse
Dimension 45 x 20 x 15mm

HC-SR04 Ultrasonic Sensor Pinout

ultrasonic sensor with Arduino pinout

The ultrasonic sensor has four pins:

  1. VCC Pin – positive terminal of the source should be connected.
  2. GND Pin – negative or ground terminal of the source should be connected.
  3. TRIG Pin – Acts as an output that emits the Ultrasonic Sound waves.
  4. ECHO Pin – Acts as an input that receives the reflected Ultrasonic Sound waves.

Ultrasonic sensor Arduino connection

Ultrasonic sensor Arduino connection

This sensor is perfect for any project that requires accurate distance measurement. The sensor can be connected to an Arduino using only a few wires. First, let’s connect the ultrasonic sensor to the Arduino.

Ultrasonic sensor And Arduino Connections:

  1. Vcc Pin______5V
  2. Trig Pin_____D9
  3. Echo Pin____D8
  4. GND Pin_____GND

Connect the Vcc pin to the 5V power supply pin on the Arduino. Connect the Trig pin to digital pin 9 on the Arduino. Connect the Echo pin to digital pin 8 on the Arduino. Finally, connect the GND pin to GND

Source Code

Now that the sensor is connected, we can write code to measure the distance to an object. The code is very simple.

Here’s an example code:

* Interface HC-SR04 Ultrasonic Sensor With Arduino.
// defining the pins
const int trigPin = 9;
const int echoPin = 10;
// defining variables
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");


  • Open Arduino IDE Software and write down your code, or Copy the Above code and paste it
  • Choose an Arduino board, by selecting Tools > Board > Arduino/Geniuno Uno
  • Choose your COM Port  Tools > Port > COM..
  • Upload your source code by pressing Ctrl + U or Sketch > Upload
  • To display the measurement data you can use Serial Monitor by pressing Ctrl + Shift + M

Leave a Reply

Your email address will not be published.

Related Articles

Back to top button