How to Measure Distance with an Ultrasonic Sensor and Control LED with Arduino: A Step-by-Step Guide

Distance Measurement Using an Ultrasonic Sensor and LED Control with Arduino

Distance Measurement Using an Ultrasonic Sensor and LED Control with Arduino

In many applications, it's essential to measure distances, whether for object detection, proximity sensing, or automation purposes. One popular tool for this is the ultrasonic sensor, often used in conjunction with Arduino microcontrollers to create interactive systems. In this article, we will learn how to use an ultrasonic sensor to measure distance and control an LED light based on that measurement. We will discuss how the system works, provide the necessary code, and include a diagram to help visualize the setup.

Components Needed

  • Arduino Board (Uno, Nano, etc.)
  • Ultrasonic Sensor (HC-SR04)
  • LED Light
  • Resistor (220 ohms for the LED)
  • Breadboard
  • Jumper wires

Understanding the Components

1. Arduino

The microcontroller platform used to program and control devices. It receives signals from the ultrasonic sensor and uses the logic in the program to activate the LED light accordingly.

2. Ultrasonic Sensor

The HC-SR04 is the most commonly used ultrasonic sensor. It has two main components:

  • Trigger Pin: This pin sends out a pulse that travels in the air.
  • Echo Pin: This pin receives the reflected pulse after it hits an object.

The sensor works by emitting ultrasonic waves and calculating the time it takes for the waves to reflect back from an object. The distance is then calculated using the speed of sound (approximately 343 meters per second at room temperature).

3. LED Light

A light-emitting diode that will be controlled by the Arduino based on the measured distance. If the object is within a certain range, the LED will turn on.

Principle of Operation

The ultrasonic sensor works using the principle of time-of-flight. It emits sound waves at a high frequency, which then bounce back when they hit an object. By measuring the time taken for the sound waves to return, the sensor can calculate the distance.

The formula for calculating distance based on time is:

Distance = (Time × Speed of Sound) / 2

We divide by 2 because the sound travels to the object and then back to the sensor, covering double the distance.

The Arduino can then use this distance measurement to perform actions, such as turning on an LED when the object is close.

How the System Works

  1. The Arduino sends a signal to the ultrasonic sensor's trigger pin.
  2. The sensor emits ultrasonic waves.
  3. These waves travel until they hit an object, then reflect back to the sensor.
  4. The echo pin receives the reflected waves, and the Arduino measures the time it took for them to return.
  5. Based on the time, the Arduino calculates the distance to the object.
  6. If the object is within a specific range (e.g., less than 10 cm), the Arduino turns on the LED. If it is outside this range, the LED stays off.

Wiring the Circuit

Here’s how to wire up the ultrasonic sensor and LED:

  • Ultrasonic Sensor:
    • VCC pin to 5V on the Arduino.
    • GND pin to GND on the Arduino.
    • TRIG pin to digital pin 9 on the Arduino.
    • ECHO pin to digital pin 10 on the Arduino.
  • LED:
    • The longer leg (anode) of the LED connects to digital pin 13 on the Arduino.
    • The shorter leg (cathode) connects to GND through a 220-ohm resistor to limit the current flowing through the LED.

Code Implementation

Now, let’s write the Arduino code to make this system work:

const int trigPin = 9;
const int echoPin = 10;
const int ledPin = 13;

long duration;
int distance;

void setup() {
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(ledPin, OUTPUT);
}

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

  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.034 / 2;

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

  if (distance < 10) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }

  delay(500);
}
        

Circuit Diagram

Here is a simple diagram for wiring the ultrasonic sensor and LED to the Arduino:

               +5V --------------------------------------- VCC (HC-SR04)
               GND ------------------------------------ GND (HC-SR04)
                      |
               TRIG ------------------------- Pin 9 (Arduino)
               ECHO ------------------------ Pin 10 (Arduino)
                      |
                    LED
                      |
                  Pin 13 (Arduino)
                      |
                   GND  ------------------------ GND (Arduino)
                  Resistor (220 ohms)  (Optional, connected in series with LED)
    

How to Test the System

  1. Upload the code to the Arduino board.
  2. Connect the ultrasonic sensor and LED according to the diagram.
  3. Open the Serial Monitor in the Arduino IDE to observe the distance readings.
  4. Place an object within 10 cm of the sensor to trigger the LED to turn on.
  5. If the object moves away (greater than 10 cm), the LED will turn off.

Applications

  • Proximity Sensors: This system can be used in robotics, where the robot needs to detect obstacles and respond accordingly by lighting an indicator or stopping.
  • Automatic Lighting Systems: When an object (like a hand or person) approaches, the LED can act as a form of automated lighting.
  • Parking Sensors: Ultrasonic sensors are often used in vehicles to help drivers detect obstacles and park more easily.

Conclusion

The combination of an ultrasonic sensor and an LED controlled by an Arduino is a simple yet effective way to measure distances and respond to environmental changes. By understanding how the sensor works and writing the appropriate code, you can create a wide variety of applications that involve proximity sensing. Whether it's for robotics, automation, or simple home projects, this basic setup can be the foundation for more advanced systems.

Post a Comment

0 Comments