Distance Measurer

In this project, we will build a distance measurer that can measure distances up to 4 meters using the HC-SR04 Ultrasonic Sensor and ESP32.

Wiring

HC-SR04    ESP32
-------    -------
VCC        3.3V
Trig       23
Echo       22
GND        GND
C++

Diagram

Arduino Code

const int trigPin = 23;
const int echoPin = 22;

void setup() {
	Serial.begin(115200);
	pinMode(trigPin, OUTPUT);
	pinMode(echoPin, INPUT);
}

void loop() {
	digitalWrite(trigPin, LOW);
	delayMicroseconds(2);
	digitalWrite(trigPin, HIGH);
	delayMicroseconds(10);
	digitalWrite(trigPin, LOW);

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

	Serial.printf("Distance: %.1f cm\n", distance);
	delay(100);
}
C++

Code Explained

1. Pin Definitions

const int trigPin = 23;
const int echoPin = 22;
C++

2. Setup Function

pinMode(trigPin, OUTPUT);    
pinMode(echoPin, INPUT); 
C++

The trigger pin is configured as an output, and the echo pin is configured as an input to handle signals.

3. Main Loop

The HC-SR04 ultrasonic sensor measures distance by sending out sound waves and measuring how long it takes for them to bounce back. The first step is to trigger the HC-SR04 to emit an ultrasonic sound wave by send a small 10 microseconds pluse.

digitalWrite(trigPin, LOW);    // Clears the trigger pin
delayMicroseconds(2);          // Waits for 2 microseconds
C++

Before starting the new pluse sequence, we need to clear the trigger pin by ensuring the trigger pin is set to LOW. A small delay of 2 microseconds is included to ensure the HC-SR04 receives and handle the LOW state from triggle pin. It gives enough time for the system to stabilize before next step.

digitalWrite(trigPin, HIGH);   // Sends a 10-microsecond pulse
delayMicroseconds(10);         
C++

The trigger pin is set to HIGH for 10 microseconds, then turn it off low set it back to LOW state.

In HC-SR04 ultrasonic sensor datasheet, it requires a pulse of at least 10 microseconds to start emitting the ultrasonic waves. This is the minimum pulse length the sensor needs to recognize as a trigger by HC-SR04. The HC-SR04 sensor will emit an ultrasonic wave for the duration of the HIGH pulse.

digitalWrite(trigPin, LOW);    // Clears the trigger pin
C++

After the 10-microsecond pulse, the trigger pin must be cleared (set to LOW) so that the ultrasonic sensor will stop emitting waves. The LOW state indicates the end of the pulse.

long duration = pulseIn(echoPin, HIGH);
C++

The Echo pin goes HIGH when the ultrasonic burst is sent and stays HIGH until the echo is received, then it goes low. The pulseIn function measures the time (in microseconds) that the echo pin remains HIGH. This duration corresponds to the time taken for the ultrasonic wave to travel to the object and back.

float distance = (duration * 0.034) / 2; 
C++

This line converts the duration (in microseconds) into a distance measurement (in centimeters).

The ultrasonic sensor works by emitting a sound wave and waiting for the echo to return. The speed of sound is constant in air, and the duration of the pulse can be used to calculate the distance.

The speed of sound in air is approximately 343 meters per second (or 0.0343 cm/µs). For simplicity, we use 0.034 cm/µs as a rough estimate of the speed of sound in air.

$$ \text{Distance (cm)} = \left(\frac{\text{Duration (µs)} \times \text{Speed of Sound (cm/µs)}}{2}\right) $$

The duration represents the round-trip time for the sound wave (there and back). The division by 2 accounts for the round-trip time, converting it to the actual distance to the object.

Example Calculation:

If duration = 1000 µs (microseconds):

  • Distance = (1000 µs * 0.034 cm/µs) / 2
  • Distance = (34 cm) / 2
  • Distance = 17 cm

Thus, the object is 17 cm away from the sensor.

Serial.printf("Distance: %.1f cm\n", distance); 
delay(100);
C++


The calculated distance is printed to the serial monitor in centimeters, with one decimal precision. A small delay of 100 milliseconds is added to control the frequency of distance measurements.

Example Output
When monitored in the serial console, it might display:

Distance: 15.3 cm
Distance: 15.4 cm
Distance: 15.2 cm
C++

This shows the distance to an object in front of the sensor in centimeters.

RELATED ARTICLES