const int triggerPin = 6; //we'll set up a global variable for our trigger pin as pin 6 const int echoPin = 7; //we'll set up a global variable for echo pin as pin 7 void setup() { pinMode(triggerPin, OUTPUT); //We need to set up our arduino's trigger pin as an OUTPUT pinMode(echoPin, INPUT); //We also need to set up the arduino pin connected to echo as an INPUT Serial.begin(9600); //We'll start the serial monitor and spit out our actual measurements there } void loop() { digitalWrite(triggerPin, HIGH); //We need to send a pulse to our trigger pin of at least 10 microseconds delayMicroseconds(10); digitalWrite(triggerPin, LOW); int echoTime = pulseIn(echoPin, HIGH); //We cheat and use the pulseIn instruction to simply time the HIGH pulse coming from our echo pin //Now let's do some maths float cmDistance = (echoTime / 2) * 0.0344; Serial.print("Distance: "); Serial.print(cmDistance); Serial.println(" mm, "); delay(250); }