Introduction: Ultimate Guide for Beginner to Ultrasonic Sensor

This article helps beginners understand how ultrasonic sensor work and how to use it with Arduino. The animated image makes it easy to understand.

The original post: Arduino Get Started

Step 1: Hardware Required

Step 2: About Ultrasonic Sensor

Ultrasonic sensor HC-SR04 is used to measure the distance to an object by using ultrasonic waves.


The ultrasonic sensor HC-SR04 includes four pins:

  • VCC pin: needs to be connected to VCC (5V)
  • GND pin: needs to be connected to GND (0V)
  • TRIG pin: this pin receives the control signal (pulse) from Arduino.
  • ECHO pin: this pin sends a signal (pulse) to Arduino. Arduino measures the duration of pulse to calculate distance.

How It Works

See the animated GIF image.

  1. Micro-controller: generates a 10-microsecond pulse on the TRIG pin.
  2. The ultrasonic sensor: automatically emits the ultrasonic waves.
  3. The ultrasonic wave: is reflected after hitting an obstacle.
  4. The ultrasonic sensor: detects the reflected ultrasonic wave and measures the travel time of the ultrasonic wave.
  5. Ultrasonic sensor: generates a pulse to the ECHO pin. The duration of the pulse is equal to the travel time of the ultrasonic wave.
  6. Micro-controller: measures the pulse duration in the ECHO pin, and then calculate the distance between sensor and obstacle.

How to Get Distance From Ultrasonic Sensor
To get distance from the ultrasonic sensor, we only need to do two steps (1 and 6 on How It Works part):

  • Generates a 10-microsecond pulse on TRIG pin
  • Measures the pulse duration in ECHO pin, and then calculate the distance between sensor and obstacle

Distance Calculation

We have:

The travel time of the ultrasonic wave (µs): travel_time = pulse_duration

The speed of the ultrasonic wave: speed = SPEED_OF_SOUND = 340 m/s = 0.034 cm/µs


The travel distance of the ultrasonic wave (cm): travel_distance = speed × travel_time = 0.034 × pulse_duration

The distance between sensor and obstacle (cm): distance = travel_distance / 2 = 0.034 × pulse_duration / 2 = 0.017 × pulse_duration

Arduino - Ultrasonic Sensor
Arduino's pins can generate a 10-microsecond pulse and measure the pulse duration. Therefore, we can get the distance from the ultrasonic sensor by using two Arduino's pins:

  • One pin is connected to TRIG PIN to generate 10µs pulse to TRIG pin of the sensor
  • Another pin is connected to ECHO PIN measure pulse from the sensor

Step 3: Wiring Diagram

Step 4: How to Program

  • How to generate a 10-microsecond pulse in Arduino's pin, for example, pin 9:

digitalWrite(9, HIGH);
digitalWrite(9, LOW);

  • How to measures the pulse duration (µs) in Arduino's pin, for example, pin 8:

duration_us = pulseIn(8, HIGH);

  • How to measures the pulse duration (µs) in Arduino's pin, for example, pin 8:

distance_cm = 0.017 * duration_us;

Step 5: Arduino Code

* Created by ArduinoGetStarted, <a href="">
* Arduino - Ultrasonic Sensor HC-SR04
* Wiring: Ultrasonic Sensor -> Arduino:
* - VCC -> 5VDC
* - TRIG -> Pin 9
* - ECHO -> Pin 8
* - GND -> GND
* Tutorial is available here: <a href="">

int trigPin = 9; // TRIG pin
int echoPin = 8; // ECHO pin

float duration_us, distance_cm;

void setup() {
// begin serial port
Serial.begin (9600);

// configure the trigger pin to output mode
pinMode(trigPin, OUTPUT);
// configure the echo pin to input mode
pinMode(echoPin, INPUT);

void loop() {
// generate 10-microsecond pulse to TRIG pin
digitalWrite(trigPin, HIGH);
digitalWrite(trigPin, LOW);

// measure duration of pulse from ECHO pin
duration_us = pulseIn(echoPin, HIGH);

// calculate the distance
distance_cm = 0.017 * duration_us;

// print the value to Serial Monitor
Serial.print("distance: ");
Serial.println(" cm");


Quick Steps

  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
  • Open Serial Monitor
  • Move your hand in front of ultrasonic sensor
  • See the distance from the sensor to your hand on Serial Monitor