Smart Ultrasonic Distance Finder Using 3 Ultrasonic Sensors

Hey geeks, welcome back to Techatronic. Are you searching for a distance finder project using an ultrasonic sensor? if so then this article is for you. In this, we are going to learn how you can make your own smart ultrasonic distance finder using three ultrasonic sensors. For making this project we are using three HC-SR04 ultrasonic sensors and an Arduino UNO development board. Do you guys know how an ultrasonic sensor works with the Arduino? Well, we explain it in our previous articles. We are displaying the real-time values of the distance captured by the sensors on a 16×2 LCD. This project works similarly to the Ultrasonic Range Finder Using Arduino and LCD. If you like this project then also view more such cool projects on Arduino.¬†Please make the circuit properly and then upload the given code.

Working of the Project

The project has three HC-SR04 ultrasonic sensors which can measure the distance in three different directions. If one of the sensors detects that the distance from the obstacle is less than the given value then the corresponding LED will glow. There are three LEDs, one for each sensor. The measured distance is displayed on a 16×2 LCD in centimeters. The ultrasonic sensors work on the principle of SONAR technology in which ultrasonic rays are transmitted and received to calculate the distance between the body and the obstacle. You can also view the values of the distance on the serial monitor screen. Open the serial monitor as shown below.¬† Also, check Smart Blind Stick made by us.

ultrasonic distance finder

Components Required

  • Arduino UNO
  • Three HC-SR04 ultrasonic sensors
  • Jumper wires
  • Breadboard
  • Three LEDs
  • USB cable for uploading the code
  • Resistors 220-ohm
  • 10K potentiometer
  • 16×2 LCD display

Circuit Diagram for the Project

ultrasonic distance finder

Connect the VCC and GND pins of all three sensors with the 5-volts and GND pins of the Arduino. Attach the TRIG and ECHO pins of the third ultrasonic sensor with the digital-5 and digital-6 pins of the Arduino. Join the TRIG and ECHO pins of the second ultrasonic sensor to the digital-7 and digital-8 pins of the Arduino. Connect the TRIG and ECHO pins of the first ultrasonic sensor with the digital-9 and digital-10 pins of the Arduino. Join the positive legs of all the three LEDs with the digital-3, digital-11, and digital-12 pins of the Arduino and the negative legs with the GND pin of Arduino through a 220-ohm resistor. Connect the LCD pins with the Arduino as shown above or take help from our article on Interfacing of a 16×2 LCD with Arduino.

Code for the Project

NOTE: Please upload the code given below to the Arduino. You have to install <LiquidCrystal.h> library first. Check how to install a zip library to the Arduino.

 #include "LiquidCrystal.h"  
 LiquidCrystal lcd(A0, A1, A2, A3, A4, A5);  
 const int trigPin1 = 9;  
 const int echoPin1 = 10;  
 const int trigPin2 = 5;  
 const int echoPin2 = 6;  
 const int trigPin3 = 7;  
 const int echoPin3 = 8;  
 long duration1;  
 long duration2;  
 long duration3;  
 int distanceCm1, distanceInch1;  
 int distanceCm2, distanceInch2;  
 int distanceCm3, distanceInch3;  
 void setup() {  
 Serial.begin(9600);   
  lcd.begin(16,2);  
 pinMode(trigPin1, OUTPUT);  
 pinMode(echoPin1, INPUT);  
 pinMode(trigPin2, OUTPUT);  
 pinMode(echoPin2, INPUT);  
 pinMode(trigPin3, OUTPUT);  
 pinMode(echoPin3, INPUT);  
 pinMode(12,OUTPUT);  
 pinMode(11,OUTPUT);  
 pinMode(3,OUTPUT);  
 }  
 void loop()  
 {  
 digitalWrite(trigPin1, LOW);  
 delayMicroseconds(2);  
 digitalWrite(trigPin1, HIGH);  
 delayMicroseconds(10);  
 digitalWrite(trigPin1, LOW);  
 duration1 = pulseIn(echoPin1, HIGH);  
 digitalWrite(trigPin2, LOW);  
 delayMicroseconds(2);  
 digitalWrite(trigPin2, HIGH);  
 delayMicroseconds(10);  
 digitalWrite(trigPin2, LOW);  
 duration2 = pulseIn(echoPin2, HIGH);  
 digitalWrite(trigPin3, LOW);  
 delayMicroseconds(2);  
 digitalWrite(trigPin3, HIGH);  
 delayMicroseconds(10);  
 digitalWrite(trigPin3, LOW);  
 duration3 = pulseIn(echoPin3, HIGH);  
 distanceCm1= duration1*0.034/2;  
 distanceInch1 = duration1*0.0133/2;  
 distanceCm2= duration2*0.034/2;  
 distanceInch2 = duration2*0.0133/2;  
 distanceCm3= duration3*0.034/2;  
 distanceInch3 = duration3*0.0133/2;  
 Serial.print("Distance1: ");  
 Serial.println(distanceCm1);  
 lcd.clear();  
  lcd.setCursor(0,0);  
  lcd.print("Ultrasonic1 ");  
  lcd.setCursor(12,0);  
  lcd.print(distanceCm1);  
 //delay(500);  
 Serial.print("Distance2: ");  
 Serial.println(distanceCm2);  
  lcd.setCursor(0,1);  
  lcd.print("U2 ");  
  lcd.setCursor(3,1);  
  lcd.print(distanceCm2);  
 Serial.print("Distance3: ");  
 Serial.println(distanceCm3);  
  lcd.setCursor(9,1);  
  lcd.print("U3 ");  
  lcd.setCursor(12,1);  
  lcd.print(distanceCm3);  
 if(distanceCm1 <15)  
 {  
   digitalWrite(3,HIGH); // LED ON  
 }  
 if(distanceCm1 >15)  
 {  
   digitalWrite(3,LOW);  // LED OFF  
 }  
 if(distanceCm2 <15)  
 {  
   digitalWrite(12,HIGH); // LED ON  
 }  
 if(distanceCm2 >15)  
 {  
   digitalWrite(12,LOW); // LED OFF  
 }  
 if(distanceCm3 <15)  
 {  
   digitalWrite(11,HIGH); // LED ON  
 }  
 if(distanceCm3 >15)   
 {  
   digitalWrite(11,LOW); // LED OFF  
 }  
 delay(1000);  
 }  

We hope that you understand the project completely and please try to make it now on your own. Use the comments section below to inform us about the errors you encountered if any. Also, check out more tutorials on Arduino and Raspberry Pi made by us.

HAPPY LEARNING!