Arduino Distance Indicator (arduino + Ultrasonic Sensor +LED )

Introduction: Arduino Distance Indicator (arduino + Ultrasonic Sensor +LED )

Hey electronics lovers!
In this tutorial i'm going to explain you how to interface arduino with ultrasonic sensor and LED's & how to indicate distance through LED.

For this project you need following components
1. Arduino - x1
2. Ultrasonic sensor HCSR-04 - x1
3. LED's - x5
4. Resistor (150ohm/ 220ohm/330ohm ) - x5
5. Some jumper cables

And for programming you need arduino IDE

Step 1: Connection

• Add LED and resistor according to circuit diagram .
• keep common all the negative terminal of all 5 LED's.
• Connect resistors with the positive terminal of this LED's.
• Now take arduino and connect pin no. 4,5,6,7 & 8 with second terminal of this resistors.
• Now connect the ground pin of arduino with the ground negative terminal of LED's.

• Now take ultrasonic sensor HCSR-04 and connect it's VCC to 5V pin of arduino and it's GND pin with arduino GND pin.
• finally connect the Trigger pin of ultrasonic sensor with pin no. 2 of arduino and echo pin of ultrasonic sensor with pin no. 3 of arduino.

Step 2: Program

This is the program , you can download it or copy it from here

------------------------------------------------------
int Time ;
int distance;

void setup()
{
pinMode(2,OUTPUT);
pinMode(3,INPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
Serial.begin(9600);


}

void loop()
{
digitalWrite(2,HIGH);
delayMicroseconds(10);
digitalWrite(2,LOW);
delayMicroseconds(5);
digitalWrite(2,HIGH);
delayMicroseconds(10);

Time = pulseIn(3,HIGH);

distance= 0.0343*Time/2;
Serial.print("distane from sensor to object is =");
Serial.println(distance);
delay(5);


if(distance>1 && distance <=10 )
{

digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
digitalWrite(6,HIGH);
digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
}

if(distance>10 && distance <=15)
{
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
digitalWrite(6,HIGH);
digitalWrite(7,HIGH);
digitalWrite(8,LOW);
}


if(distance>15 && distance <=20)
{

digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
digitalWrite(6,HIGH);
digitalWrite(7,LOW);
digitalWrite(8,LOW);

}

if(distance>20 && distance <=25)
{
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
digitalWrite(8,LOW);


}
if(distance>25 && distance <=30)
{
digitalWrite(4,HIGH);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
digitalWrite(8,LOW);
}

if(distance>30)
{
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
digitalWrite(8,LOW);

}
}

Program explanation :

Here first of all we calculate distance from the ultrasonic sensor and we store this values of distance in variable 'distance'.

After this we add if statements and inside this we write 'distance' variable .
So LED will blink according to different values of 'distance' variable .

Step 3: Testing

Be the First to Share

    Recommendations

    • Reclaimed Materials Contest

      Reclaimed Materials Contest
    • Halloween Contest

      Halloween Contest
    • Unusual Uses Contest

      Unusual Uses Contest

    Comments