LED Distance Indicator Using Ultrasonic Sensor

by mansiramteke139 in Circuits > Sensors

23 Views, 0 Favorites, 0 Comments

LED Distance Indicator Using Ultrasonic Sensor

20250102_143627[1].jpg
20250102_143642[1].jpg
20250102_143648[1].jpg
20250102_143724[1].jpg

Introduction to LED Distance Indicator Using Ultrasonic Sensor:

The LED Distance Indicator is a project that combines an ultrasonic sensor and an LED to visually represent the distance between an object and the sensor. By leveraging the ultrasonic sensor to measure distance and using an LED to display that information in a visually intuitive way, the system offers a simple yet effective means of interacting with the environment. This type of project can be applied in various fields, such as robotics, proximity detection, and distance measurement.

the ultrasonic sensor measures the distance to an object by emitting high-frequency sound waves and calculating the time it takes for the waves to bounce back. The LED serves as an indicator of the distance: when the object is far, the LED might dim, and as the object moves closer, the LED brightness increases.

Key Components:

Ultrasonic Sensor (HC-SR04):

  1. Trigger Pin: Initiates the measurement process by sending out a short pulse.
  2. Echo Pin: Receives the reflected pulse and calculates the distance based on the time taken for the echo to return.
  3. The sensor operates by sending a sound wave, measuring how long it takes for the wave to reflect off the object, and calculating the distance based on that time.

LED:

  1. The LED acts as an output to display the distance measured by the ultrasonic sensor. Using Pulse Width Modulation (PWM), the LED brightness is varied depending on the distance detected
  2. As the distance decreases, the LED brightness increases, providing a visual cue about the proximity of an object.



Supplies

Screenshot_20250108_172816_Google[1].jpg
Screenshot_20250108_172856_Google[1].jpg
Screenshot_20250108_172955_Google[1].jpg
Screenshot_20250108_173058_Google[1].jpg
Screenshot_20250108_175155_Google[1].jpg
Screenshot_20250108_175122_Google[1].jpg

Here, I'm going to explain you how to interface Arduino with ultrasonic sensor and LED's & how to indicate distance through LED.

Components:

1. Arduino

2. Ultrasonic sensor HCSR-04

3. LED's (05)

4. Resistor (150ohm)

5. Some jumper cables

6. Breadboard

Circuit Connections:

1. VCC → 5V (on the Arduino)

2. GND → GND (on the Arduino)

3. Trigger (TRIG) → Pin 9 (on the Arduino)

4. Echo (ECHO) → Pin 10 (on the Arduino)

Pin Setup: The trigPin sends a pulse to trigger the ultrasonic sensor, and the echoPin receives the pulse back. The ledPin controls the LED's brightness.

Ultrasonic Sensor Logic:

  1. The pulseIn() function measures the time it takes for the ultrasonic pulse to return to the sensor.
  2. The distance is calculated using the formula: Distance=Duration×Speed by 2
  3. The map() function is used to convert the distance into a value that can control the LED brightness. The constrain() function ensures the brightness is between 0 and 255.

LED Control: The LED brightness is adjusted based on the distance. When the object is far, the LED is dim, and as the object approaches, the LED gets brighter.

Connection

20250102_143648[1].jpg
20250102_143635[1].jpg

• Add LED and resistor according to circuit diagram .

• keep common all the negative terminal of all 5 LED's.

• Connect resistors with the positive terminal of this LED's.

• Now take arduino and connect pin no. 4,5,6,7 & 8 with second terminal of this resistors.

• Now connect the ground pin of arduino with the ground negative terminal of LED's.


• Now take ultrasonic sensor HCSR-04 and connect it's VCC to 5V pin of arduino and it's GND pin with arduino GND pin.

• finally connect the Trigger pin of ultrasonic sensor with pin no. 2 of arduino and echo pin of ultrasonic sensor with pin no. 3 of arduino.

Program

LED code.png

// Pin definitions

const int trigPin = 2; // Trigger pin for ultrasonic sensor

