The HC-SR04 ultrasonic sensor is one of the best first projects for any Arduino beginner. It measures distance by bouncing sound off objects — just like a bat or a car’s parking sensor — and it costs very little. In this guide you’ll wire it up and read the distance in centimetres on the Serial Monitor.

What you’ll need
- Arduino Uno R3 (with USB cable) — the brain of the project
- HC-SR04 Ultrasonic Sensor — measures the distance
- Solderless Breadboard — to build the circuit without soldering
- Male-to-Female Jumper Wires — to connect the sensor to the board
- Active Buzzer (optional) — to turn it into a distance alarm
How it works
The HC-SR04 has two “eyes”: a transmitter and a receiver. When you send a short pulse to the TRIG pin, it emits an ultrasonic burst. The sound hits an object, bounces back, and the ECHO pin goes HIGH for as long as the round-trip takes. Since sound travels at about 343 m/s, we convert that time into distance.
Wiring
- VCC → Arduino 5V
- GND → Arduino GND
- TRIG → Arduino D9
- ECHO → Arduino D10
The Arduino code
const int trigPin = 9;
const int echoPin = 10;
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
// send a 10 microsecond pulse
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// read the echo time and convert to cm
long duration = pulseIn(echoPin, HIGH);
float distanceCm = duration * 0.0343 / 2;
Serial.print("Distance: ");
Serial.print(distanceCm);
Serial.println(" cm");
delay(300);
}Upload the sketch, open Tools → Serial Monitor at 9600 baud, and move your hand in front of the sensor. The distance updates live.
Project ideas
- A parking / reverse alarm that beeps faster as you get closer (add the buzzer).
- A contactless “auto” dustbin lid or hand-sanitiser dispenser.
- An obstacle-detecting robot.
Get the parts
Everything for this project is in stock at Smart Home | Arduino Corner — Shaheen Market, DAV College Road, Rawalpindi — with fast, cash-on-delivery shipping across Pakistan. Grab the HC-SR04 and an Arduino Uno to get started. Browse the full store →