Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Connect the HC-SR04’s VCC and GND to the Arduino UNO R4’s 5 V and ground, then connect Trig and Echo to two digital pins. The sketch sends Trig a pulse longer than 10 microseconds, times the Echo pulse, and estimates distance in centimeters with duration ÷ 58. The steps work with both UNO R4 Minima and UNO R4 WiFi; wireless features are not needed.

What you need and which UNO R4 to choose

  • An Arduino UNO R4 Minima or UNO R4 WiFi
  • An HC-SR04 ultrasonic sensor module
  • Jumper wires

Both UNO R4 variants operate at 5 V at the RA4M1/GPIO level, so either is suitable for this basic connection. The Minima has 14 digital I/O pins. The WiFi model adds an ESP32 module for Wi-Fi and Bluetooth, but this project does not use those features. See Arduino’s UNO R4 Minima documentation and UNO R4 WiFi documentation for board details.

HC-SR04 is a widely used module name, but modules from different manufacturers may not have identical documentation. The figures below are published in the HC-SR04 manual hosted by Arobose; check the documentation supplied with your particular module.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How to connect the HC-SR04 to Arduino

The module has four connections: VCC, Trig, Echo, and GND. One straightforward pin mapping is SunFounder’s example of D4 for Trig and D3 for Echo; other available digital pins can work if the sketch uses the same assignments.

#1 Best Overall
WWZMDiB 2 Pcs HC-SR04 Ultrasonic Sensor Module Compatible with for Arduino R3 MEGA Mega2560 Duemilanove Nano Robot XBee ZigBee (2 Pcs HC-SR04 Ultrasonic Sensor)
  • HC-SR04 Ultrasonic Sensor:This is a device that can use sound waves to measure the distance of an object. It measures distance by emitting a sound wave of a specific frequency and listening to the bounce of that sound wave. The distance between the sonar sensor and the object can be calculated by recording the time elapsed between the generation of the sound wave and the bounce of the sound wave
  • Working Voltage: 5V DC;Quiescent current: less than 2mA
  • Ranging Distance:2cm - 450 cm;High precision: 0.3 cm
  • Effectual Angle: <15°
  • Test mode :Test distance = ((Duration of high level)*(Sonic :340m/s))/2
HC-SR04 pin Connect to Purpose
VCC Arduino 5 V Power; the manual specifies a 5 V DC supply.
GND Arduino GND Shared electrical ground.
Trig Arduino D4 Receives the measurement-start pulse.
Echo Arduino D3 Returns a pulse whose width represents the sound’s round-trip travel time.

D4 and D3 are example assignments, not required pins. If you choose different pins, change the declarations in the sketch to match. SunFounder’s connection guidance is available in its ultrasonic project tutorial.

Upload a sketch that measures Echo

This example uses Arduino’s pulseIn() function with a timeout. If no Echo pulse arrives within the timeout, the sketch reports that there is no valid reading instead of turning a missing return into a distance.

Rank #2
ELEGOO 5PCS HC-SR04 Ultrasonic Module Distance Sensor Kit
  • NON-CONTACT DISTANCE SENSING: Add object detection to robot navigation, parking-distance prototypes, automatic lids, counters and interactive projects; each HC-SR04 uses a 40 kHz ultrasonic burst and echo timing to estimate distance
  • 5-PACK FOR REPEATABLE PROTOTYPING: Use multiple HC-SR04 modules across builds, compare sensor positions or keep spares for testing and replacement; each module integrates an ultrasonic transmitter, receiver and control circuit
  • 5 V MODULE WITH 3-450 CM RANGE: Connect VCC, Trig, Echo and GND, use a 10 µs trigger pulse and measure Echo duration; resolution is 0.3 cm with an effective angle under 15°, while the controller board and external power source are not included
  • PROTECT 3.3 V GPIO: The HC-SR04 operates from 5 V and its Echo output is 5 V, so use a voltage divider or suitable level shifting with 3.3 V inputs; keep the module dry and use it for prototyping rather than calibrated measurement
  • FOR ROBOTICS & STEM PROJECTS: Suitable for distance measurement, object detection, automatic lids, parking alerts, robot navigation and other hands-on electronics builds
const int trigPin = 4;
const int echoPin = 3;

void setup() {
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  digitalWrite(trigPin, LOW);
}

void loop() {
  // Start a ranging cycle with a Trigger pulse longer than 10 microseconds.
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Measure how long Echo remains HIGH; 30000 microseconds is the timeout.
  unsigned long duration = pulseIn(echoPin, HIGH, 30000UL);

  if (duration == 0) {
    Serial.println("No valid reading (Echo timeout)");
  } else {
    float distanceCm = duration / 58.0;
    Serial.print("Distance: ");
    Serial.print(distanceCm);
    Serial.println(" cm");
  }

  delay(60);
}

Open the Serial Monitor at 9600 baud to see the result. Keep the interval between measurement cycles over 60 ms when following SunFounder’s guidance for this module tutorial; it is not a universal guarantee for every HC-SR04 variant.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How to calculate distance from the Echo pulse

What the pulse measures

The sensor sends an ultrasonic burst and waits for its reflection. The Echo output stays high for the time the sound takes to travel from the sensor to the target and back. The manual lists the operating frequency as 40 kHz and describes a Trigger pulse longer than 10 microseconds as the start of a ranging cycle.

Rank #3
MTDELE 5 Pcs HC-SR04 Ultrasonic Sensor Module with 5Pcs Mounting Bracket
  • HC-SR04 Ultrasonic Sensor:Compatible with for Arduino R3 UNO MEGA Mega2560 Duemilanove XBee Nano Robot With 5Pcs mounting bracket
  • Working Voltage: 5V DC; Quiescent current: Less than 2mA
  • Ranging Distance:2 - 450 cm;High precision:0.3 cm;Effectual Angle: < 15°
  • Test distance=((high level duration)*(sound wave: 340m/s))/2
  • Merchandise included:5Pcs HC-SR04 Ultrasonic Sensor;5Pcs Mounting bracket;20Pcs Mounting screw;10Pcs Female to Female Wire; 10Pcs Male to Female Wire

Convert round-trip time to one-way distance

For a time measurement in seconds, the physical calculation is:

distance = Echo duration × speed of sound ÷ 2

The division by two matters because the measured time includes both the outgoing and returning trip. The manual says, “Normally sound speed is 340m/s under room temperature.” With duration in microseconds, a common centimeter approximation is:

Rank #4
5pcs HC-SR04 Ultrasonic Sensor, Distance Sensor with Ultrasonic Transmitter and Receiver Module Compatible with Ar-duino UNO MEGA2560 Nano Robot XBee ZigBee
  • Test mode :Using IO trigger for high level signal.( Not less that 10us),The Module sends eight 40 kHz automatically and detect whether there is a pulse signal back.
  • The detection zone: 0.78~196 in/ (2cm~500cm); High precision: up to 0.12 in/(0.3 cm) Effectual angle: less than 15°.
  • Power supply: 5V DC; Quiescent current: less than 2mA.
  • Test distance = ((Duration of high level)*(Sonic :340m/s))/2.
  • Package included: 5 x HC-SR04 Ultrasonic Module.

distance in cm ≈ duration in microseconds ÷ 58

For example, an Echo duration of 580 microseconds gives an estimated distance of about 10 cm. This is a calculation example, not a test result.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

What the module specifications do—and do not—tell you

The HC-SR04 manual hosted by Arobose lists a 2–400 cm range, 0.3 cm resolution, 40 kHz operating frequency, 5 V DC supply, and a Trigger pulse longer than 10 microseconds. These are published module specifications, not measurements made for this guide or a promise of performance with every target and environment. The manual’s publication date is not stated in the retrieved document, and no independent controlled error testing is available here.

