Close Menu
Circuit of Things
  • Home
  • Projects
    • Raspberry Pi Projects
    • Arduino Projects
    • ESP8266 Projects
    • ESP32 Projects
    • IoT Tutorials
    • Sensors & Modules
    • IoT Basics
  • About Us
  • Get In Touch
  • 3D Print
  • Shop Now !
  • Electro Calc

Stay in the Loop!

Subscribe to get the latest IoT tutorials, Arduino projects, and electronics guides delivered straight to your inbox.

Instagram WhatsApp
  • Terms and Conditions
  • Disclaimer
  • Privacy Policy
Circuit of Things Circuit of Things
  • Home
  • Projects
    • Raspberry Pi Projects
    • Arduino Projects
    • ESP8266 Projects
    • ESP32 Projects
    • IoT Tutorials
    • Sensors & Modules
    • IoT Basics
  • About Us
  • Get In Touch
  • 3D Print
  • Shop Now !
  • Electro Calc
Hire us
Circuit of Things
Home»Arduino Projects»HC-SR04 Ultrasonic Sensor Arduino: Distance Measurement and Projects
Arduino Projects

HC-SR04 Ultrasonic Sensor Arduino: Distance Measurement and Projects

Sai Preetham KoyyalaBy Sai Preetham KoyyalaMay 4, 2026No Comments2 Mins Read
Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
Share
Facebook Twitter LinkedIn Pinterest Email
Contents
  • Introduction
  • How It Works
  • Wiring to Arduino Uno
  • Project 1: Distance Meter
  • Project 2: Parking Sensor with Buzzer
  • Project 3: Water Tank Level Monitor
  • Conclusion

Introduction

The HC-SR04 uses sound waves to measure distance, exactly like a bat. It is one of the most useful and affordable sensors for Arduino projects. In this tutorial we cover wiring, calibration, and three practical projects.

How It Works

  • Trigger pin: 10µs HIGH pulse fires ultrasonic burst.
  • Echo pin: HIGH pulse duration proportional to distance.
  • Formula: Distance (cm) = Echo duration (µs) / 58

Wiring to Arduino Uno

HC-SR04 Arduino
VCC 5V
GND GND
Trig Pin 9
Echo Pin 10

Project 1: Distance Meter

#define TRIG 9
#define ECHO 10
void setup() { Serial.begin(9600); pinMode(TRIG,OUTPUT); pinMode(ECHO,INPUT); }
long getDist() {
  digitalWrite(TRIG,LOW); delayMicroseconds(2);
  digitalWrite(TRIG,HIGH); delayMicroseconds(10); digitalWrite(TRIG,LOW);
  return pulseIn(ECHO,HIGH)/58;
}
void loop() { Serial.println(String(getDist())+" cm"); delay(200); }

Project 2: Parking Sensor with Buzzer

#define TRIG 9
#define ECHO 10
#define BUZZER 11
void setup() { pinMode(TRIG,OUTPUT); pinMode(ECHO,INPUT); pinMode(BUZZER,OUTPUT); }
long getDist() {
  digitalWrite(TRIG,LOW); delayMicroseconds(2);
  digitalWrite(TRIG,HIGH); delayMicroseconds(10); digitalWrite(TRIG,LOW);
  return pulseIn(ECHO,HIGH)/58;
}
void loop() {
  long d=getDist();
  if(d<10) { digitalWrite(BUZZER,HIGH); }
  else if(d<30) { digitalWrite(BUZZER,HIGH); delay(100); digitalWrite(BUZZER,LOW); delay(100); }
  else { digitalWrite(BUZZER,LOW); delay(200); }
}

Project 3: Water Tank Level Monitor

Mount the HC-SR04 at the top of a tank pointing down. As water rises, echo time decreases. Convert this to a percentage.

#define TRIG 9
#define ECHO 10
#define TANK_H 100
void setup() { Serial.begin(9600); pinMode(TRIG,OUTPUT); pinMode(ECHO,INPUT); }
void loop() {
  digitalWrite(TRIG,LOW); delayMicroseconds(2);
  digitalWrite(TRIG,HIGH); delayMicroseconds(10); digitalWrite(TRIG,LOW);
  long dist=pulseIn(ECHO,HIGH)/58;
  int pct=constrain(((TANK_H-dist)*100)/TANK_H,0,100);
  Serial.println("Water Level: "+String(pct)+"%");
  delay(1000);
}

Conclusion

The HC-SR04 is a must-have for every maker. From robotics to smart home automation, its applications are nearly limitless!

Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
Previous ArticleESP32 MQTT Tutorial: Publish and Subscribe with HiveMQ
Next Article Raspberry Pi Pico W: Getting Started with MicroPython and Wi-Fi
Sai Preetham Koyyala
  • Website
  • Facebook
  • X (Twitter)
  • Instagram
  • LinkedIn

Hello, I am Sai Preetham Koyyala. I'm an electronics engineer by profession, a DIY enthusiast by passion. ESP32 and Arduino are the main topics of my work.

Related Posts

Arduino Projects

Relay Module with Arduino: Control High Voltage Appliances Safely

May 4, 2026
Arduino Projects

PIR Motion Sensor with Arduino: Smart Security Alarm System

May 4, 2026
Arduino Projects

Arduino Servo Motor Control: Sweep, Potentiometer and Button Projects

May 4, 2026
Add A Comment
Leave A Reply Cancel Reply


KSP Electronics
✓ Trusted Store
Everything for Your
Next IoT Build
Boards, sensors, modules & kits delivered fast across India.
ESP32 Arduino Sensors Modules DIY Kits
500+
Products
Fast
Delivery
★★★★★
Rated
🛒 Shop Now →
kspelectronics.in
💻
● Available for Projects
Hire Me for
IoT Projects
ESP32, LoRaWAN, cloud dashboards & custom hardware — end-to-end IoT solutions for your needs.
ESP32 LoRaWAN MQTT Arduino Cloud PCB Design
3+
Yrs Exp
20+
Projects
★★★★★
Rated
💬 Chat on WhatsApp
View services →
Top Posts

Raspberry Pi: How to Control a DC Motor with L298N and PWN on a Web Server

July 14, 2023

Relay Module with Arduino: Control High Voltage Appliances Safely

May 4, 2026

Esp8266 / NodeMCU Pinout: A Comprehensive Guide for Beginners

June 25, 2023
Follow Us
  • Telegram

Stay in the Loop!

Subscribe to get the latest IoT tutorials, Arduino projects, and electronics guides delivered straight to your inbox.

Most Popular

Raspberry Pi: How to Control a DC Motor with L298N and PWN on a Web Server

July 14, 2023

Relay Module with Arduino: Control High Voltage Appliances Safely

May 4, 2026

Esp8266 / NodeMCU Pinout: A Comprehensive Guide for Beginners

June 25, 2023
Our Picks

Relay Module with Arduino: Control High Voltage Appliances Safely

May 4, 2026

NodeMCU ESP8266 Sensor Data to ThingSpeak IoT Platform

May 4, 2026

ESP32 Firebase Realtime Database: Send and Receive Data from the Cloud

May 4, 2026

Stay in the Loop!

Subscribe to get the latest IoT tutorials, Arduino projects, and electronics guides delivered straight to your inbox.

© 2026 Circuit of Things. All Rights Reserved. Built with ❤️ for the Maker Community.

  • About
  • Privacy
X (Twitter) Instagram YouTube WhatsApp
  • Book an Appointment
  • My Bookings
  • Support Us
© 2026 Circuit of Things. Designed by Sai Preetham Koyyala.

Type above and press Enter to search. Press Esc to cancel.