Using Ultrasonic Sensor to Calculate Distance and Display in Serial Monitor

 

In this activity, students will learn how to use an Ultrasonic Sensor (HC-SR04) with Arduino to measure the distance of an object and display the readings on the Serial Monitor.

 

1. Components Required

Component

Quantity

Description

Arduino Uno/Nano

1

Micro-controller Board

Ultrasonic Sensor (HC-SR04)

1

Measures distance using sound waves

Jumper Wires

As needed

Connects components to Arduino

Breadboard (Optional)

1

For easy connections

USB Cable

1

To upload code and power Arduino

 

2. Understanding the Ultrasonic Sensor (HC-SR04)

The HC-SR04 Ultrasonic Sensor uses sound waves to measure distance. It has four pins:
🔹 VCC → Connect to 5V (Power supply)
🔹 GND → Connect to GND (Ground)
🔹 TRIG → Sends an ultrasonic pulse (Trigger pin)
🔹 ECHO → Receives the reflected signal (Echo pin)

💡 How it Works:

  1. The TRIG pin sends out a short sound pulse (ultrasound).
  2. The sound wave hits an object and reflects back.
  3. The ECHO pin measures the time taken for the sound to return.
  4. Using the speed of sound (343 m/s), the distance is calculated.

👉 Formula to Calculate Distance:

Distance=Time×Speed of Sound/2

(Since the sound travels to the object and back, we divide by 2.)

 

3. Circuit Connections


 

Ultrasonic Sensor Pin

Arduino Pin

VCC

5V

GND

GND

TRIG

D9

ECHO

D10

Diagram Explanation:

  • VCC and GND power the sensor.
  • TRIG pin sends signals from Arduino.
  • ECHO pin receives signals and calculates distance.

 

4. Arduino Code

 #define TRIG_PIN 9   // Define TRIG pin
#define ECHO_PIN 10  // Define ECHO pin

void setup() {
  Serial.begin(9600);   // Start serial communication
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
}

void loop() {
  // Send an ultrasonic pulse
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  // Measure the time taken for the echo
  long duration = pulseIn(ECHO_PIN, HIGH);

  // Convert time into distance (in cm)
  float distance = (duration * 0.0343) / 2;

  // Print distance on Serial Monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  delay(500);  // Wait for 500ms before next measurement
}

5. How to Run the Code

  1. Connect the Arduino to your PC using a USB cable.
  2. Open Arduino IDE and select the correct COM port.
  3. Copy and paste the above code into the Arduino IDE.
  4. Click Upload to send the code to the Arduino.
  5. Open Serial Monitor (Press Ctrl + Shift + M or go to Tools > Serial Monitor).
  6. Move an object in front of the sensor and observe the distance values updating in real time.

 

6. Observations

✅ If an object is closer, the distance value decreases.
✅ If an object is farther, the distance value increases.
✅ If nothing is in front of the sensor, it may display "Out of Range" values.

 

7. Conclusion

🔹 This activity teaches how to measure distance using an ultrasonic sensor.
🔹 The Serial Monitor displays the real-time distance of objects.
🔹 The concept is useful for robotics, automation, and security systems.

🚀 Next Step: Try adding a buzzer or LED to indicate when an object is too close!

Using Ultrasonic Sensor to Calculate Distance and Display in Serial Monitor Using Ultrasonic Sensor to Calculate Distance and Display in Serial Monitor Reviewed by Skill Training on February 11, 2025 Rating: 5
Powered by Blogger.