Upload
nazrin-gl
View
228
Download
0
Embed Size (px)
Citation preview
8/16/2019 Arduino Distance Detector With a Buzzer and LED
http://slidepdf.com/reader/full/arduino-distance-detector-with-a-buzzer-and-led 1/9
Arduino Distance Detector with a Buzzer and LED's
Step 1: Materials Needed
The Materials needed for this project are:
• (1x) Arduino Uno
• (1x) Breadboard
• (1x) HC-!"# Ultrasonic ensor
• (1x) Bu$$er
• (%x) &reen 's
• (%x) *ello+ 's
• (%x) !ed 's
• (,x) . oh/ !esistors
• A lot of ju/per +ires
8/16/2019 Arduino Distance Detector With a Buzzer and LED
http://slidepdf.com/reader/full/arduino-distance-detector-with-a-buzzer-and-led 2/9
Step 2: Setup
The photo abo0e sho+s the setup of the project The ju/per +ires shouldbe connected as follo+s:
Connect a ju/per +ire fro/ the 2 0olt pin on the Arduino to the botto/
channel of the breadboardConnect another ju/per +ire fro/ a 3round pin on the arduino to theupper channel of the breadboard
Bu$$er -4 pin
("n Ultrasonic ensor)cho -4 pin 5 Tri3 -4 pin ,
(6n "rder fro/ !i3ht to 'eft)
'1 -4 pin 7'% -4 pin 8' -4 pin 1.'# -4 pin 11'2 -4 pin 1%'5 -4 pin 1
The ju/per +ires connected to the 's should be connected to the leadon the ri3ht9 +hile the left lead of the ' should connected to the 3roundchannel 0ia a . oh/ resistor
8/16/2019 Arduino Distance Detector With a Buzzer and LED
http://slidepdf.com/reader/full/arduino-distance-detector-with-a-buzzer-and-led 3/9
Step 3: Assembly: Breadboard
irst thin3s ;rst9 let<s connect the 2 0olt and 3round pin to the breadboard
A3ain9 the +ire attached to the 2 0olt pin should be connected to the
botto/ channel of the breadboard9 +hile the +ire attached to the 3round
pin should be connected to the upper channel of the breadboard
Step 4: Assembly: Ultrasonic Sensor
8/16/2019 Arduino Distance Detector With a Buzzer and LED
http://slidepdf.com/reader/full/arduino-distance-detector-with-a-buzzer-and-led 4/9
=o+ it<s ti/e to attach the HC-!"# ultrasonic sensor 6t is easiest to
place the ultrasonic sensor as far ri3ht to the breadboard as possible
!eferrin3 bac> to the setup picture9 ?ou should connect the 3round pin on
the ultrasonic sensor to the 3round channel on the breadboard =extconnect the cho pin on the sensor to pin 5 on the Arduino =o+ connect
the Tri3 pin on the sensor to pin , on the Arduino9 and lastl? connect the
@CC pin on the sensor to the 2 0olt channel on the breadboard 6f ?ou did
that all correctl?9 ?our asse/bl? should loo> li>e the picture abo0e
Step 5: Assembly: LEDs
=ext is connectin3 the '<s to the breadboard and Arduino "nce a3ain
referrin3 bac> to the setup picture9 attachin3 the 's is prett? basic9 just
+ith a lot of repetition The +a? to connect the/ is to connect the anode9
or the lon3er le39 or the one on the ri3ht9 to a pin on the Arduino +ith a ju/per +ire9 and to connect the cathode9 or the shorter le39 or the one on
the left9 to the 3round channel on the breadboard usin3 a . oh/
resistor Then just repeat that step for all six of the 's9 +ith the red '
all the +a? on the ri3ht bein3 connected to pin 7 on the Arduino9 the
anode of the red ' to the left of that one bein3 connected to pin 8 on
the Arduino9 and so on The last '9 that bein3 the 3reen ' all the +a?
on the left9 should ha0e its anode9 or ri3ht le39 connected to pin 1 on the
Arduino "nce ?ou ha0e done that9 ?our setup should loo> so/ethin3 li>e
this
8/16/2019 Arduino Distance Detector With a Buzzer and LED
http://slidepdf.com/reader/full/arduino-distance-detector-with-a-buzzer-and-led 5/9
6C'A6M!
!esistors are not absolutel? necessar? for the build9 ho+e0er the are
hi3hl? reco//ended to be used The onl? reason 6 a/ not usin3 the/ is
because 6 don<t ha0e enou3h resistors
Step 6: Assembly: Buzzer
The last part of the ph?sical setup for this build is attachin3 the bu$$er to
the breadboard and the Arduino This part is probabl? the easiest part of
the setup All ?ou ha0e to do is attach the lon3er le3 of the bu$$er to pin
of the Arduino and attach the shorter le3 of the bu$$er to the 3round
channel of the breadboard
6C'A6M!
6t is H6&H'* reco//ended to use a resistor in connectin3 the shorter le3
of the bu$$er to the 3round channel of the breadboard This 3reatl?
reduces the 0olu/e of the bu$$er *ou don<t ha0e to use a resistor9 but if
?ou don<t9 the bu$$er +ill be 0er? loud and uite fran>l? anno?in3
8/16/2019 Arduino Distance Detector With a Buzzer and LED
http://slidepdf.com/reader/full/arduino-distance-detector-with-a-buzzer-and-led 6/9
Step 7: The Code
Now that you have finished the physical setup of the build, now its time for the
code. I assume that you already have the Arduino program on your computer,
so now all you have to do is copy and paste the code from below.
#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 setup() {
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);
8/16/2019 Arduino Distance Detector With a Buzzer and LED
http://slidepdf.com/reader/full/arduino-distance-detector-with-a-buzzer-and-led 7/9
}
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);
8/16/2019 Arduino Distance Detector With a Buzzer and LED
http://slidepdf.com/reader/full/arduino-distance-detector-with-a-buzzer-and-led 8/9
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,
8/16/2019 Arduino Distance Detector With a Buzzer and LED
http://slidepdf.com/reader/full/arduino-distance-detector-with-a-buzzer-and-led 9/9
run the code and you're finished. If you've followed all the directions, the
closer you're hand gets to the HC-SRO4, the LEDs should progressively light
up until and the closer your hand gets, the buzzer will produce a higher tone
each time.