The Best DIY STEM Tutorials and Projects

Arduino Distance Indicator +

OVERVIEW

In this project we will make a Distance Indicator using Arduino Uno, Ultrasonic sensor, and LEDs. we will detect ultrasonic waves to measure distance and turn on LEDs to show the distance between an object.

You can follow this video and instructions below to make this Amazing Arduino Distance Indicator Project.

Video

Components Required

Here is a list of Components that we will need to make this Project.

Sr #Item NameQuantity
1Arduino Uno1
2Breadboard1
3Male to Male Jumper Wires12
4LEDs7
5Resistors 220k ohms7
6Ultrasonic Sensor1

Wiring Diagram

Here is a Complete Wiring Diagram along with Instructions for this Project

Program / Code

Code is Explained in the Comments of the Code

//  www.theacelabs.com
//  Distance Indicator ____ AceLabs ____

const int trig = 12; // set ultrasonic sensor trigger pin to 12
const int echo = 13; // set ultrasonic sensor echo pin to 13

// set LED pins
const int LED1 = 8;
const int LED2 = 7;
const int LED3 = 6;
const int LED4 = 5;
const int LED5 = 4;
const int LED6 = 3;
const int LED7 = 2;


int duration = 0;
int distance = 0;

// set all as input or output
void setup() 
{ 
  pinMode(trig , OUTPUT);
  pinMode(echo , INPUT);
  
  pinMode(LED1 , OUTPUT);
  pinMode(LED2 , OUTPUT);
  pinMode(LED3 , OUTPUT);
  pinMode(LED4 , OUTPUT);
  pinMode(LED5 , OUTPUT);
  pinMode(LED6 , OUTPUT);
  pinMode(LED7 , OUTPUT);
  
  Serial.begin(9600);

}

// main program to set leds to turn on/off measuring distance
void loop()
{
  digitalWrite(trig , HIGH);
  delayMicroseconds(1000);
  digitalWrite(trig , LOW);


  duration = pulseIn(echo , HIGH);
  distance = (duration/2) / 28.5 ;
  Serial.println(distance);
  

  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);
  }
  if ( distance <= 42 )
  {
    digitalWrite(LED6, HIGH);
  }
  else
  {
    digitalWrite(LED6, LOW);
  }
  if ( distance <= 49 )
  {
    digitalWrite(LED7, HIGH);
  }
  else
  {
    digitalWrite(LED7, LOW);
  }
}

Thank you so much for going through our tutorial, we hope it was easy to follow and you enjoyed it, please share your feedback and pictures of the project (if you have made it) in the comments below .!

Share your love

Leave a Reply

Your email address will not be published. Required fields are marked *