DIY Distance Detector

Using simple parts like an Arduino, an ultrasonic sensor, a buzzer, and LEDs, one may construct a distance sensor (distance detector). This project’s main objective is to use buzzers and LEDs to determine how distant an object is from a sensor.

Components Required

Components Required

  • Arduino uno.
  • Breadboard
  • Stepper wires (male to male, male to female)
  • Substantial amount of green, yellow and red leds(light emitting diodes)
  • Buzzer(one can use both 0-5V buzzer or 3-27V buzzer)
  • HC-SR04 Ultrasonic sensor
  • 330ohm resistors( at least 7)
  • 9V battery

Setup Design

Setup Design

The project’s setup is shown in the picture up top. It is recommended to connect the jumper wires as follows:

  • Jumper wire from the Arduino’s 5 volt pin to the breadboard’s bottom channel.
  • Another jumper wire should be connected from the Arduino’s ground pin to the breadboard’s upper channel.
  • Buzzer -> pin 3

(On Ultrasonic Sensor)

  • Echo -> pin 6
  • Trig -> pin 7

(In Order from Right to Left)

  • LED1 -> pin 8
  • LED2 -> pin 9
  • LED3 -> pin 10
  • LED4 -> pin 11
  • LED5 -> pin 12
  • LED6 -> pin 13

The right lead of the LED should have jumper wires attached to it, and the left lead should have a 330 ohm resistor connected to the ground channel.

Assembly-Breadboard

Assembly-Breadboard
  • The Arduino’s ground and 5V pins must first be connected to the breadboard.
  • Connect the wire coming from the Arduino’s 5V pin to the bottom channel on the board’s left half.
  • Connect the wire coming from the Arduino’s ground pin to the upper channel on the board’s left half.

Ultrasonic Sensor

Ultrasonic Sensor
Ultrasonic Sensor

Connect the HC-SRO4 to the breadboard’s upper right half. While making the connections, refer to the setup schematic shown in the earlier diagram.

The ground pin on the ultrasonic sensor should be connected to the ground channel on the breadboard according to the preceding setup diagram. Next, attach pin 6 of the Arduino to the sensor’s Echo pin. Finally, connect the sensor’s VCC pin to the 5 volt channel on the breadboard after connecting the sensor’s Trig pin to pin 7 on the Arduino.

LED Connections

LED Connections

The next step is to connect the breadboard and Arduino to the Leds. The anode, or the longer leg, or the one on the right, is connected to a pin on the Arduino using a jumper wire, and the cathode, or the shorter leg, or the one on the left, is connected using a 330 ohm resistor to the ground channel on the breadboard. Simply repeat that step for each of the six LEDs, connecting the anode of the red LED farthest to the right to pin 8 on the Arduino, the anode of the red LED next to it to pin 9, and so forth. The anode, or right leg, of the last LED, which is the green LED farthest to the left, needs to be connected to pin 13 on the Arduino.

Connect the Buzzer and the 9V Battery

Connect the Buzzer and the 9V Battery
Connect the Buzzer and the 9V Battery
  • Connect the 9V battery and the buzzer.
  • The buzzer needs to be connected to the breadboard and Arduino at this point.  The longer buzzer leg must be connected to Arduino pin 3 and the shorter buzzer leg must be connected to the breadboard’s ground channel.

Code

#define trigPin 7
#define echoPin 6
#define led 13
#define led2 12
#define led3 11
#define led4 10
#define led5 9
#define led6 8
#define buzzer 3
int sound = 250;
void s etup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);
  pinMode(buzzer, OUTPUT);
 
}
void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;

 if (distance <= 30) {
    digitalWrite(led, HIGH);
    sound = 250;
}
  else {
    digitalWrite(led,LOW);
  }
  if (distance < 25) {
      digitalWrite(led2, HIGH);
      sound = 260;
}
  else {
      digitalWrite(led2, LOW);
  }
  if (distance < 20) {
      digitalWrite(led3, HIGH);
      sound = 270;

  else {
    digitalWrite(led3, LOW);
  }
  if (distance < 15) {
    digitalWrite(led4, HIGH);
    sound = 280;
}
  else {
    digitalWrite(led4,LOW);
  }
  if (distance < 10) {
    digitalWrite(led5, HIGH);
    sound = 290;
}
  else {
    digitalWrite(led5,LOW);
  }
  if (distance < 5) {
    digitalWrite(led6, HIGH);
    sound = 300;
}
  else {
    digitalWrite(led6,LOW);
  }
 
  if (distance > 30 || distance <= 0){
    Serial.println(“Out of range”);
    noTone(buzzer);
  }
  else {
    Serial.print(distance);
    Serial.println(” cm”);
    tone(buzzer, sound);
   
  }
  delay(500);
}

Result

Now, upload the software to the Arduino and run it. If all the connections are made correctly, the leds will begin to illuminate and the buzzer will sound as soon as we place our fingers very close to the ultrasonic sensor.

Applications

For the Car Rear Parking System, this model can serve as a kind of reference.

Hope you find this project simple. Superkitz will be back soon with more informative blogs. Thank You Bye.

Leave a Reply