const int echoPin = 3; // Echo pin for ultrasonic sensor

const int ledPin = 5; // Pin for LED


// Variables to store distance and duration

long duration;

int distance;


void setup() {

// Initialize the pins

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

pinMode(ledPin, OUTPUT);

// Start serial communication for debugging

Serial.begin(9600);

}


void loop() {

// Send a 10µs pulse to trigger the ultrasonic sensor

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

// Read the pulse duration from the echo pin

duration = pulseIn(echoPin, HIGH);

// Calculate the distance in centimeters

distance = duration * 0.0344 / 3; // Speed of sound is 0.0344 cm/µs

// Print the distance to the Serial Monitor

Serial.print("Distance: ");

Serial.print(distance);

Serial.println("10 cm");

// Map the distance to an LED brightness (0 to 255)

int ledBrightness = map(distance, 0, 200, 255, 0); // Adjust 200 for desired max distance

ledBrightness = constrain(ledBrightness, 0, 255); // Ensure brightness is within valid range

// Set LED brightness

analogWrite(ledPin, ledBrightness);

// Wait for a short period before measuring again

delay(500);

}

Working

Working Steps:

1.Ultrasonic Sensor Operation:

  1. The HC-SR04 sensor uses ultrasonic waves to measure the distance to an object.
  2. It has two main parts:
  3. Trigger Pin: Sends a short ultrasonic pulse.
  4. Echo Pin: Receives the reflected ultrasonic wave.
  5. The time taken for the wave to return is used to calculate the distance: Distance (cm)=Time (microseconds)×0.034​​/2

2.Microcontroller (Arduino):

  1. The Arduino processes the time data from the ultrasonic sensor to compute the distance.
  2. The distance is then mapped to a number of LEDs to be lit.

3.LED Display:

  1. The LED array visually represents the distance:
  2. Closer Object: More LEDs light up.
  3. Farther Object: Fewer LEDs light up.


How It Works:

  1. The Arduino sends a trigger signal to the ultrasonic sensor, which emits an ultrasonic pulse.
  2. The sensor listens for the echo and calculates the time it takes to return.
  3. The Arduino converts this time into a distance using the formula.
  4. The distance is mapped to the LEDs:
  5. If the distance is close, more LEDs light up.
  6. If the distance is far, fewer LEDs light up.
  7. The map() function converts the distance range into the number of LEDs to light.


Flow of Execution


This code is a simple implementation of a distance measurement system using an ultrasonic sensor, an Arduino, and an LED that changes its brightness based on the measured distance. Here's a detailed breakdown of the code:

Code Explanation

Pin Definitions and Variables

cpp
Copy code
const int trigPin = 2; // Trigger pin for ultrasonic sensor
const int echoPin = 3; // Echo pin for ultrasonic sensor
const int ledPin = 5; // Pin for LED

long duration; // To store the time for the echo
int distance; // To store the calculated distance
  1. trigPin and echoPin: The ultrasonic sensor uses these pins to send and receive signals.
  2. ledPin: The LED is connected to this pin.
  3. duration: Stores the time it takes for the ultrasonic wave to return.
  4. distance: Stores the calculated distance in centimeters.

Setup Function

cpp
Copy code
void setup() {
pinMode(trigPin, OUTPUT); // Set trigger pin as output
pinMode(echoPin, INPUT); // Set echo pin as input
pinMode(ledPin, OUTPUT); // Set LED pin as output
Serial.begin(9600); // Initialize serial communication
}
  1. Pin Modes:
  2. The trigger pin sends a pulse, so it is set as OUTPUT.
  3. The echo pin receives the reflected pulse, so it is set as INPUT.
  4. The LED pin controls the brightness of the LED, so it is also set as OUTPUT.
  5. Serial Communication: Enables debugging by sending distance measurements to the Serial Monitor.

Loop Function

The main functionality of the code is contained in the loop() function.

