blackpanda856
Published © GPL3+

Controlling LED's Using Ultrasonic Distance Sensor

Turns the 2 LED's on and off by calculating the distance of the object from the Ultrasonic Sensor.

IntermediateFull instructions provided23,223
Controlling LED's Using Ultrasonic Distance Sensor

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Breadboard (generic)
Breadboard (generic)
×1
5 mm LED: Red
5 mm LED: Red
×1
5 mm LED: Green
5 mm LED: Green
×1
Resistor 1k ohm
Resistor 1k ohm
×2
Male/Female Jumper Wires
Male/Female Jumper Wires
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Wire Stripper & Cutter, 18-10 AWG / 0.75-4mm² Capacity Wires
Wire Stripper & Cutter, 18-10 AWG / 0.75-4mm² Capacity Wires

Story

Read more

Schematics

Circuit Diagram

This is the Circuit Diagram of this project. Assemble your project by following this circuit diagram.

Code

Code

C/C++
This is the code of this project. Copy paste this code to your Arduino IDE.
#define Trigpin 7
#define Echopin 8
#define low_led 9
#define high_led 10
float distance;
int duration;
int ll = 700;

void setup() {
  pinMode (Trigpin, OUTPUT);
  pinMode (low_led, OUTPUT);
  pinMode (high_led, OUTPUT);
  pinMode (Echopin, INPUT);
  Serial.begin(9600);
  Serial.println ("Welcome To Distance Meter");
  Serial.println ("Coded By Jevins Annson");
  digitalWrite (low_led, LOW);
  digitalWrite (high_led, LOW);
}

void loop() {
  digitalWrite(Trigpin, LOW);
  delayMicroseconds(2);
  digitalWrite(Trigpin, HIGH);
  delayMicroseconds(10);
  digitalWrite(Trigpin, LOW);
  duration = pulseIn(Echopin, HIGH);
  distance = duration * 0.034 / 2;
  delay (ll);
  Serial.println (" ");
  Serial.print ("Distance = ");
  Serial.print (distance);
  Serial.print (" CM");
  Serial.println (" ");

  if (distance>=30)
  {
    Serial.println ("Nobody Is Infront Of the Sensor");
    digitalWrite (low_led, HIGH);
    delay (500);
    digitalWrite (low_led, LOW);
    delay (500);
    digitalWrite (low_led, HIGH);
  }
  else 
  {
    Serial.println ("Someone Is Infront Of the Sensor");
    digitalWrite (high_led, HIGH);
    delay (100);
    digitalWrite (high_led, LOW);
    delay (100);
    digitalWrite (high_led, HIGH);
    delay (100);
  }
}

Credits

blackpanda856

blackpanda856

2 projects • 4 followers

Comments