Visualizing the Invisible: The "Distance Magic" LED Meter
Sonic waves are constantly bouncing around us, invisible to the human eye. This "Distance Magic" project brings these waves into the visual spectrum by creating a multi-stage LED meter that reacts to physical proximity. By turning distance into a visual signal, this device serves as a foundational step for robotics, automated parking assistants, or interactive art installations.
Precision Ultrasonic Echo-Location
The heart of the project is the HC-SR04 Ultrasonic Sensor, which utilizes the same principles as biological sonar used by bats and dolphins:
- Sonic Pulse: The sensor's Trigger pin emits a high-frequency (40kHz) sonic burst.
- Echo Return: The sonic wave bounces off an object and returns to the Echo pin.
- Time-of-Flight Calculation: The Arduino measures the duration (in microseconds) between the trigger and the echo. Using the speed of sound (approx. 343 m/s), the distance is calculated with the formula:
Distance = Time / 2 / 28.5.
Linear Metering and Conditional Logic
The project features a 5-stage LED Meter that provides a granular visual representation of the measured distance. The logic is programmed in increments of 7 centimeters:
- LED 1 (0-7cm): Object is very close.
- LED 2 (8-14cm): Object is moving closer.
- LED 3 (15-21cm): Mid-range detection.
- LED 4 (22-28cm): Warning threshold.
- LED 5 (29-35cm): Object detected at maximum monitored range.
Each LED is protected by a 220-ohm resistor and is controlled via specific digital pins (4-8) on the Arduino Uno.
Interactive Real-Time Debugging
Beyond the visual LEDs, the project utilizes the Serial Monitor to provide precise numerical data. By streaming the exact distance in centimeters back to the PC, makers can calibrate the sensor effectively or use the data to trigger more complex events, such as stopping a robot or activating a remote IoT notification via an ESP32 or ESP8266 upgrade.
const int echo = 13;
const int trig = 12;
const int LED1 =8;
const int LED2 =7;
const int LED3 =6;
const int LED4 =5;
const int LED5 =4;
int duration = 0;
int distance = 0;
void setup()
{
pinMode(trig,OUTPUT);
pinMode(echo,INPUT);
pinMode(LED1,OUTPUT);
pinMode(LED2,OUTPUT);
pinMode(LED3,OUTPUT);
pinMode(LED4,OUTPUT);
pinMode(LED5,OUTPUT);
Serial.begin(9600);
}
void loop()
{
digitalWrite(trig,HIGH);
delayMicroseconds(1000);
digitalWrite(trig,LOW);
duration = pulseIn(echo,HIGH);
distance = (duration/2)/28.5;
Serial.begin(distance);
if(distance <=7)
{
digitalWrite(LED1,HIGH);
}
else
{
digitalWrite(LED1,LOW);
}
if(distance <=7)
{
digitalWrite(LED1,HIGH);
}
else
{
digitalWrite(LED1,LOW);
}
if(distance <=14)
{
digitalWrite(LED2,HIGH);
}
else
{
digitalWrite(LED2,LOW);
}
if(distance <=21)
{
digitalWrite(LED3,HIGH);
}
else
{
digitalWrite(LED3,LOW);
}
if(distance <=28)
{
digitalWrite(LED4,HIGH);
}
else
{
digitalWrite(LED4,LOW);
}
if(distance <=35)
{
digitalWrite(LED5,HIGH);
}
else
{
digitalWrite(LED5,LOW);
}
}