Arduino – HC-SR04 Distance Sensor

Last Updated on 10. October 2026 by SuffocationLink status: OK

Ansteuerung des HC-SR04 Ultraschall-Entfernungsmessers mit Hilfe des Arduino UNO.

Foundations

HC-SR04 Distance SensorLink status: OK

Structure

Circuit

(Ich habe nachträglich noch die Pins von 0/1 auf 2/3 geändert, weil an 0/1 noch der ISP hängt).

Test programme

Hier ein kleines Testprogramm. Es filtert zu kleine und zu große Werte heraus und ermöglicht die Mittelwertbildung über ein definiertes Intervall.

Source code HCSR04Test.ino (GitHub GistLink status: OK)

// *************************************************
// SR04 Ultrasonic Distance Sensor
// time to distance conversion is
// divide by 2 (both ways to the obstical and back)
// divide by sonic constant 29.1
#define TRIGGER_PIN   3
#define ECHO_PIN      2    
#define SC_MM         5.82
#define SC_CM         58.2

#define MAX_DUR       300 * 58.2 // ~3m in dur 
#define MIN_DUR       2 * 58.2 // ~2cm in dur
#define TO_SMALL      -1
#define TO_BIG        -2
#define INT_TO_SMALL  -3


/** get duration */
long getDur(int interval){
  if(interval < 1) return -3;
  long dur=0;
  
  for(int i = 0; i < interval; i++ ){
    // init sensor
    digitalWrite(TRIGGER_PIN, LOW);
    delayMicroseconds(3);
    noInterrupts();  
  
    // start 10 us pulse
    digitalWrite(TRIGGER_PIN, HIGH); 
    delayMicroseconds(10);
    digitalWrite(TRIGGER_PIN, LOW);
    
    dur += pulseIn(ECHO_PIN, HIGH); // determine the time
    interrupts();
    
  }

  dur /= interval;
  if(dur > MAX_DUR ) { return TO_BIG;}
 // else if (dur < MIN_DUR){ return TO_SMALL;}
  return dur;
}
 
void initDist(){ pinMode(TRIGGER_PIN, OUTPUT); pinMode(ECHO_PIN, INPUT);}

/** get the distance in centi meter*/
double getDistCM(long dur){ return (dur / SC_CM);}

/** get the distance in milli meter*/
long getDistMM(long dur) { return (dur / SC_MM);}
// *************************************************

void setup(){
  Serial.begin(9600); 
  while(!Serial){delay(1);} // wait until the serial is ok (only for Leonardo)
  initDist();
}

void loop (){
  
  Serial.print("Current distance is: "); 
  long dur = getDur(5);
  if(dur > 0) Serial.println(getDistCM(dur));
  else if( dur == TO_BIG) Serial.println("TO FAR");
  else if( dur == TO_SMALL) Serial.println("TO NEAR");
  delay(1000); // wait a second
}

SR04Test.inoLink status: OK

Hier könnte jetzt noch der Median angewendet werden, um Ausreißer herauszunehmen.

In der Praxis bietet es sich wahrscheinlich an, die Millimeter wegzukürzen und nur die Zentimeterwerte, die wesentlich geringeren Schwankungen unterliegen, zu verwenden.

Output

Related Posts

TF Mini Laser RangefinderLink status: OK
RC car with PS2 controllerLink status: OK
US-100Link status: OK
HC-SR04 Distance SensorLink status: OK
US-015Link status: OK

Sources

DateChangeAuthor
10.10.2026Gist-Einbettung durch Code-Block ersetzt (Gist als Quelle verlinkt)Suffocation
08.10.2026Überschriften-Hierarchie korrigiert (H1→H2, Unterebenen eine Stufe tiefer)Suffocation
04.10.2026Rechtschreibung korrigiert: Tippfehler, Kommas, GrammatikSuffocation
03.10.2026Log-Liste eingefügtSuffocation
24.03.2018Post createdSuffocation
Changelog

1 thought on “Arduino – HC-SR04 Entfernungsmesser”

Leave a Reply

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