int trigger=PD6; // This Pin bekomms the name Trigger will cause a sound wave as sooon as programm starts int echo=PD5; // If the Trigger Sound wave is reflected from the wall ... // and returns to the sensor, the sensor will send a 5V signal to this pin (echo). long duration=0; // Startvalue for this variable is 0 long distance=0; // Startvalue for this variable is 0 void setup() { Serial.begin (9600); // This is the Baudrate 9600 Bit/second which I will use for the readings pinMode(trigger, OUTPUT); //Pin named "trigger" will be used as output pinMode(echo, INPUT); //Pin named "echo" will be used as output } void loop() // This part of the programm will repeat during the running time of the programm { digitalWrite(trigger, LOW); // Pin PD6 (trigger) gets a 0V signal from the microcontroller board (ATmega328p) delay(5); digitalWrite(trigger, HIGH); //PD6 (trigger) delay(10); digitalWrite(trigger, LOW); duration = pulseIn(echo, HIGH); distance = (duration/2) * 0.03432; if (distance >= 500 || distance <= 0) { Serial.print(0); Serial.print(" "); Serial.println(0); } else { Serial.print(duration); //Serial.print("\ milisecond"); //Serial.print("\t"); Serial.print(" "); Serial.println(distance); //Serial.print("\t"); //Serial.println("cm"); } delay(1000); }