get-started-with-arduino-for-beginners-lesson-8-ultrasonic-sensor
|

How to Use an Ultrasonic Sensor with Arduino: Beginner’s Guide

Introduction

Ultrasonic sensors are widely used in electronics and robotics to detect objects and measure distances without physical contact. They are perfect for applications like obstacle avoidance in robots, parking sensors, and distance measurement devices. This guide will walk you through understanding, wiring, and coding an ultrasonic sensor using an Arduino.

What is an Ultrasonic Sensor?

An ultrasonic sensor is a device that measures distance by emitting ultrasonic sound waves and calculating the time it takes for the sound to reflect from an object. These sensors are great for non-contact distance measurement, making them useful in various applications.

Types of Ultrasonic Sensors

Several types of ultrasonic sensors are available, but for this guide, we will focus on the HC-SR04 ultrasonic sensor, one of the most common and beginner-friendly sensors used with Arduino.

  • HC-SR04: This sensor emits a short pulse of sound at a frequency beyond human hearing (ultrasonic) and listens for the sound to bounce back. It calculates the distance by measuring the time between sending the sound and receiving the echo.
  • Other Ultrasonic Sensors: While the HC-SR04 is the most commonly used, other types, like the US-100 and PIR-based ultrasonic sensors, work similarly but may have different specifications, ranges, or features.

Use cases of ultrasonic sensors

Ultrasonic sensors are highly versatile and can be used in various practical applications. In robotics, they help detect obstacles, allowing robots to navigate around objects. They are commonly found in parking sensors, where they measure the distance between a vehicle and nearby objects to provide feedback when reversing. Additionally, ultrasonic sensors help create simple distance measurement devices, enabling users to measure the distance between the sensor and an object. They are also employed in liquid-level monitoring systems, where they can detect the liquid level in a container without requiring direct contact.

How an Ultrasonic Sensor Works

The HC-SR04 sensor operates by sending out ultrasonic waves and listening for their echo. Here’s a simple explanation of how it works:

  1. Triggering the Sensor: The sensor has a trigger pin that, when set HIGH for 10 microseconds, sends an ultrasonic pulse from the sensor’s transmitter.
  2. Echo Detection: The pulse travels through the air until it hits an object and then reflects on the sensor. The echo pin measures the time it takes for the pulse to return.
  3. Calculating Distance: The Arduino calculates the distance based on the time the echo returns. Since sound travels at a known speed (approximately 343 meters per second in air), the distance can be calculated using the formula:
Distance (cm)=Time (µs)×0.03432\text{Distance (cm)} = \frac{\text{Time (µs)} \times 0.0343}{2}Distance (cm)=2Time (µs)×0.0343

The factor of 2 is because the sound wave travels to the object and back.

how-ultasonic-sensor-works
How an Ultrasonic Sensor Works

Connect Ultrasonic Sensor with Arduino

Wiring Diagram

To use the HC-SR04 ultrasonic sensor with Arduino, connect four pins: VCC, GND, Trig (Trigger), and Echo. Below is a simple wiring guide:

  1. VCC: Connect to the 5V pin on the Arduino.
  2. GND: Connect to the GND pin on the Arduino.
  3. Trig: Connect to digital pin 9 on the Arduino.
  4. Echo: Connect to digital pin 10 on the Arduino.
ultrasonic-sensor-circuit-diagram

Code

Now that the sensor is wired, let’s write the Arduino code to trigger the sensor and read the distance. This code will trigger the ultrasonic sensor, read the echo time, and convert it into the distance in centimeters.

#define trigPin 9
#define echoPin 10

void setup() {
  Serial.begin(9600);  // Start the serial communication for displaying the results
  pinMode(trigPin, OUTPUT);    // Set the trigPin as output
  pinMode(echoPin, INPUT);     // Set the echoPin as input
}

void loop() {
  long duration, distance;

  // Send a 10-microsecond pulse to the trigger pin to start the measurement
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Read the time it takes for the echo to return
  duration = pulseIn(echoPin, HIGH);

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

  // Display the distance in the Serial Monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  delay(500);  // Wait half a second before measuring again
}

Explanation of Code:

  1. pinMode(): This setting configures the trigger pin as an output (to send signals) and the echo pin as an input (to receive signals).
  2. Triggering the Sensor: The code sends a 10-microsecond pulse to the trigger pin to start the measurement process.
  3. Reading the Echo: The pulseIn() function measures the time the echo pin stays HIGH, representing the time the ultrasonic pulse returns.
  4. Calculating the Distance: The distance is calculated by multiplying the pulse duration by the speed of sound in cm/µs (0.0343) and dividing by 2 (since the pulse travels to the object and back).
  5. Displaying the Distance: The calculated distance is printed to the Serial Monitor.

Conclusion

In conclusion, you can integrate an ultrasonic sensor with Arduino to create precise distance measurement systems and innovative projects. You understand how the sensor works, wire it correctly, and program it effectively to implement this technology in applications like obstacle detection, automated parking systems, or robotics. This tutorial builds a strong foundation for using ultrasonic sensors in your projects and empowers you to explore more advanced concepts. You combine the sensor with other components like motors or displays to build dynamic and interactive systems. Your creativity defines the endless possibilities with ultrasonic sensors and Arduino!

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *