Ordered on weekdays before 14:00 = shipped the same day and delivered the next day in 98% of cases. Free returns 30-day reflection period 1-year warranty

Wiring diagram and code example

How to connect the HC-SR04 ultrasonic distance sensor to an Arduino UNO or ESP32, with an example sketch you can upload straight away.

HC-SR04 Ultrasonic Distance Sensor Demo

This project measures distance using the HC-SR04 ultrasonic sensor. An ultrasonic pulse is emitted and the return echo time is measured to calculate distance in centimeters, which is then printed to the Serial Monitor every 500 ms.

Wiring diagram: HC-SR04 ultrasonic distance sensor on an Arduino UNO
Wiring diagram · Click to enlarge
Connection table: HC-SR04 ultrasonic distance sensor on an Arduino UNO
Connection table · Click to enlarge
HC_SR04_Ultrasonic_Distance_Sensor_Demo.ino · compiled and verified
/*
 * ==================================================================
 *  Generated by Codey.online  —  https://www.codey.online
 * ==================================================================
 *  Project   : HC-SR04 Ultrasonic Distance Sensor Demo
 *  Board     : Arduino UNO (arduino:avr:uno)
 *  Parts     : HC-SR04
 *  Libraries : none (built-in)
 *
 *  Codey Online is an AI-powered browser IDE for Arduino and ESP32.
 *  Describe your project and Codey writes the code, draws the wiring
 *  diagram and uploads it to your board, straight from the browser.
 *  This code is free to use, modify and share, without warranty.
 * ==================================================================
 */

// HC-SR04 Ultrasonic Distance Sensor Demo

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

void setup() {
  Serial.begin(115200);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  digitalWrite(trigPin, LOW);
  Serial.println(F("HC-SR04 Ultrasonic Sensor Ready"));
}

void loop() {
  // Trigger a 10-microsecond high pulse
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Measure the duration of the incoming echo pulse (timeout ~30ms / ~5m)
  unsigned long duration = pulseIn(echoPin, HIGH, 30000UL);

  if (duration == 0) {
    Serial.println(F("Out of range"));
  } else {
    // Speed of sound is ~343 m/s -> ~29.15 us per cm (round-trip 58.3 us/cm)
    float distanceCm = duration / 58.2;
    Serial.print(F("Distance: "));
    Serial.print(distanceCm, 1);
    Serial.println(F(" cm"));
  }

  delay(500);
}

Wiring diagram and example code generated by Codey.online — https://www.codey.online

HC-SR04 Ultrasonic Distance Sensor with ESP32

This project measures distance using an HC-SR04 ultrasonic distance sensor. Because the HC-SR04 runs on 5V and outputs a 5V Echo pulse, a simple 1kΩ / 2kΩ resistor voltage divider safely scales the Echo signal down to ~3.3V for the ESP32. Open the Serial Monitor at 115200 baud to view real-time distance measurements in centimeters.

Wiring diagram: HC-SR04 ultrasonic distance sensor on an ESP32 DevKit V1
Wiring diagram · Click to enlarge
Connection table: HC-SR04 ultrasonic distance sensor on an ESP32 DevKit V1
Connection table · Click to enlarge
HC_SR04_Ultrasonic_Distance_Sensor_with_.ino · compiled and verified
/*
 * ==================================================================
 *  Generated by Codey.online  —  https://www.codey.online
 * ==================================================================
 *  Project   : HC-SR04 Ultrasonic Distance Sensor with ESP32
 *  Board     : ESP32 DEVKIT V1 (esp32:esp32:esp32doit-devkit-v1)
 *  Parts     : HC-SR04
 *  Libraries : none (built-in)
 *
 *  Codey Online is an AI-powered browser IDE for Arduino and ESP32.
 *  Describe your project and Codey writes the code, draws the wiring
 *  diagram and uploads it to your board, straight from the browser.
 *  This code is free to use, modify and share, without warranty.
 * ==================================================================
 */

// HC-SR04 Ultrasonic Distance Sensor with ESP32
const int TRIG_PIN = 5;
const int ECHO_PIN = 18;

void setup() {
  Serial.begin(115200);
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  Serial.println("HC-SR04 Distance Sensor Initialized");
}

void loop() {
  // Send a 10-microsecond HIGH pulse to trigger the sensor
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  // Measure the round-trip echo pulse duration in microseconds
  long duration = pulseIn(ECHO_PIN, HIGH, 30000); // 30ms timeout (~5m range)

  if (duration == 0) {
    Serial.println("Out of range / no echo received");
  } else {
    // Speed of sound is ~0.0343 cm/us (divide by 2 for one-way distance)
    float distanceCm = (duration * 0.0343) / 2.0;
    Serial.print("Distance: ");
    Serial.print(distanceCm, 1);
    Serial.println(" cm");
  }

  delay(500);
}

Wiring diagram and example code generated by Codey.online — https://www.codey.online

HC-SR04 ultrasonic distance sensor

This sensor is ideal for making a robot car that needs to keep track of the distance to objects. Or you can make your own parking aid with it. Unlimited possibilities with this ultrasonic distance sensor. Ideal for your Arduino / ESP32 / Wemos or Raspberry Pi project.

View the Wiring diagram and example code >>>

Sensor angle: <15o
Dimensions: 45x20mm
Resolution: 3mm
Detection range: 2-450cm
Voltage: 5V
Current: <2mA

The HC-SR04 ultrasonic distance sensor is a device that uses ultrasonic waves to measure distance. The sensor consists of a transmitter and a receiver connected to a microcontroller, such as an Arduino.

The sensor works by having the transmitter emit an ultrasonic pulse, which is then reflected by objects in the environment. The receiver then detects the reflection and sends the time back to the microcontroller, which can then calculate the distance to the object.

The HC-SR04 ultrasonic distance sensor is suitable for measuring distances up to 3 meters with an accuracy of about 3 mm. The sensor is easy to use with an Arduino microcontroller and is suitable for a wide range of applications, such as making automatic parking systems or detecting obstacles in a certain environment.

The sensor has a small size and low power consumption, making it suitable for projects where space and power management are important. The sensor is also easy to install thanks to the four holes on the back, which make it easy to attach the sensor to a surface.

All in all, the HC-SR04 ultrasonic distance sensor is a practical and versatile sensor that can be an excellent addition to any Arduino project.

Dimensions
Length: 4.55cm
Width: 2.05cm
Height: 1.53cm

  • Distance sensor
  • 45 x 20 mm
  • Detection 2-450 cm
Playing loads the video from YouTube.

Why choose this product?

  • Distance measurement – Measure objects with ultrasonic waves.
  • Build a robot car – Ideal for building a robot car.
  • Build a parking aid – Suitable for your own parking aid.
  • Arduino compatible – Suitable for Arduino, ESP32, and Raspberry Pi projects.
  • Easy mounting – Four holes on the back.
Specifications
Width2.05 cm
Resolution3 mm
Length4.55 cm
Height1.53 cm

Frequently bought together

Total: € 16,37

Reviews

★★★★★ 5,0/5 2 reviews
  • ★★★★★Jan FavresseVerified purchase

    seems to be good

  • ★★★★★Brenon EjupovicVerified purchase

    great thing.

Write a review
Your rating

Shipping & returns

Shipping

  • Parcel with track & trace€ 7,701 working day
  • Pick up at PostNL location€ 7,40Within 24 hours
  • Letterbox parcel 48 hours€ 3,70Within 2 business days
  • Letterbox parcel 24 hours€ 4,55Within 24 hours
  • Pick up in storeFree
All about shipping

Returns

Not satisfied? You have 30 days after delivery to return the product.

All about returns

Recently viewed