Best Value
EPLZON HC-SR04 Ultrasonic Module Distance Sensor fit for Arduino UNO MEGA Nano Robot XBee ZigBee (Pack of 5 pcs)
  • EPLZON HC-SR04 Ultrasonic ranging transducer sensor
  • Test mode: Use IO to trigger high-level signals. (Not less than 10us), the module automatically sends 8 40kHz and detects whether there is a pulse signal return.
  • Detection area: 0.78~196 in/(2cm~500cm); high precision: up to 0.12 inch/(0.3 cm), effective angle: less than 15°; Trigger input pulse width: 10uS
  • Power supply: 5V DC; Quiescent current: less than 2mA;Dimension: 1.77 x 0.78 x 0.59 inches/45mm x 20mm x 15mm(length*width*height)
  • Test distance=((high level duration)*(sound wave: 340m/s))/2

Target shape and surroundings can affect whether a useful echo returns. Treat the displayed value as an estimate, particularly near the stated range limits; a timeout means the sketch did not receive a valid pulse within its wait period, not that the object is at a precise distance.

Quick Recap

Bestseller No. 1
WWZMDiB 2 Pcs HC-SR04 Ultrasonic Sensor Module Compatible with for Arduino R3 MEGA Mega2560 Duemilanove Nano Robot XBee ZigBee (2 Pcs HC-SR04 Ultrasonic Sensor)
WWZMDiB 2 Pcs HC-SR04 Ultrasonic Sensor Module Compatible with for Arduino R3 MEGA Mega2560 Duemilanove Nano Robot XBee ZigBee (2 Pcs HC-SR04 Ultrasonic Sensor)
Working Voltage: 5V DC;Quiescent current: less than 2mA; Ranging Distance:2cm - 450 cm;High precision: 0.3 cm
$5.99
Bestseller No. 3
MTDELE 5 Pcs HC-SR04 Ultrasonic Sensor Module with 5Pcs Mounting Bracket
MTDELE 5 Pcs HC-SR04 Ultrasonic Sensor Module with 5Pcs Mounting Bracket
Working Voltage: 5V DC; Quiescent current: Less than 2mA; Ranging Distance:2 - 450 cm;High precision:0.3 cm;Effectual Angle: < 15°
$9.99
Bestseller No. 4
5pcs HC-SR04 Ultrasonic Sensor, Distance Sensor with Ultrasonic Transmitter and Receiver Module Compatible with Ar-duino UNO MEGA2560 Nano Robot XBee ZigBee
5pcs HC-SR04 Ultrasonic Sensor, Distance Sensor with Ultrasonic Transmitter and Receiver Module Compatible with Ar-duino UNO MEGA2560 Nano Robot XBee ZigBee
Power supply: 5V DC; Quiescent current: less than 2mA.; Test distance = ((Duration of high level)*(Sonic :340m/s))/2.
$14.50
Bestseller No. 5
EPLZON HC-SR04 Ultrasonic Module Distance Sensor fit for Arduino UNO MEGA Nano Robot XBee ZigBee (Pack of 5 pcs)
EPLZON HC-SR04 Ultrasonic Module Distance Sensor fit for Arduino UNO MEGA Nano Robot XBee ZigBee (Pack of 5 pcs)
EPLZON HC-SR04 Ultrasonic ranging transducer sensor; Test distance=((high level duration)*(sound wave: 340m/s))/2
$9.99

Troubleshoot missing or implausible readings

  • No reading or repeated timeouts: Confirm that sensor GND and Arduino GND are connected, VCC goes to 5 V, and Trig and Echo are not swapped.
  • Sketch uploads but readings are wrong: Check that trigPin and echoPin match the pins actually wired. The example expects Trig on D4 and Echo on D3.
  • Inconsistent returns: Check the target and setup, and allow more than 60 ms between cycles if using SunFounder’s documented timing guidance.
  • Still not working: Verify the pin labels, supply-voltage specification, and timing/range documentation for your exact sensor module; the HC-SR04 name alone does not establish that every maker’s version is identical.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.