Arduino Ultrasonic Distance Sensor Arduino Beginner

Arduino Ultrasonic Distance Sensor

Published on Jan 02, 2026

Ultrasonic Distance Sensor Project

The HC-SR04 ultrasonic sensor is perfect for measuring distances from 2cm to 400cm. This tutorial will show you how to interface it with Arduino.

Components Needed

  • Arduino Uno
  • HC-SR04 ultrasonic sensor
  • Jumper wires
  • Breadboard

How It Works

The sensor sends out ultrasonic sound waves and measures the time it takes for the echo to return. Using the speed of sound, we can calculate the distance.

Code Example

const int trigPin = 9;
const int echoPin = 10;

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);
  
  long duration = pulseIn(echoPin, HIGH);
  float distance = duration * 0.034 / 2;
  
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
  delay(500);
}

Tutorial Resources

Video Tutorial
Found this tutorial helpful?

Share it with your classmates and friends!

Related Tutorials

Arduino
Getting Started with Arduino

Learn the basics of Arduino programming and build your first LED blink project.

Read Tutorial
Arduino
Temperature Monitoring with DHT22

Create a temperature and humidity monitoring system using DHT22 sensor. Display readings on LCD or serial monitor.

Read Tutorial

Need Components for This Project?

Browse our shop for all the parts you need

Visit Shop
Chat with us