0
0
AutocadHow-ToBeginner · 4 min read

How to Measure Distance Using Arduino: Simple Guide

To measure distance using Arduino, connect an ultrasonic sensor like the HC-SR04 and use digitalWrite and pulseIn functions to send and receive sound pulses. Calculate distance by timing the echo and converting it to centimeters or inches.
📐

Syntax

Use the following basic steps to measure distance with an ultrasonic sensor:

  • digitalWrite(triggerPin, HIGH): Send a short pulse to start measurement.
  • delayMicroseconds(10): Wait 10 microseconds for the pulse.
  • digitalWrite(triggerPin, LOW): Stop the pulse.
  • pulseIn(echoPin, HIGH): Measure the time for the echo to return.
  • Calculate distance using the formula: distance = duration * 0.034 / 2 (speed of sound in cm/us divided by 2 for round trip).
arduino
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
long duration = pulseIn(echoPin, HIGH);
float distance = duration * 0.034 / 2;
💻

Example

This example shows how to measure distance using an HC-SR04 ultrasonic sensor connected to Arduino pins 9 (trigger) and 10 (echo). It prints the distance in centimeters to the Serial Monitor.

arduino
#define triggerPin 9
#define echoPin 10

void setup() {
  Serial.begin(9600);
  pinMode(triggerPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(2);

  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);

  long duration = pulseIn(echoPin, HIGH);
  float distance = duration * 0.034 / 2;

  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  delay(500);
}
Output
Distance: 15.23 cm Distance: 15.10 cm Distance: 15.30 cm ...
⚠️

Common Pitfalls

Common mistakes when measuring distance with Arduino include:

  • Not setting the trigger pin LOW before sending the pulse, causing incorrect timing.
  • Using delay() instead of delayMicroseconds() for the 10 microsecond pulse.
  • Incorrect wiring of the sensor pins.
  • Ignoring the sensor's minimum and maximum range limits.
  • Not allowing enough time between measurements.

Always check wiring and use precise timing functions.

arduino
/* Wrong way: Using delay() for pulse length */
digitalWrite(triggerPin, HIGH);
delay(10); // Incorrect: 10 milliseconds instead of microseconds

/* Right way: Use delayMicroseconds() */
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10); // Correct: 10 microseconds
📊

Quick Reference

Tips for measuring distance with Arduino and ultrasonic sensors:

  • Use delayMicroseconds(10) for the trigger pulse.
  • Calculate distance with distance = duration * 0.034 / 2.
  • Connect sensor trigger to an output pin and echo to an input pin.
  • Allow at least 60ms between measurements to avoid interference.
  • Use Serial Monitor to view distance readings.

Key Takeaways

Use an ultrasonic sensor like HC-SR04 with Arduino pins for trigger and echo.
Send a 10 microsecond pulse on trigger pin using delayMicroseconds.
Measure echo pulse duration with pulseIn to calculate distance.
Calculate distance by multiplying duration by 0.034 and dividing by 2.
Avoid common timing mistakes and check wiring carefully.