# Project - 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 Arduino.

# Wiring
```
HC-SR04 Arduino
------- -------
VCC 3.3V
Trig 3
Echo 4
GND GND
```
# Code
```cpp
const int trigPin = 3;
const int echoPin = 4;
void setup() {
Serial.begin(9600);
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);
}
```
# Code Explained
**1. Pin Definitions**
```cpp
const int trigPin = 3;
const int echoPin = 4;
```
**2. Setup Function**
```cpp
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
```
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.
```cpp
digitalWrite(trigPin, LOW); // Clears the trigger pin
delayMicroseconds(2); // Waits for 2 microseconds
```
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.
```cpp
digitalWrite(trigPin, HIGH); // Sends a 10-microsecond pulse
delayMicroseconds(10);
```
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.
```cpp
digitalWrite(trigPin, LOW); // Clears the trigger pin
```
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.
```cpp
long duration = pulseIn(echoPin, HIGH);
```
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.
```cpp
float distance = (duration * 0.034) / 2;
```
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.
```cpp
Serial.printf("Distance: %.1f cm\n", distance);
delay(100);
```
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:
```cpp
Distance: 15.3 cm
Distance: 15.4 cm
Distance: 15.2 cm
```
This shows the distance to an object in front of the sensor in centimeters.
Project - Distance Measurer
RELATED ARTICLES
Photoresistor Sensor
Project - Temperature Measurement
NTC Temperature Sensor
Project - IR Sender
Project - IR Receiver
Remote infrared Sensor
Project - Motion Detection Alarm
- Choosing a selection results in a full page refresh.