[:en] Distance Measurement with the Ultrasound Distance Sensor HC-SR04[:de]Abstandsmessung mit dem Ultraschall Distanz Sensor HC-SR04[:]

HC-SR04 ultrasonic distance sensor module with connecting wires on a blue circuit board, placed on a green grid mat.

[:en]

HC-SR04 Ultraschall Sensor

The HC-SR04 is an ultrasound distance sensor that is very easy to control and is available at an affordable price. The working principle is quite simple: Similar to sonar, the sensor emits a sound pulse that travels from the sensor and may be reflected by an object. The sound travels from the sensor to the object, gets reflected there, and returns to the sensor as an echo. The sensor has built-in signal processing that immediately displays any detected echoes. The time it takes for the echo to be received is also known as the time of flight. By measuring this time with the Arduino, you can calculate the distance using the speed of sound.

The sensor is controlled as follows: When there is a falling edge on the trigger input of the sensor, which must last longer than 10µs, the transmission of an ultrasound signal is prepared. Once this signal has been transmitted, the echo pin switches from LOW to HIGH. When the echo of the ultrasound pulse is received, the echo pin returns to LOW. For distance measurement, you only need to measure the time during which the echo pin remains HIGH, and then convert that time into distance.

Here is the wiring diagram:

Ultraschall_Steckplatine

Thanks to the standard libraries provided with the Arduino, the code is also straightforward to write.

//*******************
// Distance HC-SR04
// Madgyver.de
//*******************

#define echopin 12
#define triggerpin 13
#define speedfactor 29.41/2 //half of the time for Microseconds needed for one centimeter


void setup() {
  Serial.begin(9600);
  pinMode(triggerpin, OUTPUT);
  pinMode(echopin, INPUT);
}

void loop() {
  Serial.println(measure() / speedfactor);
  delay (10);
}

int measure() {
  int duration = 0;

  //sending start condition
  digitalWrite(triggerpin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerpin, LOW);

  //measure time of flight in microseconds
  duration = pulseIn(echopin, HIGH, 10000); //10ms timeout

  return duration;
}

The code does exactly what was described earlier, so I will only focus on the conversion. The function “measure” gives us a value that corresponds to the time until the echo is received. The value is given in microseconds. The conversion factor is calculated as follows:

  • Sound travels at a speed of about 340m/s at normal room temperatures.
  • The reciprocal of this is 0.00294117647s/m, which is the time sound takes to travel one meter.
  • Multiplying by 1000000 gives the same time in microseconds: 2941.17647µs/m.
  • In centimeters, this is approximately 29.41µs/cm.
  • Since the signal travels to the object and back, we need to consider this as well.

[:de]

Abstandsmessung mit dem Ultraschall Distanz Sensor HC-SR04

HC-SR04 Ultraschall SensorDer HC-SR04 ist ein Ultraschall-Abstandssensor der sehr einfach anzusteuern ist und Preisgünstig zu bekommen ist. Das Arbeitsprinzip ist recht einfach: Ähnlich wie bei einem Sonar, sendet der Sensor einen Schallpuls aus der vom Sensor ausgeht und eventuell von einem Objekt reflektiert wird. Dabei wandert der Schall vom Sensor zum Objekt, wird dort reflektiert und kommt als Echo wieder zurück zum Sensor. Der Sensor hat eine eingebaute Signalverabeitung, die ein evtl Echo sofort anzeigt. Die Zeit, die bis zum Empfang des Echos vergeht, nennt man auch die Laufzeit. Wenn man diese Zeit mit dem Arduino misst, kann man mit Hilfe der Schallgeschwindigkeit den Abstand messen.

Angesteuert wird der Sensor folgendermaßen: Bei einer fallenden Flanke am Triggereingang des Sensors, die länger als 10µs dauern muss,  wird das Senden eines Ultraschallsignals vorbereitet. Sobald dieses gesendet wurde, wechselt der Echo Pin von LOW auf HIGH. Wenn das Echo des Ultraschallimpulses empfangen wurde, wechselt der Echo Pin wieder auf Low. Für eine Abstandsmessung muss also lediglich die Zeit gemessen werden, für die der Echo Pin auf HIGH steht und die Zeit muss dann in eine Streck umgewandelt werden.

Hier ist die Beschaltung:

Ultraschall_Steckplatine

Der Code gestaltet sich, dank der Standardlibraries, die mit dem Arduino mitgeliefert werden, auch sehr einfach.

//*******************
// Distance HC-SR04
// Madgyver.de
//*******************

#define echopin 12
#define triggerpin 13
#define speedfactor 29.41/2 //half of the time for Microseconds needed for one centimeter


void setup() {
  Serial.begin(9600);
  pinMode(triggerpin, OUTPUT);
  pinMode(echopin, INPUT);
}

void loop() {
  Serial.println(measure() / speedfactor);
  delay (10);
}

int measure() {
  int duration = 0;

  //sending start condition
  digitalWrite(triggerpin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerpin, LOW);

  //measure time of flight in microseconds
  duration = pulseIn(echopin, HIGH, 10000); //10ms timeout

  return duration;
}

Der Code tut genau das, was weiter oben beschrieben wurde, daher werde ich nur auf die Umrechnung eingehen. Die Funktion measure gibt uns eine Zahl, die der Zeit entspricht bis das Echo empfangen wurde. Der Wert wird in Mikrosekunden angegeben. Der Umrechungsfaktor ergibt sich wie folgt:

  • Schall hat bei normalen Raumtemperaturen eine Geschwindigkeit von etwa 340m/s
  • der Kehrwert davon ist 0.00294117647s/m, dies ist die Zeit der Schall für das zurücklegen von einem meter braucht.
  • Mal 1000000 ergibt diese selbe Zeit in Mikrosekunden gemessen: 2941.17647µs/m
  • in Zentimetern ergibt das etwa 29.41µs/cm
  • Da das Signal einmal hin und wieder zurück wandert, müssen wir dies natürlich auch noch berücksichtigen

[:]


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.