
Building a Distance-Measuring LED Indicator Using Arduino and Ultrasonic Sensor 🔥
Arduino Project: Ultrasonic Sensor with LED Indicators
Welcome to this in-depth guide on building a simple yet effective Arduino project that uses an ultrasonic sensor to measure distance and control a series of LEDs. This project is ideal for beginners looking to expand their knowledge of Arduino, sensors, and basic electronics.
I had made this video in january 2022
Project Overview
In this project, we will utilize the HC-SR04 ultrasonic sensor to measure distances and light up LEDs based on the proximity of an object. This can be the foundation for a variety of applications, such as proximity alarms, automated lighting systems, or even robotics. The LEDs will provide visual feedback, lighting up in different patterns depending on how close an object is to the sensor.
Components Required
- Arduino board (e.g., Arduino Uno)
- HC-SR04 Ultrasonic Sensor
- Three LEDs (any color)
- Three 220Ω Resistors
- Jumper wires
- Breadboard
Understanding the Hardware Setup
The hardware setup is straightforward. The ultrasonic sensor has four pins:
- VCC: Connects to 5V on the Arduino.
- GND: Connects to the ground (GND) on the Arduino.
- Trig: Connects to pin 8 on the Arduino.
- Echo: Connects to pin 11 on the Arduino.
Each LED is connected to a digital pin on the Arduino (pins 3, 5, and 6) through a 220Ω resistor to limit the current and prevent damage to the LEDs.
Explaining the Code
Let’s dive into the code that brings this project to life. The code is structured in two main sections: the setup() function and the loop() function. The setup() function runs once when the Arduino is powered on or reset, and it initializes the pins. The loop() function contains the main logic and runs repeatedly.
Setup Function
In the setup() function, we start by initializing the serial communication at a baud rate of 9600, which allows us to see the distance readings on the serial monitor. We then define the pin modes for the ultrasonic sensor and the LEDs.
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
}
Loop Function
The loop() function contains the core logic of the project. The ultrasonic sensor works by sending out a sound pulse through the trigger pin and measuring the time it takes for the echo to return. This time is then used to calculate the distance to an object.
void loop() {
long duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(10);
Based on the calculated distance, different LEDs are lit up or made to blink. Here’s how the LEDs behave for various distance ranges:
- Distance < 5 cm: All LEDs are off, indicating that the object is very close.
- 5 cm < Distance < 10 cm: The first LED lights up.
- 10 cm < Distance < 15 cm: The second LED lights up.
- 15 cm < Distance < 20 cm: The third LED lights up.
- 20 cm < Distance < 30 cm: All three LEDs light up simultaneously.
- Distance > 30 cm: The LEDs blink in sequence, creating a visual effect that indicates the object is far away.
Here’s the code that handles this logic:
// Code for LED blinking based on distance
if (distance < 5) {
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
} else if (distance > 5 && distance < 10) {
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
} else if (distance > 10 && distance < 15) {
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
digitalWrite(led3, LOW);
} else if (distance > 15 && distance < 20) {
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, HIGH);
} else if (distance > 20 && distance < 30) {
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
} else if (distance > 30) {
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
delay(50);
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
digitalWrite(led3, LOW);
delay(50);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, HIGH);
delay(50);
}
Full Code for the Project
Below is the complete code for this project. You can copy and paste this directly into your Arduino IDE:
int trigPin = 8;
int echoPin = 11;
int led1 = 3;
int led2 = 5;
int led3 = 6;
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
}
void loop() {
long duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(10);
if (distance < 5) {
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
} else if (distance > 5 && distance < 10) {
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
} else if (distance > 10 && distance < 15) {
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
digitalWrite(led3, LOW);
} else if (distance > 15 && distance < 20) {
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, HIGH);
} else if (distance > 20 && distance < 30) {
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
} else if (distance > 30) {
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
delay(50);
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
digitalWrite(led3, LOW);
delay(50);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, HIGH);
delay(50);
}
}
Conclusion and Next Steps
This project is a great way to get hands-on experience with Arduino and basic electronics. You’ve learned how to use an ultrasonic sensor to measure distances and control LEDs based on the proximity of objects. As a next step, consider expanding this project by adding more LEDs, integrating a buzzer for auditory feedback, or displaying the distance on an LCD screen.
Further Reading and Resources
If you’re interested in diving deeper into Arduino and electronics, here are some book recommendations to help you along your journey:
- “Getting Started with Arduino” by Massimo Banzi - A perfect introduction to Arduino from one of its co-founders.
- “Arduino Cookbook” by Michael Margolis - A comprehensive guide with practical projects and in-depth explanations.
- “Programming Arduino: Getting Started with Sketches” by Simon Monk - Focuses on Arduino programming for beginners.
- “Exploring Arduino: Tools and Techniques for Engineering Wizardry” by Jeremy Blum - A book for those who want to explore more advanced Arduino projects and techniques.
With these resources and the knowledge you’ve gained from this project, you’re well on your way to becoming an Arduino expert. Happy tinkering!
