Breadcrumb Abstract Shape
Breadcrumb Abstract Shape

Ultrasonic Sensor – Distance Measurement with Arduino

Introduction

The ultrasonic sensor is a device that measures distance by using sound waves beyond the human hearing range (greater than 20 kHz). It has two main components: a transmitter, which sends out a short burst of ultrasonic sound, and a receiver, which listens for the echo when that sound bounces back from an object. When the transmitter emits the pulse, it travels through the air until it hits an obstacle and reflects back toward the sensor. The receiver detects this returning echo, and the Arduino uses its internal clock to measure how much time passed between sending the pulse and receiving the echo. This time measurement is then stored in a variable and, using simple physics, converted into distance. Since the sound wave travels to the object and back, the measured time corresponds to twice the actual distance, so the final distance is calculated using the formula:

Distance = Velocity of Sound × Time 2

The ultrasonic sensor and the object are placed facing each other
My Sample Image

The sensor emits ultrasonic sound waves toward the object
My Sample Image

These sound waves bounce back as echoes after hitting the object
My Sample Image

The sensor receives the echo and Arduino calculates the distance using the travel time
My Sample Image

Hardware Required

• Arduino Uno (or compatible board)
• 1 × Ultrasonic Sensor (HC-SR04)
• Breadboard
• Jumper wires
• USB cable

Circuit

• VCC → 5V on Arduino
• GND → GND on Arduino
• TRIG → Digital Pin 9
• ECHO → Digital Pin 10

Schematic Diagram

hhhh

Code with Step-by-Step Explanation

After you build the circuit, plug your Arduino board into your computer, start the Arduino Software (IDE), and enter the code below. You may also load it from the menu:

File → Examples → 01.Basics → Blink.

/*
Ultrasonic Sensor with Arduino
Measures distance and displays it on Serial Monitor.
*/
const int trigPin = 9;
const int echoPin = 10;
long duration;
int distance;

void setup() {
pinMode(trigPin, OUTPUT); // Trig pin will send signal
pinMode(echoPin, INPUT); // Echo pin will receive signal
Serial.begin(9600); // Start Serial Monitor
}

void loop() {
// Clear the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Send a 10us HIGH pulse to trigger the sensor
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Read the echoPin, returns time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculate distance in cm
distance = duration * 0.034 / 2;

// Print distance
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(500);
}
Line-by-Line Explanation

  • const int trigPin = 9;
    Declares a constant integer variable named trigPin and assigns it to pin 9 of the Arduino. This pin is connected to the TRIG pin of the ultrasonic sensor, which is used to send the ultrasonic pulse.
  • const int echoPin = 10;
    Declares another constant integer variable named echoPin and assigns it to pin 10 of the Arduino. This pin is connected to the ECHO pin of the ultrasonic sensor, which is responsible for receiving the reflected signal.
  • long duration;
    Creates a variable of type long to store the time taken (in microseconds) for the echo to return. long is used because the timing values can be quite large.
  • int distance;
    Creates a variable of type int to store the calculated distance in centimeters.

  • void setup()
    This function runs once when the Arduino is powered on or reset.
    • pinMode(trigPin, OUTPUT); → Configures pin 9 as an output. Arduino will send the pulse to the TRIG pin.
    • pinMode(echoPin, INPUT); → Configures pin 10 as an input. Arduino listens for the echo signal.
    • Serial.begin(9600); → Initializes serial communication at 9600 bps for Serial Monitor output.

  • void loop()
    This function runs repeatedly and contains the main logic.
    • digitalWrite(trigPin, LOW); → Clears the TRIG pin for 2 microseconds.
    • digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); → Sends a 10µs HIGH pulse to trigger the sensor.
    • duration = pulseIn(echoPin, HIGH); → Measures the time for the echo to return.
    • distance = duration * 0.034 / 2; → Converts time into distance using speed of sound.
    • Serial.print("Distance: "); Serial.print(distance); Serial.println(" cm"); → Prints result in Serial Monitor.
    • delay(500); → Waits half a second before next measurement.

3 Comments

  1. I really enjoyed this post. Very exciting article!! Lorem ipsum is dummy text used in laying out print, graphic or web designs.

  2. Inspiring education blog! Illuminating perspectives on effective teaching. Practical insights and innovative approaches make this a must-read for educators seeking impactful strategies. Bravo!

Leave a Reply

Your email address will not be published. Required fields are marked *