1. Trigger the Ultrasonic Sensor
cpp
Copy code
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
  1. The trigger pin is pulsed HIGH for 10 microseconds to initiate an ultrasonic wave.
  2. Why 10 microseconds? This is the standard pulse width required to trigger the HC-SR04 sensor.
2. Measure Echo Duration
cpp
Copy code
duration = pulseIn(echoPin, HIGH);
  1. pulseIn: Measures the time (in microseconds) for the echo pin to go HIGH, indicating the return of the ultrasonic wave.
3. Calculate Distance
cpp
Copy code
distance = duration * 0.0344 / 3;
  1. The speed of sound is approximately 0.0344 cm/µs.
  2. The signal travels to the object and back, so the distance is divided by 2. (The code uses /3 here, possibly to adjust for testing or experimental factors.)
4. Debugging Output
cpp
Copy code
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
  1. Prints the measured distance to the Serial Monitor for debugging.
5. Map Distance to LED Brightness
cpp
Copy code
int ledBrightness = map(distance, 0, 200, 255, 0);
ledBrightness = constrain(ledBrightness, 0, 255);
  1. map(): Maps the distance to a brightness value (0 to 255).
  2. 0 cm (closest) → LED brightness = 255 (fully lit).
  3. 200 cm (farthest) → LED brightness = 0 (off).
  4. constrain(): Ensures the brightness is within the valid range of 0 to 255.
6. Set LED Brightness
cpp
Copy code
analogWrite(ledPin, ledBrightness);
  1. analogWrite(): Sets the LED brightness using Pulse Width Modulation (PWM).
7. Delay for Stability
cpp
Copy code
delay(500);
  1. Introduces a delay of 500 ms (half a second) before repeating the measurement process.

Flow of Execution

  1. The trigger pin sends a short pulse to the ultrasonic sensor.
  2. The echo pin measures the time it takes for the reflected sound wave to return.
  3. The Arduino calculates the distance using the duration of the echo.
  4. The distance is mapped to a brightness level for the LED.
  5. The LED brightness changes based on how far the object is from the sensor.
  6. The process repeats every 500 milliseconds.


Conclusion

Conclusion of an LED Distance Indicator

The LED distance indicator demonstrates a simple and effective way to measure distances and provide visual feedback using an ultrasonic sensor and LEDs. This project is highly versatile and can be applied in various fields.

Key Takeaways

  1. Accuracy and Simplicity:
  2. The ultrasonic sensor (e.g., HC-SR04) provides reliable and precise distance measurements in real-time.
  3. Mapping the distance to LED brightness or number of LEDs lit makes it easy to interpret the information.
  4. Visual Representation:
  5. The LED system provides a quick and intuitive way to understand proximity, even from a distance.
  6. Cost-Effectiveness:
  7. The system uses inexpensive components, making it suitable for low-cost applications.
  8. Applications:
  9. Parking Sensors: Helps drivers gauge the distance to obstacles.
  10. Obstacle Detection: Useful in robotics for navigation.
  11. Level Monitoring: Measures liquid or material levels in containers.
  12. Educational Tool: A great way to understand concepts of distance measurement, sensors, and microcontroller programming.
  13. Scalability:
  14. The system can be expanded to include more LEDs, an LCD screen, or even sound alerts for better feedback.

Learning Outcomes

  1. Practical Understanding:
  2. Gained insights into the working of ultrasonic sensors, microcontrollers, and LEDs.
  3. Learned how to integrate sensors with visual feedback systems.
  4. Programming Skills:
  5. Practiced working with Arduino IDE, pin configuration, and functions like pulseIn(), map(), and analogWrite().
  6. Hardware Integration:
  7. Understood the importance of proper connections, pin definitions, and power management.

Conclusion

The LED distance indicator is a simple yet powerful demonstration of how sensors can interact with microcontrollers to create functional and visually appealing systems. With additional enhancements, such as adding a buzzer or using an LCD screen, this project can evolve into more sophisticated applications. This makes it a valuable project for both learning and practical use.