Fiverr gig
$15 — I will help you with your project and simulate it for you
Hire me
🤖 Arduino Projects

Ultrasonic Sensor Arduino (HC-SR04) — Wiring, Code & Proteus

The Engineer PostMay 29, 20265 min read
HC-SR04 ultrasonic sensor wired to Arduino Uno with Trig on D9 and Echo on D10

An Ultrasonic Sensor Arduino (HC-SR04) project measures distance by timing a 40 kHz ping bouncing off an obstacle. In this tutorial we wire the HC-SR04 to an Arduino Uno, walk through the ultrasonic sensor Arduino code, and open the same circuit in Proteus so you can simulate radar, parking and obstacle-avoidance projects.

Welcome to this beginner-friendly Arduino tutorial on the ultrasonic. By the end of the guide, you'll wire the module to an Arduino UNO, flash a short sketch, and read live values on the Serial Monitor — no prior electronics experience required.

Need this built for you? I do custom Arduino, ESP32 and Proteus projects from $15.

What you'll learn

  • How the module works in plain language
  • The exact parts you need and how to wire them safely
  • The full Arduino IDE sketch with comments
  • Common issues and how to fix them
Arduino with an Ultrasonic Sensor — overview

Arduino code

Open the Arduino IDE, paste the sketch below into a new file, install any libraries the sketch #includes (Tools → Manage Libraries), select your board and COM port, then click Upload.

const int trigPin = 4; // Trigger pin of the ultrasonic sensor (connected to Arduino digital pin 2)
const int echoPin = 5; // Echo pin of the ultrasonic sensor (connected to Arduino digital pin 3)
void setup() {
  Serial.begin(9600); // Initialize serial communication for debugging
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}
void loop() {
  // Trigger the ultrasonic sensor by sending a 10us pulse
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Measure the time it takes for the echo to return
  long duration = pulseIn(echoPin, HIGH);
  // Calculate the distance in centimeters
  // Speed of sound in air at room temperature is approximately 343 meters/second or 0.0343 cm/microsecond
  // Divide the duration by 2 to account for the time it takes for the sound pulse to travel to the object and back
  int distance = duration * 0.0343 / 2;
  // Print the distance to the Serial Monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
  delay(1000); // Delay for readability (adjust as needed)
}

How it works

The sketch initialises serial communication and the ultrasonic driver in setup(), then in loop() it samples the sensor at a regular interval and prints the result to the Serial Monitor at 9600 baud. Open the Serial Monitor (Ctrl+Shift+M) after upload to see live readings.

Troubleshooting checklist

  • No readings: verify the baud rate in Serial Monitor matches the sketch (usually 9600).
  • Garbage characters: wrong baud rate or loose GND wire.
  • Library not found: install the exact library referenced in the #include line via Library Manager.
  • Sensor not detected (I²C): run an I²C scanner sketch to confirm the address.

What to build next

Once the basic readout works, try logging values to an SD card, sending them over Wi-Fi with an ESP32, or pushing them to a Blynk IoT dashboard. Pair this module with our simulator round-up to prototype the circuit before soldering.

Stuck on your own build? Send me your Arduino / ESP32 / Proteus project on Fiverr — from $15, you don't pay if it doesn't work.

Frequently Asked Questions

Q.How to connect the HC-SR04 ultrasonic sensor to Arduino?

Wire VCC to 5V, GND to GND, Trig to a digital output pin (e.g. D9) and Echo to a digital input pin (e.g. D10). No pull-up resistors are needed.

Q.What is the ultrasonic sensor Arduino code?

Send a 10 µs HIGH pulse on Trig, then use pulseIn(Echo, HIGH) to measure the echo time in microseconds. Distance in cm = duration * 0.0343 / 2.

Q.What is the maximum range of the HC-SR04?

Rated 2 cm to 400 cm, but reliable readings in real-world conditions are usually 3 cm to about 250 cm. Soft surfaces and small objects reduce range.

Q.Can I simulate the HC-SR04 ultrasonic sensor in Proteus?

Yes. Proteus includes an ULTRASONIC / HC-SR04 model that responds to Trig pulses. Load the compiled .hex into a virtual Arduino Uno and read the distance on a virtual terminal.

TEP

The Engineer Post

Embedded systems engineer and educator. Writes weekly tutorials at EmbedLab to help beginners ship real hardware.

Related projects

Browse all 50+ projects