Let us first understand what is Ultrasonic sensor and why, where it is used? Then will move to interfacing part.
Ultrasonic sensor
As shown below the HC-SR04 Ultrasonic (US) sensor is a 4 pin module, whose pin names are Vcc, Trigger, Echo and Ground respectively. This sensor is a very popular sensor used in many applications where measuring distance or sensing objects are required. The module has two eyes like projects in the front which forms the Ultrasonic transmitter and Receiver. The sensor works with the simple high school formula that is
Distance = Speed × Time
The Ultrasonic transmitter transmits an ultrasonic wave, this wave travels in air and when it gets objected by any material it gets reflected back towards the sensor this reflected wave is observed by the Ultrasonic receiver module and distance is computed.
Interfacing Ultrasonic sensor with Arduino

Step by step wiring connections
Insert the LED into the breadboard
Connect the +ve rail of the breadboard to +5v of Arduino and –ve rail of the breadboard to GND of Arduino
Connect the +ve terminal of the LED to the digital pin 2 of Arduino
Connect the –ve terminal of the LED to the –ve rail through 220 Ohm.
Connect the VCC of the sensor to the +ve rail and Gnd of the sensor to the -ve rail
Connect the trigger pin of the sensor to the digital pin 8 of Arduino
Connect the Eco pin of the sensor to the digital pin 9 of Arduino
Code for Interfacing the Ultrasonic sensor
Define the Pin Mode in the void Setup
Define the logic in the void Loop
Functions used in this example:
pinMode(Pin_Number, IN/OUT); to set the mode i.e. input or output for example pinMode(3,OUTPUT);
digitalWrite(Pin_Number,LOW/HIGH); to set the pin as High or low for example digitalWrite(3,LOW);
Serial.begin(9600); to start serial monitor
delay(microseconds) to put a time delay between two instructions. For example delay(1000) i.e. 1 sec delay
Serial.println(data); to print the value on the serial monitor