The core idea is to use three HC-SR04 Ultrasonic Sensors to get a broader view of the space in front of the device. By having sensors facing left, center, and right, the device can provide more contextual information about obstacles than a single sensor could. This is similar to how modern cars have multiple parking sensors. The readings from all three sensors are shown simultaneously on a 16x2 LCD display.
Components RequiredAccording to the article, you will need:
Arduino Uno (or compatible board like Nano, Mega)
- Arduino Uno (or compatible board like Nano, Mega)
HC-SR04 Ultrasonic Sensors (x3)
- HC-SR04 Ultrasonic Sensors (x3)
16x2 LCD Display (with I2C interface module)
- 16x2 LCD Display (with I2C interface module)
Breadboard
- Breadboard
Jumper Wires (Male-to-Male and Male-to-Female)
- Jumper Wires (Male-to-Male and Male-to-Female)
9V Battery (with DC barrel jack connector) for portability
- 9V Battery (with DC barrel jack connector) for portability
The connections can be broken down into two parts:
1. I2C LCD Connections (This simplifies wiring drastically)The I2C module has only 4 pins:
GND → Arduino GND
- GND → Arduino GND
VCC → Arduino 5V
- VCC → Arduino 5V
SDA → Arduino Analog Pin A4
- SDA → Arduino Analog Pin A4
SCL → Arduino Analog Pin A5
- SCL → Arduino Analog Pin A5
Each sensor has 4 pins: VCC, Trig, Echo, GND.
Left Sensor:
VCC → 5V
- VCC → 5V
GND → GND
- GND → GND
Trig → Digital Pin 2
- Trig → Digital Pin 2
Echo → Digital Pin 3
- Echo → Digital Pin 3
- Left Sensor:VCC → 5VGND → GNDTrig → Digital Pin 2Echo → Digital Pin 3
Center Sensor:
VCC → 5V
- VCC → 5V
GND → GND
- GND → GND
Trig → Digital Pin 4
- Trig → Digital Pin 4
Echo → Digital Pin 5
- Echo → Digital Pin 5
- Center Sensor:VCC → 5VGND → GNDTrig → Digital Pin 4Echo → Digital Pin 5
Right Sensor:
VCC → 5V
- VCC → 5V
GND → GND
- GND → GND
Trig → Digital Pin 6
- Trig → Digital Pin 6
Echo → Digital Pin 7
- Echo → Digital Pin 7
- Right Sensor:VCC → 5VGND → GNDTrig → Digital Pin 6Echo → Digital Pin 7
Important Note: The project page shows a standard LCD connected with many wires, but it highly recommends using an I2C LCD module to save pins and reduce wiring complexity, which is the setup described above.
How the Code Works (The Core Logic)The code works by sequentially triggering each sensor, reading its echo pulse, calculating the distance, and then printing all three results to the LCD.
Libraries: The code includes the Wire.h library for I2C communication and the LiquidCrystal_I2C.h library to control the LCD.
- Libraries: The code includes the
Wire.hlibrary for I2C communication and theLiquidCrystal_I2C.hlibrary to control the LCD.
Sensor Pins: It defines the Trigger and Echo pins for all three sensors.
- Sensor Pins: It defines the Trigger and Echo pins for all three sensors.
setup() Function:
Initializes serial communication (for debugging).
- Initializes serial communication (for debugging).
Initializes the LCD and turns on its backlight.
- Initializes the LCD and turns on its backlight.
Sets the Trig pins as OUTPUT and the Echo pins as INPUT.
- Sets the Trig pins as
OUTPUTand the Echo pins asINPUT. setup()Function:Initializes serial communication (for debugging).Initializes the LCD and turns on its backlight.Sets the Trig pins asOUTPUTand the Echo pins asINPUT.
loop() Function:
Reads each sensor: It calls a custom function (e.g., calculateDistance()) for each sensor, passing its Trig and Echo pins as arguments.
- Reads each sensor: It calls a custom function (e.g.,
calculateDistance()) for each sensor, passing its Trig and Echo pins as arguments.
Calculates Distance: The calculateDistance() function works as follows:
Ensures the Trig pin is low for a short period.
- Ensures the Trig pin is low for a short period.
Sends a high-frequency sound pulse by setting the Trig pin high for 10 microseconds.
- Sends a high-frequency sound pulse by setting the Trig pin high for 10 microseconds.
Uses the pulseIn() function to measure the duration the Echo pin stays high. This duration is the time taken for the sound wave to travel to the object and back.
- Uses the
pulseIn()function to measure the duration the Echo pin stays high. This duration is the time taken for the sound wave to travel to the object and back.
Calculates the distance using the formula: Distance = (Duration * Speed of Sound) / 2. The speed of sound is taken as 0.034 cm/µs. Dividing by 2 is done because the time is for the round trip.
- Calculates the distance using the formula: Distance = (Duration * Speed of Sound) / 2. The speed of sound is taken as 0.034 cm/µs. Dividing by 2 is done because the time is for the round trip.
- Calculates Distance: The
calculateDistance()function works as follows:Ensures the Trig pin is low for a short period.Sends a high-frequency sound pulse by setting the Trig pin high for 10 microseconds.Uses thepulseIn()function to measure the duration the Echo pin stays high. This duration is the time taken for the sound wave to travel to the object and back.Calculates the distance using the formula: Distance = (Duration * Speed of Sound) / 2. The speed of sound is taken as 0.034 cm/µs. Dividing by 2 is done because the time is for the round trip.
Displays on LCD: The code then clears the LCD and prints the three distances on two lines, for example:L:23cm C:15cmR:45cm
- Displays on LCD: The code then clears the LCD and prints the three distances on two lines, for example:
L:23cm C:15cmR:45cm loop()Function:Reads each sensor: It calls a custom function (e.g.,calculateDistance()) for each sensor, passing its Trig and Echo pins as arguments.Calculates Distance: ThecalculateDistance()function works as follows:Ensures the Trig pin is low for a short period.Sends a high-frequency sound pulse by setting the Trig pin high for 10 microseconds.Uses thepulseIn()function to measure the duration the Echo pin stays high. This duration is the time taken for the sound wave to travel to the object and back.Calculates the distance using the formula: Distance = (Duration * Speed of Sound) / 2. The speed of sound is taken as 0.034 cm/µs. Dividing by 2 is done because the time is for the round trip.Displays on LCD: The code then clears the LCD and prints the three distances on two lines, for example:L:23cm C:15cmR:45cm
Here is a simplified version of the code logic used on the website.
cpp
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Initialize the LCD (0x27 is the common I2C address, 16 columns, 2 rows)
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Define sensor pins
#define leftTrig 2
#define leftEcho 3
#define centerTrig 4
#define centerEcho 5
#define rightTrig 6
#define rightEcho 7
long duration;
int distance;
void setup() {
lcd.init();
lcd.backlight();
lcd.print("Smart Distance");
lcd.setCursor(0, 1);
lcd.print(" Finder Ready ");
delay(2000);
pinMode(leftTrig, OUTPUT);
pinMode(leftEcho, INPUT);
// ... do the same for center and right sensor pins
}
void loop() {
int leftDistance = calculateDistance(leftTrig, leftEcho);
int centerDistance = calculateDistance(centerTrig, centerEcho);
int rightDistance = calculateDistance(rightTrig, rightEcho);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("L:");
lcd.print(leftDistance);
lcd.print(" C:");
lcd.print(centerDistance);
lcd.setCursor(0, 1);
lcd.print("R:");
lcd.print(rightDistance);
lcd.print(" cm");
delay(500); // Short delay before next reading
}
// Custom function to calculate distance for a given sensor
int calculateDistance(int trigPin, int echoPin) {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
return distance;
}Advantages and ApplicationsAdvantages
Applications
Wider Field of View
Simple Parking Assistance System for vehicles or robots.
Real-time Data Display
Obstacle Detection Robot that can decide which way to turn.
Portable and Standalone (with battery)
Smart Cane for the visually impaired to detect side obstacles.
Excellent for Learning sensor multiplexing.
Proximity Awareness System for industrial machinery or exhibits.
Potential Challenges and ImprovementsPower Supply: Three sensors and an LCD can draw a significant amount of current. A 9V battery may drain quickly. Using a larger battery pack (like 4xAA) or a LiPo battery is a good improvement.
- Power Supply: Three sensors and an LCD can draw a significant amount of current. A 9V battery may drain quickly. Using a larger battery pack (like 4xAA) or a LiPo battery is a good improvement.
Sensor Interference (Crosstalk): If all sensors are triggered at the exact same time, the sound wave from one sensor can be detected by another's receiver, causing false readings. The sequential triggering in the code mitigates this, but adding a small delay between each sensor reading can help further.
- Sensor Interference (Crosstalk): If all sensors are triggered at the exact same time, the sound wave from one sensor can be detected by another's receiver, causing false readings. The sequential triggering in the code mitigates this, but adding a small delay between each sensor reading can help further.
Adding Alarms: You could add a buzzer that beeps faster as any of the three sensors detects an object getting closer.
- Adding Alarms: You could add a buzzer that beeps faster as any of the three sensors detects an object getting closer.
Data Logging: Connect the Arduino to an SD card module to log the distance data over time for analysis.
- Data Logging: Connect the Arduino to an SD card module to log the distance data over time for analysis.
The Smart Ultrasonic Distance Finder is an excellent intermediate Arduino project. It effectively teaches how to manage multiple sensors of the same type, use I2C components to simplify wiring, and display data cleanly on an LCD. It has direct, practical applications and serves as a great foundation for more complex robotics and automation projects.


_ztBMuBhMHo.jpg?auto=compress%2Cformat&w=48&h=48&fit=fill&bg=ffffff)

















Comments