Mayank SenapatiKalyan Prusty
Published © GPL3+

Distance Indicator Using Ultrasonic Sensor

This project lights a set of LED's depending upon the distance of the obstacle with respect to the ultrasonic sensor.

BeginnerWork in progress1.5 hours4,296
Distance Indicator Using Ultrasonic Sensor

Things used in this project

Hardware components

Arduino UNO & Genuino UNO
Arduino UNO & Genuino UNO
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
LED (generic)
LED (generic)
×5
Jumper wires (generic)
Jumper wires (generic)
×10
Breadboard (generic)
Breadboard (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE
Proteus 8 Professional

Story

Read more

Schematics

Schematic

Code

ULTRA.ino

C/C++
const int trigPin = 9;
const int echoPin = 10;

long duration;
int dCm, dInch;

void setup() {

Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
for(int i = 1 ; i<=6;i++)
{ 
   pinMode(i,OUTPUT);
}
}

void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);
dCm= duration*0.034/2;
dInch = duration*0.0133/2;

if(dCm<2){
  digitalWrite(6,LOW);
   digitalWrite(2,LOW);
   digitalWrite(3,LOW);
   digitalWrite(4,LOW);
   digitalWrite(5,LOW);
}
else if(dCm>2&&dCm < 5 ){
  digitalWrite(6,HIGH);
   digitalWrite(2,LOW);
   digitalWrite(3,LOW);
   digitalWrite(4,LOW);
   digitalWrite(5,LOW);  
}
else if(dCm > 5 && dCm <10){
   digitalWrite(6,HIGH);
   digitalWrite(2,HIGH);
   digitalWrite(3,LOW);
   digitalWrite(4,LOW);
   digitalWrite(5,LOW);   
}
else if(dCm > 10 && dCm <15){
   digitalWrite(6,HIGH);
   digitalWrite(2,HIGH);
   digitalWrite(3,HIGH);
   digitalWrite(4,LOW);
   digitalWrite(5,LOW);   
}
else if(dCm > 15 && dCm <20){
   digitalWrite(6,HIGH);
   digitalWrite(2,HIGH);
   digitalWrite(3,HIGH);
   digitalWrite(4,HIGH);
   digitalWrite(5,LOW);   
}else if(dCm>20){
  digitalWrite(6,HIGH);
   digitalWrite(2,HIGH);
   digitalWrite(3,HIGH);
   digitalWrite(4,HIGH);
   digitalWrite(5,HIGH);
}

Serial.print("Distance: "); // Prints string "Distance" 
Serial.print(dCm); // Prints the distance value from the sensor
Serial.println(" cm");
delay(10);
Serial.print("Distance: ");
Serial.print(dInch);
Serial.println(" inch");
delay(10);
}

Credits

Mayank Senapati

Mayank Senapati

1 project • 1 follower
Kalyan Prusty

Kalyan Prusty

1 project • 12 followers

Comments