Ultrasonic Sensor Actual Response Time

Hi, I'm working on the usual obstacle detection using HC-SR04. Basically, when an obstacle is detected, a vibration motor will switch on. Obstacle detection is just a part of my study. My professor requested me to measure the response time. Meaning, the time difference between detection of the hc-sr04, and the activation of the vibration motor. I know it's less than a second. Does anyone know a thing or two on how to do it? Thank you.

If you need anything like the code, photo, or whatsoever, just tell me. Thank you again.

1 Like

You’ll find it is fast. Time is wasted in the delay between pings

#include <NewPing.h>
#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
#define anypin 5 // your motorpin
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
void setup() 
{
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}
void loop() 
{
  delay(50);   // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  if (sonar.ping_cm()) // detect
  { long detect_time=micros(); // mark time
    // now trigger the motor
    digitalWrite(anypin,HIGH);
    long elapsed=micros()-detect_time;
    Serial.println(elapsed);
  }
}

Thank you knut_ny. Since it is in microseconds, I guess I won't be able to measure it using any sort of actual or physical timer. Maybe I should settle with implementing it in code. I will try your suggestion. Thank you. :)

Worst case if an object appears @ 2m:

50ms delay in the code
send a ping (ca 20 microseconds)
sound travel time (330m/s) 4/330 => 12 ms
detection… microseconds

Sum: <63 ms

1 Like