1. Objective
The goal of this lab is to learn how to use the HC-SR04 ultrasonic distance sensor with an Arduino Uno. You will write a program that measures the distance to an object and controls an LED based on that distance. The LED will turn ON when an object is detected within a specific range and turn OFF otherwise.
By the end of this lab, you will be able to:
- Understand the working principle of an ultrasonic sensor.
- Interface the HC-SR04 sensor with an Arduino Uno.
- Use the
pulseIn()function to measure time. - Convert sensor readings into a meaningful unit (centimeters).
- Control an output (LED) based on sensor input using conditional logic (if-else statements).
2. Required Components
- 1 x Arduino Uno R3
- 1 x USB Cable
- 1 x HC-SR04 Ultrasonic Sensor
- 1 x Standard LED (any color)
- 1 x 220Ω Resistor
- 1 x Breadboard
- Jumper Wires (Male-to-Male and Male-to-Female)
3. Circuit Diagram and Wiring
Connect the components according to the table and diagram below. Pay close attention to the pin connections for the HC-SR04 sensor.
| Component Pin | Connects To | Notes |
|---|---|---|
| HC-SR04 VCC | Arduino 5V | Power for the sensor. |
| HC-SR04 Trig (Trigger) | Arduino Digital Pin 9 | Sends the ultrasonic pulse. |
| HC-SR04 Echo | Arduino Digital Pin 10 | Receives the reflected pulse. |
| HC-SR04 GND | Arduino GND | Common ground. |
| LED Anode (Long leg) | One end of the 220Ω Resistor | The other end of the resistor connects to Arduino Digital Pin 7. |
| LED Cathode (Short leg) | Arduino GND | Connect to the same ground rail as the sensor. |
(A visual representation of the breadboard setup would look like this: The sensor is placed on the breadboard. Its VCC and GND are connected to the power rails. Trig and Echo pins are connected to Arduino pins 9 and 10. The LED's long leg is connected via a resistor to pin 7, and its short leg is connected to the ground rail.)
4. Procedure
Step 1: Theory - How it Works
The HC-SR04 sensor works like a sonar. It sends out a short burst of high-frequency sound from its Trigger pin. This sound wave travels through the air, hits an object, and bounces back. The sensor's Echo pin then detects this returning wave.
The Arduino measures the time it takes for the echo to return. Since we know the speed of sound (approximately 340 m/s or 0.034 cm/µs), we can calculate the distance.
The formula is: Distance = (Travel Time / 2) * Speed of Sound
We divide the time by 2 because the measured time is for the sound to travel to the object and back. We only want the one-way distance.
Distance (cm) = Travel Time (in microseconds) / 58. We will use this shortcut in our code.
Step 2: Writing the Arduino Code
Open the Arduino IDE and enter the following code. Read the comments carefully to understand what each line does.
// Lab 4: Ultrasonic Object Detector
// This program measures distance using an HC-SR04 sensor and turns on an LED
// if an object is detected within 20 cm.
// Define the pin numbers for the ultrasonic sensor and the LED
const int trigPin = 9;
const int echoPin = 10;
const int ledPin = 7;
// Define a variable for the detection distance threshold (in cm)
const int detectionDistance = 20;
// Variables to store the duration of the echo pulse and the calculated distance
long duration;
int distance;
void setup() {
// Set up the LED pin as an output
pinMode(ledPin, OUTPUT);
// Set up the sensor pins
pinMode(trigPin, OUTPUT); // Trig pin sends the pulse, so it's an output
pinMode(echoPin, INPUT); // Echo pin receives the pulse, so it's an input
// Start the serial communication to display results on the Serial Monitor
Serial.begin(9600);
Serial.println("Ultrasonic Sensor Test");
}
void loop() {
// --- Step 1: Trigger the sensor ---
// To generate a clean pulse, first make sure the trigPin is low
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Now, send a 10 microsecond high pulse to trigger the sensor
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// --- Step 2: Read the echo pulse ---
// The pulseIn() function reads how long the echoPin stays HIGH.
// This is the time it took for the sound wave to travel to the object and back.
duration = pulseIn(echoPin, HIGH);
// --- Step 3: Calculate the distance ---
// Using the simplified formula: distance (cm) = duration (µs) / 58
distance = duration / 58;
// --- Step 4: Print the distance to the Serial Monitor ---
// This is very useful for debugging and seeing the sensor work.
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// --- Step 5: Control the LED based on the distance ---
// Check if an object is detected within our defined threshold
if (distance < detectionDistance && distance > 0) {
// If yes, turn the LED ON
digitalWrite(ledPin, HIGH);
} else {
// If no, turn the LED OFF
digitalWrite(ledPin, LOW);
}
// Wait for 100 milliseconds before taking the next reading
delay(100);
}
Step 3: Upload and Test
- Connect your Arduino to your computer via USB.
- In the Arduino IDE, go to Tools > Board and select "Arduino Uno".
- Go to Tools > Port and select the correct port for your Arduino.
- Click the "Upload" button to send the code to your Arduino.
- Once uploading is complete, open the Serial Monitor (Tools > Serial Monitor or the magnifying glass icon). Make sure the baud rate is set to 9600.
- Place your hand or an object in front of the sensor. Watch the distance values printed on the Serial Monitor.
- Observe the LED. It should turn on when the object is closer than 20 cm and turn off when it's farther away.
5. Submission
To complete the lab, you must submit a short video of your working project. Your video should clearly show:
- Your complete circuit setup (Arduino, breadboard, sensor, LED).
- Your computer screen with the Arduino IDE and the Serial Monitor running.
- A demonstration of the project working:
- Show the distance reading on the Serial Monitor.
- Move an object (like your hand) towards the sensor until it is within 20 cm.
- Show that the LED turns ON.
- Move the object away from the sensor and show that the LED turns OFF.