Ultrasonic Sensor with Arduino

Introduction

Ultrasonic sensors are widely used in robotics, automation, and measurement applications due to their ability to detect objects and measure distances accurately. When combined with an Arduino microcontroller, they offer a cost-effective and reliable way to implement obstacle detection, distance measurement, and various other interactive projects.

This article provides a detailed overview of how ultrasonic sensors work, how to interface them with an Arduino, and their applications in real-world scenarios.

What is an Ultrasonic Sensor?

An ultrasonic sensor is an electronic device that uses sound waves to measure the distance to an object. It emits high-frequency sound waves (above human hearing range) and calculates the time taken for the echo to return after hitting an obstacle.

Working Principle

  1. The sensor emits an ultrasonic pulse (typically at 40 kHz).
  2. The sound wave travels through the air and bounces back when it hits an object.
  3. The sensor detects the reflected wave and calculates the time delay.
  4. Using the speed of sound (approximately 343 m/s at room temperature), the distance to the object is computed using the formula:

Components Required

  • Arduino Uno (or any compatible board)
  • HC-SR04 Ultrasonic Sensor
  • Jumper Wires
  • Breadboard
  • Power Source (USB cable or battery)

HC-SR04 Ultrasonic Sensor

The HC-SR04 is one of the most commonly used ultrasonic sensors for Arduino projects. It consists of four pins:

PinDescription
VCCPower supply (5V)
TrigTrigger pin (input)
EchoEcho pin (output)
GNDGround

Interfacing Ultrasonic Sensor with Arduino

Wiring Diagram

HC-SR04 PinArduino Pin
VCC5V
TrigD9
EchoD10
GNDGND

Arduino Code

#define TRIG_PIN 9
#define ECHO_PIN 10

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

void loop() {
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  long duration = pulseIn(ECHO_PIN, HIGH);
  float distance = duration * 0.0343 / 2;

  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  delay(500);
}

Explanation of Code

  • The TRIG_PIN sends a 10-microsecond pulse to trigger the ultrasonic signal.
  • The ECHO_PIN receives the reflected wave.
  • The pulseIn function calculates the duration for the sound to return.
  • The distance is computed using the speed of sound.
  • The measured distance is displayed on the Serial Monitor.

Applications of Ultrasonic Sensors

1. Obstacle Avoidance in Robotics

  • Used in autonomous robots and drones to detect and avoid obstacles.
  • Helps in navigation by providing real-time distance measurement.

2. Distance Measurement Tools

  • Used in digital rulers, parking sensors, and smart measuring devices.

3. Liquid Level Detection

  • Measures the level of liquid in tanks without direct contact.
  • Used in industrial automation and smart water monitoring systems.

4. Security and Surveillance

  • Motion detection systems use ultrasonic sensors to detect unauthorized movement.
  • Used in alarm systems and smart security devices.

5. Home Automation

  • Used in automatic door opening systems.
  • Smart lighting systems activate when a person enters a room.

Advantages of Using Ultrasonic Sensors with Arduino

  • Non-Contact Measurement: Ideal for measuring distances without physical contact.
  • Affordable & Easy to Use: Cost-effective and easily available for DIY projects.
  • High Accuracy: Provides precise distance measurements within a few millimeters.
  • Works in Various Environments: Functions well in dark and dusty environments where optical sensors may fail.

Limitations of Ultrasonic Sensors

  • Affected by Temperature & Humidity: Changes in air conditions can affect accuracy.
  • Limited Detection Angle: Objects outside the detection cone may not be detected.
  • Sensitivity to Soft Surfaces: Sound waves may be absorbed by soft materials, reducing effectiveness.
Categories: Uncategorized