Project 5
Ultrasonic Distance Measurement (HC-SR05)- Arduino Uno
Project Description
An ultrasonic sensor measures distance by sending out high-frequency sound waves and detecting their reflected echo. It uses a trigger pin to emit a sound pulse and an echo pin to measure the time it takes for the signal to return. This time is converted into distance, allowing the sensor to accurately detect objects from a few centimeters up to several meters.
Components
- Arduino Uno
- HC-SR05 Ultrasonic Sensor
- Jumper wires
- Breadboard
Code
#define TRIG_PIN 8
#define ECHO_PIN 9
long duration;
float distance;
void setup() {
Serial.begin(9600);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
}
void loop() {
// Clear the trig pin
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
// Send 10µs pulse
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Read the echo pulse
duration = pulseIn(ECHO_PIN, HIGH);
// Calculate distance in cm
distance = duration * 0.034 / 2;
// Print result
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(500);
}
Taking things further
- Robotic collision avoidance when combined with a servo motor legged robot or a wheeled robot
- Parking sensor
- Level sensing when installed in the cover of a liquid or solid container
- Proximity sensing can be implemented by triggering an action such as a buzzer or light when a certain closeness to the sensor is achieved
- Beeper volume control: PWM can be used to control the volume of a buzzer
Power supply


