Distance Detector With LEDs and Buzzer

by 760800 in Circuits > Arduino

38 Views, 0 Favorites, 0 Comments

Distance Detector With LEDs and Buzzer

IMG_2550_(1).jpg

A smart circuit with LEDs, a distance sensor, and a buzzer. The circuit activates when you are 30 cm away from it, the buzzers makes a quiet but clear noise and the green LED turn on, when you get 25 cm the other green LED turns on, when your 20 cm one yellow LED turns on, when your 15 cm the other LED turns on, when your 10 cm away one red LED turns on, and when your 5 cm away the last red LED turns on with the buzzer progressively getting louder each time. This circuit can help prevent collisions and can ensure safety.

Supplies

Materials Needed:

1x Arduino Uno

1x Breadboard

1x Ultrasonic Sensor

1x Buzzer

2x Green LEDs

2x Yellow LEDs

2x Red LEDs

7x 330 ohm Resistors

A lot of jumper wires

Setup

Screenshot 2024-06-17 123204.png

The photo above shows the setup of the project.


Connect a jumper wire from the 5v pin on the Arduino to the positive of the breadboard

Connect another jumper wire from the ground pin on the Arduino to the negative of the breadboard


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 jumper wires connected to the LEDs should be connected to the lead on the right, while the left lead of the LED should connected to the ground channel via a 330 ohm resistor.

Assemble Breadboard

First, let's connect the 5v and ground pin to the breadboard. Again, the wire attached to the 5v pin should be connected to the positive of the breadboard, while the wire attached to the ground pin should be connected to the negative of the breadboard.

Assemble Ultrasonic Sensor

Now it's time to attach the Ultrasonic Sensor. It is easiest to place the ultrasonic sensor as far right to the breadboard as possible. Looking back to the setup picture, you should connect the ground pin on the ultrasonic sensor to the ground on the breadboard. Next connect the Echo pin on the sensor to pin 6 on the Arduino. Now connect the Trig pin on the sensor to pin 7 on the Arduino, and lastly connect the VCC pin on the sensor to the positive on the breadboard.

Assemble LEDs

Next is connecting the LED's to the breadboard and Arduino. Once again looking back to the setup picture, attaching the LEDs is pretty basic. The way to connect them is to connect the anode, or the longer leg, to a pin on the Arduino with a jumper wire, and to connect the cathode, or the shorter leg, to the ground channel on the breadboard using a 330 ohm resistor. Then just repeat that step for all six of the LEDs, with the red LED all the way on the right being connected to pin 8 on the Arduino, the anode of the red LED to the left of that one being connected to pin 9 on the Arduino, and so on. The last LED, being the green LED all the way on the left, should have it's anode, or the longer leg, connected to pin 13 on the Arduino.

Assemble Buzzer

The last part of the physical setup for this build is attaching the buzzer to the breadboard and the Arduino. This part is probably the easiest part of the setup. All you have to do is attach the longer leg of the buzzer to pin 3 of the Arduino and attach the shorter leg of the buzzer to the ground channel of the breadboard.

Code

#define trigPin 7

#define echoPin 6

#define led1 13

#define led2 12

#define led3 11

#define led4 10

#define led5 9

#define led6 8

#define buzzer 3


int sound = 250;



void setup() {

 Serial.begin (9600);

 pinMode(trigPin, OUTPUT);

 pinMode(echoPin, INPUT);

 pinMode(led1, 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(led1, HIGH);

  sound = 250;

}

 else {

  digitalWrite(led1,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);

}


Once you've done that, and you've plugged in your Arduino to your computer, run the code and you're finished.