How to Use Ultrasonic Sensor HC-SR04 with Arduino: A Beginner’s Guide

Introduction

In this tutorial, we will learn how to use the Ultrasonic Sensor HC-SR04 with Arduino to measure distances accurately. Ultrasonic sensors are perfect for obstacle detection and distance measurement in various DIY electronics projects. We’ll cover how the sensor works, wiring it to the Arduino, and a simple test code to get started.


What is the Ultrasonic Sensor HC-SR04?

The Ultrasonic Sensor HC-SR04 is a popular, low-cost, and easy-to-use sensor that measures the distance to objects using ultrasonic sound waves. It’s widely used in robotics, automation, and object detection systems.

The sensor works by sending out ultrasonic waves through a transmitter and measuring the time it takes for the echo to return to the receiver. The time taken is then converted into distance.

Key Specifications:

  • Voltage: 5V
  • Measuring Range: 2cm to 400cm
  • Accuracy: ±3mm
  • Operating Frequency: 40kHz
ultrasonic-sensor

Working Principle of Ultrasonic Sensor HC-SR04

The HC-SR04 works by using ultrasonic sound waves, which are sound waves that humans cannot hear. Here’s how it works:

  1. Trigger Signal: The sensor’s trigger pin (TRIG) sends an ultrasonic sound wave.
  2. Echo Signal: The sound wave hits an object, reflects back, and the sensor’s echo pin (ECHO) detects the returning signal.
  3. Time Measurement: The time taken between sending and receiving the sound wave is measured.
  4. Distance Calculation: The sensor calculates the distance using the formula:

 \text{Distance} = \frac{\text{Time (µs)} \times 0.0343}{2}

The division by 2 is because the sound wave travels to the object and back.

ultrasonic-sensor-principle

How to Use the HC-SR04 with Arduino

Components Required:

  • 1x HC-SR04 Ultrasonic Sensor
  • 1x Arduino Board (Uno, Mega, etc.)
  • Jumper Wires
  • Breadboard
ultrasonic-sensor-wiring

Step 1: Wiring the Ultrasonic Sensor HC-SR04 with Arduino

  • Connect the VCC pin of the sensor to the 5V pin on the Arduino.
  • Connect the GND pin to the ground (GND) pin on the Arduino.
  • Connect the TRIG pin to digital pin 9 on the Arduino.
  • Connect the ECHO pin to digital pin 10 on the Arduino.

Test Code for Ultrasonic Sensor HC-SR04 with Arduino

Here’s a simple test code to get started with the Ultrasonic Sensor HC-SR04. This code will print the distance measured by the sensor in the Serial Monitor of the Arduino IDE.

// Define pins
const int trigPin = 9;
const int echoPin = 10;

// Variables for time and distance
long duration;
int distance;

void setup() {
  // Initialize serial communication at 9600 baud rate
  Serial.begin(9600);
  
  // Set the trigPin as output and echoPin as input
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  // Send a pulse to start the measurement
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Read the echoPin and calculate duration of pulse
  duration = pulseIn(echoPin, HIGH);

  // Calculate the distance
  distance = duration * 0.0343 / 2;

  // Print the distance in the serial monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
  
  delay(1000);  // Wait a second before the next measurement
}

Step 2: Upload and Test

  1. Open the Arduino IDE.
  2. Copy and paste the code into the editor.
  3. Connect your Arduino to your computer using a USB cable.
  4. Select the correct board and COM port.
  5. Upload the code to your Arduino.
  6. Open the Serial Monitor (Ctrl + Shift + M) to view the measured distance.

Example Readings:

serial-monitor-readings

Applications of Ultrasonic Sensor HC-SR04

The HC-SR04 Ultrasonic Sensor is widely used in:

  • Obstacle Avoidance Systems: For robots to avoid objects.
  • Distance Measurement: To measure distances in industrial or DIY projects.
  • Water Level Detection: In water tanks to monitor levels.
  • Security Systems: As part of motion or proximity detection systems.

Troubleshooting Tips

  1. Inaccurate Readings: If the sensor gives inaccurate distance readings, ensure the sensor is connected correctly and the wiring is solid.
  2. No Readings: If no readings appear in the Serial Monitor, recheck the connections and confirm that the correct COM port is selected in the Arduino IDE.
  3. Code Errors: Ensure the Arduino libraries are up to date, and verify that the code has no syntax errors.

Conclusion

Using the Ultrasonic Sensor HC-SR04 with Arduino is an easy and effective way to measure distances in your projects. This sensor, combined with the versatility of Arduino, allows for various applications in robotics, automation, and DIY electronics. By following the simple guide and test code provided, you can start incorporating the Ultrasonic Sensor HC-SR04 into your own projects.

ultrasonic-sensor-HC-SR04-with-arduino

You can buy the Ultrasonic sensor HC-SR04 from here:

https://www.sparkfun.com/products/15569


New to Arduino?

You will also like: 0.96 inch I2C OLED Display with Arduino

Leave a Comment