Werktags vor 14:00 Uhr bestellt = am selben Tag versendet 30 Tage Widerrufsrecht 1 Jahr Garantie
← Zurück zur Übersicht

Beim Abspielen wird das Video von YouTube geladen.

Schaltplan und Codebeispiel

So schließt du den HC-SR04 Ultraschall-Abstandssensor an einen Arduino UNO oder ESP32 an, mit einem Beispiel-Sketch, den du direkt hochladen kannst.

HC-SR04 Ultraschall-Abstandssensor Demo

Dieses Projekt misst die Entfernung mit dem HC-SR04 Ultraschallsensor. Ein Ultraschallimpuls wird ausgesendet und die Rücklaufzeit des Echos wird gemessen, um die Entfernung in Zentimetern zu berechnen, die dann alle 500 ms im Serial Monitor ausgegeben wird.

Anschlussplan: HC-SR04 Ultraschall-Abstandssensor an einem Arduino UNO
Anschlussplan · Zum Vergrößern klicken
Anschlusstabelle: HC-SR04 Ultraschall-Abstandssensor an einem Arduino UNO
Anschlusstabelle · Zum Vergrößern klicken
HC_SR04_Ultrasonic_Distance_Sensor_Demo.ino · kompiliert und getestet
/*
 * ==================================================================
 *  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 Ultraschall-Abstandssensor mit ESP32

Dieses Projekt misst die Entfernung mit einem HC-SR04 Ultraschall-Abstandssensor. Da der HC-SR04 mit 5V arbeitet und einen 5V Echo-Impuls ausgibt, reduziert ein einfacher Spannungsteiler aus 1kΩ / 2kΩ Widerständen das Echo-Signal sicher auf ~3.3V für den ESP32. Öffnen Sie den Serial Monitor mit 115200 baud, um die Echtzeit-Abstandsmessungen in Zentimetern anzuzeigen.

Anschlussplan: HC-SR04 Ultraschall-Abstandssensor an einem ESP32 DevKit V1
Anschlussplan · Zum Vergrößern klicken
Anschlusstabelle: HC-SR04 Ultraschall-Abstandssensor an einem ESP32 DevKit V1
Anschlusstabelle · Zum Vergrößern klicken
HC_SR04_Ultrasonic_Distance_Sensor_with_.ino · kompiliert und getestet
/*
 * ==================================================================
 *  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

Zuletzt angesehen

10-poliger DIP-Schalter 2,54 mm

10-poliger DIP-Schalter 2,54 mm

€ 0,80
MAX30100 Pulsoximeter Herzfrequenz-Sensormodul

MAX30100 Pulsoximeter Herzfrequenz-Sensormodul

€ 2,60
Micro-DC-Motor 3V mit Kabeln

Micro-DC-Motor 3V mit Kabeln

€ 1,60
Schiebeschalter SS12D00G2 3-polig

Schiebeschalter SS12D00G2 3-polig

€ 0,60
Breadboard 840 Löcher hohe Qualität

Breadboard 840 Löcher hohe Qualität

€ 2,45
Mini-Holzflugzeug mit Solarenergie (Bildung)

Mini-Holzflugzeug mit Solarenergie (Bildung)

€ 22,00
Ansehen
Kabelbinder Weiß 20cm 3.5mm

Kabelbinder Weiß 20cm 3.5mm

€ 0,25