____       _          ____                        
|  _ \ ___ | |__   ___/ ___| _   _ _ __ ___   ___  
| |_) / _ \| '_ \ / _ \___ \| | | | '_ ` _ \ / _ \ 
|  _ < (_) | |_) | (_) |__) | |_| | | | | | | (_) |
|_| \_\___/|_.__/ \___/____/ \__,_|_| |_| |_|\___/ 
                                                   
  ____            _    _                 _    
 / ___|___   ___ | | _| |__   ___   ___ | | __
| |   / _ \ / _ \| |/ / '_ \ / _ \ / _ \| |/ /
| |__| (_) | (_) |   <| |_) | (_) | (_) |   < 
 \____\___/ \___/|_|\_\_.__/ \___/ \___/|_|\_\

Introducing the HC-SR04 ultrasonic rangefinder

insert alt text here

insert alt text here

//
// HC-SR04 rangefinder example
// Written by Ted Burke - 8-Mar-2023
//

void setup()
{
  // LED output
  pinMode(2, OUTPUT); // red LED
  pinMode(3, OUTPUT); // green LED

  // Trigger and echo pins for rangefinder
  pinMode(8, OUTPUT); // trigger pin
  pinMode(9, INPUT);  // echo pin

  // Open serial connection to PC at 9600 bits/s
  Serial.begin(9600);
}

void loop()
{
  unsigned long Techo;
  double distance;

  // Send trigger pulse from Arduino to HC-SR04
  digitalWrite(8, HIGH);
  delayMicroseconds(20); // 20 us pulse
  digitalWrite(8, LOW);

  // Measure duration of the echo pulse in microseconds
  Techo = pulseIn(9, HIGH);

  // Calculate the distance
  distance = (340e-6 * Techo) / 2.0;

  // Print the distance
  Serial.println(distance);

  // Short delay to allow any echoes to dissipate
  delay(50);
}

This alternative version of the example program controls two LEDs based on the measured distance.

More importantly, it uses a separate function to perform the measurement process, which allows the loop function to take a measurement just by calling the getDistance function, as follows:

d = getDistance();

This is the full example:

//
// HC-SR04 rangefinder example
// Written by Ted Burke - 8-Mar-2023
//

void setup()
{
  // LED output
  pinMode(2, OUTPUT); // red LED
  pinMode(3, OUTPUT); // green LED

  // Trigger and echo pins for rangefinder
  pinMode(8, OUTPUT); // trigger pin
  pinMode(9, INPUT);  // echo pin

  // Open serial connection to PC at 9600 bits/s
  Serial.begin(9600);
}

void loop()
{
  double d;

  d = getDistance();

  Serial.println(d);

  if (d < 0.5)
  {
    // Nearest object is closer than 0.5 metres 
    digitalWrite(2, LOW);  // red LED off
    digitalWrite(3, HIGH); // green LED on
  }
  else
  {
    // Nearest object is farther than 0.5 metres 
    digitalWrite(2, HIGH); // red LED on
    digitalWrite(3, LOW);  // green LED off
  }
}

// This function takes a measurement and returns the
// distance in metres as a double precision floating
// point value.
double getDistance()
{
  // Declare variables
  unsigned long Techo; // echo pulse duration in microseconds
  double distance;     // distance in metres

  // Short delay to allow any possible echoes from
  // previous ultrasonic pulses to dissipate
  delay(50);

  // Send trigger pulse from Arduino to HC-SR04
  digitalWrite(8, HIGH);
  delayMicroseconds(20); // 20 us pulse
  digitalWrite(8, LOW);

  // Measure duration of the echo pulse (sent from HC-SR04
  // to Arduino) in microseconds
  Techo = pulseIn(9, HIGH);

  // Calculate the distance in metres
  // - 340x10^-6 is the speed of sound in metres per microsecond
  // - Techo is the duration of the echo pulse in microseconds
  distance = (340e-6 * Techo) / 2.0;

  // Return the calculated distance to the calling function (loop)
  return distance;
}