Op werkdagen vóór 14:00 uur besteld = dezelfde dag verzonden en in 98% van de gevallen de volgende dag in huis. Gratis retourneren 30 dagen bedenktijd 1 jaar garantie
← Tillbaka till översikten

Vid uppspelning laddas videon från YouTube.

Kopplingsschema och kodexempel

Så här kopplar du HC-SR04 ultraljudsavståndssensor till en Arduino UNO eller ESP32, med ett exempel­sketch som du kan ladda upp direkt.

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.

Kopplingsschema: HC-SR04 ultraljudsavståndssensor på en Arduino UNO
Kopplingsschema · Klicka för att förstora
Kopplingstabell: HC-SR04 ultraljudsavståndssensor på en Arduino UNO
Kopplingstabell · Klicka för att förstora
HC_SR04_Ultrasonic_Distance_Sensor_Demo.ino · kompilerad och testad
/*
 * ==================================================================
 *  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.

Kopplingsschema: HC-SR04 ultraljudsavståndssensor på en ESP32 DevKit V1
Kopplingsschema · Klicka för att förstora
Kopplingstabell: HC-SR04 ultraljudsavståndssensor på en ESP32 DevKit V1
Kopplingstabell · Klicka för att förstora
HC_SR04_Ultrasonic_Distance_Sensor_with_.ino · kompilerad och testad
/*
 * ==================================================================
 *  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