Components and supplies
Jumper wires (generic)
Arduino UNO
LED (generic)
Resistor 221 ohm
Breadboard (generic)
Buzzer
Ultrasonic Sensor - HC-SR04 (Generic)
Project description
Code
Ultrasonic Security System
arduino
Downloadable files
Step 2
Step 2
Step 2
Step 2
Comments
Only logged in users can leave comments
cuppermo
a year ago
#define trigPin 2 #define echoPin 3 #define LEDlampRed 4 #define LEDlampJaune 5 #define LEDlampGreen 6 #définir le buzzer sonore 7 must be according to your plan = #define trigPin 7 #define echoPin 6 #define LEDlampRed 9 #define LEDlampJaune 10 #define LEDlampGreen 11 #définir le buzzer sonore 3
Elcoco
a year ago
#define trigPin 2 #define echoPin 3 #define LEDlampRed 4 #define LEDlampJaune 5 #define LEDlampGreen 6 #définir le buzzer sonore 7 must be according to your plan = #define trigPin 7 #define echoPin 6 #define LEDlampRed 9 #define LEDlampJaune 10 #define LEDlampGreen 11 #définir le buzzer sonore 3
pavankr30
a year ago
#define trigPin 2 #define echoPin 3 #define LEDlampRed 4 #define LEDlampYellow 5 #define LEDlampGreen 6 #define soundbuzzer 7 int sound = 500; void setup() { Serial.begin(9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(LEDlampRed, OUTPUT); pinMode(LEDlampYellow, OUTPUT); pinMode(LEDlampGreen, OUTPUT); pinMode(soundbuzzer, OUTPUT); } void loop() { long durationindigit, distanceincm; digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); durationindigit = pulseIn(echoPin, HIGH); distanceincm = (durationindigit / 5) / 29.1; if (distanceincm < 50) { digitalWrite(LEDlampGreen, HIGH); } else { digitalWrite(LEDlampGreen, LOW); } if (distanceincm < 20) { // Changed 'distance' to 'distanceincm' digitalWrite(LEDlampYellow, HIGH); } else { digitalWrite(LEDlampYellow, LOW); } if (distanceincm < 5) { // Changed 'distance' to 'distanceincm' digitalWrite(LEDlampRed, HIGH); sound = 1000; } else { digitalWrite(LEDlampRed, LOW); } if (distanceincm > 5 || distanceincm <= 0) { // Changed 'distance' to 'distanceincm' Serial.println("Outside the permissible range of distances"); noTone(soundbuzzer); } else { Serial.print(distanceincm); // Changed 'distance' to 'distanceincm' Serial.println(" cm"); tone(soundbuzzer, sound); // Changed 'buzzer' to 'soundbuzzer' } delay(300); }
kwqrilkehr
a year ago
code dose not work :D
kwqrilkehr
a year ago
the code dose not work pls help
kwqrilkehr
a year ago
the code dosent work
dadsdasd
a year ago
code full of bugs shit programer
Dr_Prawn
a year ago
This code works in any case: const int trigPin = 2; const int echoPin = 3; const int LEDlampRed = 4; const int LEDlampYellow = 5; const int LEDlampGreen = 6; const int buzzer = 7; int sound = 500; long durationInDigit, distanceInCM; void setup() { Serial.begin (9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(LEDlampRed, OUTPUT); pinMode(LEDlampYellow, OUTPUT); pinMode(LEDlampGreen, OUTPUT); pinMode(buzzer, OUTPUT); } void loop() { digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); durationInDigit = pulseIn(echoPin, HIGH); distanceInCM = (durationInDigit * 0.034) / 2; if (distanceInCM > 50) { digitalWrite(LEDlampGreen, LOW); digitalWrite(LEDlampYellow, LOW); digitalWrite(LEDlampRed, LOW); noTone(buzzer); } else if (distanceInCM <= 50 && distanceInCM > 20) { digitalWrite(LEDlampGreen, HIGH); digitalWrite(LEDlampYellow, LOW); digitalWrite(LEDlampRed, LOW); noTone(buzzer); } else if (distanceInCM <= 20 && distanceInCM > 5) { digitalWrite(LEDlampYellow, HIGH); digitalWrite(LEDlampGreen, HIGH); digitalWrite(LEDlampRed, LOW); tone(buzzer, 500); } else { digitalWrite(LEDlampGreen, HIGH); digitalWrite(LEDlampYellow, HIGH); tone(buzzer, 1000); digitalWrite(LEDlampRed, HIGH); delay(300); digitalWrite(LEDlampRed, LOW); delay(300); } Serial.print(distanceInCM); Serial.println(" cm"); }
charsmora
a year ago
dr_langostino. te agradesco mucho solo hay que cambiarle los pines a como vienen en tu codigo es realmente funcional. soy nuevo en esto muchas gracias por compartir
Dr_Prawn
a year ago
Wow, how did that code actually work for you? Didn't you even test it? And the others here? Besides, your Fritzing! diagram shows a completely different connection method than your code. There is no clear line to be seen here at all. I would not recommend this to anyone here. You'd better learn C programming yourself, then you'll have more fun and get more out of it.
mashedpatato
a year ago
is there a video for this??
mastudio_dev1
a year ago
Cool
nikhil0001
2 years ago
please provide the processing code also here as soon as possible
coolcoder1
2 years ago
This is cool, it will be useful, and I look forward to making it. Your really smart dude. Respect.
dovym2000
2 years ago
Hello, I'm having a bit of a problem with the code saying that it has errors, any tips that might help?
solitaireplus
2 years ago
cool
tasneem787
2 years ago
can we relay for red led
mastercoder1234
2 years ago
Project not working, pls help!
mastercoder1234
2 years ago
Project not working, pls help!
tantonter4
2 years ago
Hey so im really new to this and i wanted to know if i still have to viod setup stuff or not?
tantonter4
2 years ago
Hey so im really new to this and i wanted to know if i still have to viod setup stuff or not?
11000
2 years ago
Good POst
mastudio_dev1
a year ago
Yes.
Anonymous user
2 years ago
can you plz tell me the app for yhe coding
Virtual_Eagle
2 years ago
You can do it online at the arduino website
Anonymous user
2 years ago
Code has wrongs!
tarantula3
2 years ago
Check this out mate: https://www.youtube.com/watch?v=LQGhprwuHe0
MylesBartlett
2 years ago
MaddMatt has fixed the code, but has still left in many errors. I have fixed them, and added more functionality below. The red light flashes, the buzzer triggers with sound 500 at less than 20cm and 1000 at less than 5cm. Distance judging was greatly improved. If the yellow light is on and nothing else is, the ultrasonic sensor is reporting a distance of 0cm as can be seen on the serial monitor. The "Outside the permissible range of distance" message is now banished, to top it all off. Code is below: const int trigPin = 2; const int echoPin = 3; const int LEDlampRed = 4; const int LEDlampYellow = 5; const int LEDlampGreen = 6; const int buzzer = 7; int sound = 500; void setup() { Serial.begin (9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(LEDlampRed, OUTPUT); pinMode(LEDlampYellow, OUTPUT); pinMode(LEDlampGreen, OUTPUT); pinMode(buzzer, OUTPUT); } void loop() { long durationindigit, distanceincm; digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); durationindigit = pulseIn(echoPin, HIGH); distanceincm = (durationindigit * 0.034) / 2; if (distanceincm > 50) { digitalWrite(LEDlampGreen, LOW); digitalWrite(LEDlampYellow, LOW); digitalWrite(LEDlampRed, LOW); noTone(buzzer); } else if (distanceincm <= 50 && distanceincm > 20) { digitalWrite(LEDlampGreen, HIGH); digitalWrite(LEDlampYellow, LOW); digitalWrite(LEDlampRed, LOW); noTone(buzzer); } else if (distanceincm <= 20 && distanceincm > 5) { digitalWrite(LEDlampYellow, HIGH); digitalWrite(LEDlampGreen, HIGH); digitalWrite(LEDlampRed, LOW); tone(buzzer, 500); } else if (distanceincm <= 0) { digitalWrite(LEDlampGreen, LOW); digitalWrite(LEDlampYellow, HIGH); digitalWrite(LEDlampRed, LOW); noTone(buzzer); } else { digitalWrite(LEDlampGreen, HIGH); digitalWrite(LEDlampYellow, HIGH); tone(buzzer, 1000); digitalWrite(LEDlampRed, HIGH); delay(300); digitalWrite(LEDlampRed, LOW); } Serial.print(distanceincm); Serial.println(" cm"); delay(300); }
moi45
2 years ago
ca ne marche pas non plus pour moi
tarantula3
2 years ago
https://youtu.be/LQGhprwuHe0
Anonymous user
2 years ago
This was my first project on Arduino and it works really well. as said above the code needs to be changed and also the pin numbers on the instructions are not the same as in the code. need to be changed! Thanks
tarantula3
2 years ago
Check this out mate: https://www.youtube.com/watch?v=LQGhprwuHe0 Here is another implementation of your project Similar approach but different project
Anonymous user
2 years ago
Just checked out your video... EXTREMELY COOL! I love your projects and your humor ... I've subscribed to your instructables and YOUTUBE
Anonymous user
2 years ago
How to do this same experiment on IOT platform , Could you please put the code here and complete procedure? using BLYNK and IOT cloud
Anonymous user
2 years ago
it says 'trigPin' was not declared in this scope when I verify it when I used the new code (haven't tried the original code yet).
IoT_hobbyist
2 years ago
You can try code in this tutorials: https://arduinogetstarted.com/tutorials/arduino-ultrasonic-sensor-piezo-buzzer
theottermaster
2 years ago
My sensor is not sensing, and I cant figure out what's wrong. Help plz
Anonymous user
2 years ago
#define trigPin 2 #define echoPin 3 #define LEDlampRed 4 #define LEDlampYellow 5 #define LEDlampGreen 6 #define soundbuzzer 7 int sound = 500; void setup() { Serial.begin (9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(LEDlampRed, OUTPUT); pinMode(LEDlampYellow, OUTPUT); pinMode(LEDlampGreen, OUTPUT); pinMode(soundbuzzer, OUTPUT); } void loop() { long durationindigit, distanceincm; digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); durationindigit = pulseIn(echoPin, HIGH); distanceincm = (durationindigit/5) / 29.1; if (distanceincm < 50) { digitalWrite(LEDlampGreen, HIGH); } else { digitalWrite(LEDlampGreen, LOW); } if (distance < 20) { digitalWrite(LEDlampYellow, HIGH); } else { digitalWrite(LEDlampYellow,LOW); } if (distance < 5) { digitalWrite(LEDlampRed, HIGH); sound = 1000; } else { digitalWrite(LEDlampRed,LOW); } if (distanceincm > 5 || distanceinsm <= 0){ Serial.println("Outside the permissible range of distances"); noTone(soundbuzzer); } else { Serial.print(distance); Serial.println(" cm"); tone(buzzer, sound); } delay(300); } IT DOSENT WORK!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Anonymous user
2 years ago
Thanks for posting! I am scanning projects to see which ones to do. Yours just says security system. It would be nice if you had a little more explanation saying what the expected behavior is.
tarantula3
2 years ago
Check this out mate: https://www.youtube.com/watch?v=LQGhprwuHe0 You may find what you are looking for
ESCUALO
2 years ago
no funciona la programacion
Anonymous user
2 years ago
This is an awesome project! I am curious if instead of LED's you could do a string of Neo pixels and the closer, or further something is to the sensor makes the Neo pixels lights up different colors. It would still be cool once the object is too close or to far to have the buzzer go off. Any thoughts??
Anonymous user
2 years ago
Wow, this explanation is awesome! I'm also using this type of ultrasonic sensor for my project. Just wondering if there is an intensity threshold for this ultrasonic sensor to consider a return signal as a valid echo? And are we allow to manipulate this threshold using Arduino?
Anonymous user
2 years ago
Can we do the project without buzzer and only with leds?
Anonymous user
2 years ago
is it possible to make this without the buzzer? if so what would the code then be?
Anonymous user
2 years ago
can i know how this thing work? what did it do?
Bravesoldiers
2 years ago
The project is good. I have a doubt whether the circuit works only when we use a yellow led? I tried to change the program to blue but it does not work.
Anonymous user
2 years ago
Could someone make blueprints of the board with 30 spaces? I only have a breadboard that is 30X10. If anyone could do this that would be Great.
Anonymous user
2 years ago
For some reason it says error i dun know why
Anonymous user
2 years ago
João Oliveira for the WIN
MylesBartlett
2 years ago
The original code of the project creator is incorrect. I have posted audited code.
Anonymous user
2 years ago
Put this code : #define trigPin 7 #define echoPin 6 #define LEDlampRed 9 #define LEDlampYellow 10 #define LEDlampGreen 11 #define soundbuzzer 3 int sound = 500; void setup() { Serial.begin (9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(LEDlampRed, OUTPUT); pinMode(LEDlampYellow, OUTPUT); pinMode(LEDlampGreen, OUTPUT); pinMode(soundbuzzer, OUTPUT); } void loop() { long durationindigit, distanceincm; digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); durationindigit = pulseIn(echoPin, HIGH); distanceincm = (durationindigit/5) / 29.1; digitalWrite(LEDlampGreen, HIGH); digitalWrite(LEDlampYellow, HIGH); digitalWrite(LEDlampRed, HIGH); if (distanceincm < 50) { digitalWrite(LEDlampGreen, HIGH); } else { digitalWrite(LEDlampGreen, LOW); } if (distanceincm < 20) { digitalWrite(LEDlampYellow, HIGH); } else { digitalWrite(LEDlampYellow,LOW); } if (distanceincm < 5) { digitalWrite(LEDlampRed, HIGH); sound = 1000; } else { digitalWrite(LEDlampRed,LOW); } if (distanceincm > 5 || distanceincm <= 0){ Serial.println("Outside the permissible range of distances"); noTone(soundbuzzer); } else { Serial.print(distanceincm); Serial.println(" cm"); tone(soundbuzzer, sound); } delay(300); }
Anonymous user
2 years ago
Why this code..instead of that..plz do reply with explanation
Anonymous user
2 years ago
this code only light on the led , the buzzer does not work
simoncrine
2 years ago
since the code didn't work for me her id digrent code you might need to change the wiring a little const int trigPin = 9; const int echoPin = 10; const int LEDG = 13; float duration, distance; const int LEDY = 12; const int LEDR = 11; const int BEEP = 8; void setup() { pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); Serial.begin(9600); pinMode(LEDG,OUTPUT); pinMode(LEDY,OUTPUT); pinMode(LEDR,OUTPUT); pinMode(BEEP,OUTPUT); } void loop() { digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = (duration*.0343)/2; Serial.print("Distance: "); Serial.println(distance); delay(500); if (distance > 50){ digitalWrite (LEDG, HIGH); digitalWrite (BEEP, LOW); }else{ digitalWrite(LEDG, LOW); } if ((distance >= 25) && (distance<= 50)){ digitalWrite (LEDY, HIGH); }else{ digitalWrite(LEDY, LOW); } if(distance < 25){ digitalWrite (LEDR, HIGH); digitalWrite (BEEP, HIGH); delay(1000); digitalWrite (BEEP, LOW); }else{ digitalWrite (LEDR, LOW); } }
titanic123
2 years ago
Hi! the nice project works perfectly. could you post the code and schematics for the same project but with a RGB LED? STILL a beginner so I couldn't figure it out
Anonymous user
2 years ago
Awesome project! But I just wanted to ask "distanceincm = (durationindigit/5) / 29.1;" what does the 29.1 means? Thanks for replying
Anonymous user
2 years ago
WHAT TF IS THIS!!!!!!
tarantula3
2 years ago
Nothing of your concern. Or you may watch this : https://youtu.be/LQGhprwuHe0
Anonymous user
2 years ago
Hi Ivan May I know if I am able to do this with S4A? If possible, can you please advise? Thank you!
Anonymous user
2 years ago
#define trigPin 2 #define echoPin 3 #define LEDlampRed 4 #define LEDlampYellow 5 #define LEDlampGreen 6 #define soundbuzzer 7 int sound = 500; void setup() { Serial.begin (9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(LEDlampRed, OUTPUT); pinMode(LEDlampYellow, OUTPUT); pinMode(LEDlampGreen, OUTPUT); pinMode(soundbuzzer, OUTPUT); } void loop() { long durationindigit, distanceincm; digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); durationindigit = pulseIn(echoPin, HIGH); distanceincm = (durationindigit/5) / 29.1; if (distanceincm < 50) { digitalWrite(LEDlampGreen, HIGH); } else { digitalWrite(LEDlampGreen, LOW); } if (distanceincm < 20) { digitalWrite(LEDlampYellow, HIGH); } else { digitalWrite(LEDlampYellow,LOW); } if (distanceincm < 5) { digitalWrite(LEDlampRed, HIGH); sound = 1000; } else { digitalWrite(LEDlampRed,LOW); } if (distanceincm > 5 || distanceincm <= 0){ Serial.println("Outside the permissible range of distances"); noTone(soundbuzzer); } else { Serial.print(distanceincm); Serial.println(" cm"); tone(soundbuzzer, sound); } delay(300); } the code need some slight adjustment as above and it worked .This was my first project for vaibhav chauhan
Anonymous user
2 years ago
This is good. My first project using the ULTRASONIC Module. I look forward to lots more.
Anonymous user
2 years ago
hello I am really interested to this project can I know if you have a soft copy of this project paper about your work
Anonymous user
2 years ago
Hey, i’m new to arduino and I saw this project. I’m really excited to do it but I don’t have the equipment for it. Is there a site were I can find this kit?
Nate_the_Great04
2 years ago
Yes, there is a kit for this. It is called The most complete starter kit. You can get it on amazon. But I don't know if it has a 221 ohm resistor though.
Anonymous user
2 years ago
Unless there isn’t a kit for this.
Anonymous user
2 years ago
the code is able to upload but the whole machine doesn't work so i went to check what was wrong at the serial monitor and it keeps saying that it is out of the permissible range of distances
panagorko
2 years ago
I've found an easy way to improve HCSR04 readings. I wish everyone will use that methods with HCSR04 because it's super simple. https://create.arduino.cc/projecthub/panagorko/next-level-ultrasonic-sensor-a67478
moses_shriver
2 years ago
suck
Nate_the_Great04
2 years ago
Could I use a 220 ohm resistor for a substitution for the 221 ohm resistor???
Anonymous user
2 years ago
sure you can
Anonymous user
2 years ago
In your explanation of the steps, in step 5 specifically, You say to put the LEDs in to the wrong pins on the Arduino board. You even say to put, both the red and green LEDs into pin 6. Which doesn't line up with your schematics or code
Anonymous user
2 years ago
Small things but your Fritzing Drawing does not match your written instruction (which are correct). Some of the code needed tweaking. Beyond that, this has been extremely helpful to me.
Anonymous user
2 years ago
Just wanna know, why are my LEDS not turning on??
D98431099
2 years ago
hello guys, can you please tell me the libraries that I have to add for the code to run.
Anonymous user
2 years ago
Code needs some slight amendments as below: #define trigPin 2 #define echoPin 3 #define LEDlampRed 4 #define LEDlampYellow 5 #define LEDlampGreen 6 #define soundbuzzer 7 int sound = 500; void setup() { Serial.begin (9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(LEDlampRed, OUTPUT); pinMode(LEDlampYellow, OUTPUT); pinMode(LEDlampGreen, OUTPUT); pinMode(soundbuzzer, OUTPUT); } void loop() { long durationindigit, distanceincm; digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); durationindigit = pulseIn(echoPin, HIGH); distanceincm = (durationindigit/5) / 29.1; if (distanceincm < 50) { digitalWrite(LEDlampGreen, HIGH); } else { digitalWrite(LEDlampGreen, LOW); } if (distanceincm < 20) { digitalWrite(LEDlampYellow, HIGH); } else { digitalWrite(LEDlampYellow,LOW); } if (distanceincm < 5) { digitalWrite(LEDlampRed, HIGH); sound = 1000; } else { digitalWrite(LEDlampRed,LOW); } if (distanceincm > 5 || distanceincm <= 0){ Serial.println("Outside the permissible range of distances"); noTone(soundbuzzer); } else { Serial.print(distanceincm); Serial.println(" cm"); tone(soundbuzzer, sound); } delay(300); }
Anonymous user
2 years ago
Remember to change the pins for this new code, it works great. Thanks
Anonymous user
2 years ago
Try to do it your own ...will help you to clear concepts
Nate_the_Great04
2 years ago
yes
Anonymous user
2 years ago
great! I'm just new to arduino and i thought something was up! thanks!
Anonymous user
2 years ago
i dont know what to change the pins to tho
Anonymous user
2 years ago
mr. nate when i use the code only the buzzer , work but no light on can fix it
Anonymous user
2 years ago
can i just simply copy and paste this into the library?
Anonymous user
2 years ago
what type for jumper wires do u need male to male?
Anonymous user
2 years ago
hey i am relling ur school
syntaxerrors
2 years ago
I managed to get this to work using an ELEGOO Mega 2560 Board! First "non example/lesson" project for me. Had to do quite a few tweaks as there are a few syntax errors within the original code but I managed to get it functioning properly. I'm very proud of myself :D Thanks for sharing!
splash1340
2 years ago
i did the programming and doesn't work
Anonymous user
2 years ago
How to do this same experiment on IOT platform , Could you please put the code here and complete procedure? using BLYNK and IOT cloud
Anonymous user
2 years ago
Just wanna know, why are my LEDS not turning on??
moses_shriver
3 years ago
suck
theottermaster
3 years ago
My sensor is not sensing, and I cant figure out what's wrong. Help plz
Anonymous user
4 years ago
Wow, this explanation is awesome! I'm also using this type of ultrasonic sensor for my project. Just wondering if there is an intensity threshold for this ultrasonic sensor to consider a return signal as a valid echo? And are we allow to manipulate this threshold using Arduino?
jancis_erika
4 years ago
hello I am really interested to this project can I know if you have a soft copy of this project paper about your work
Anonymous user
4 years ago
can i know how this thing work? what did it do?
Anonymous user
4 years ago
In your explanation of the steps, in step 5 specifically, You say to put the LEDs in to the wrong pins on the Arduino board. You even say to put, both the red and green LEDs into pin 6. Which doesn't line up with your schematics or code
Anonymous user
4 years ago
the code is able to upload but the whole machine doesn't work so i went to check what was wrong at the serial monitor and it keeps saying that it is out of the permissible range of distances
Anonymous user
4 years ago
This is an awesome project! I am curious if instead of LED's you could do a string of Neo pixels and the closer, or further something is to the sensor makes the Neo pixels lights up different colors. It would still be cool once the object is too close or to far to have the buzzer go off. Any thoughts??
kelechibg
4 years ago
This is good. My first project using the ULTRASONIC Module. I look forward to lots more.
D98431099
4 years ago
hello guys, can you please tell me the libraries that I have to add for the code to run.
panagorko
4 years ago
I've found an easy way to improve HCSR04 readings. I wish everyone will use that methods with HCSR04 because it's super simple. https://create.arduino.cc/projecthub/panagorko/next-level-ultrasonic-sensor-a67478
Anonymous user
4 years ago
Can we do the project without buzzer and only with leds?
titanic123
5 years ago
Hi! the nice project works perfectly. could you post the code and schematics for the same project but with a RGB LED? STILL a beginner so I couldn't figure it out
Anonymous user
5 years ago
#define trigPin 2 #define echoPin 3 #define LEDlampRed 4 #define LEDlampYellow 5 #define LEDlampGreen 6 #define soundbuzzer 7 int sound = 500; void setup() { Serial.begin (9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(LEDlampRed, OUTPUT); pinMode(LEDlampYellow, OUTPUT); pinMode(LEDlampGreen, OUTPUT); pinMode(soundbuzzer, OUTPUT); } void loop() { long durationindigit, distanceincm; digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); durationindigit = pulseIn(echoPin, HIGH); distanceincm = (durationindigit/5) / 29.1; if (distanceincm < 50) { digitalWrite(LEDlampGreen, HIGH); } else { digitalWrite(LEDlampGreen, LOW); } if (distance < 20) { digitalWrite(LEDlampYellow, HIGH); } else { digitalWrite(LEDlampYellow,LOW); } if (distance < 5) { digitalWrite(LEDlampRed, HIGH); sound = 1000; } else { digitalWrite(LEDlampRed,LOW); } if (distanceincm > 5 || distanceinsm <= 0){ Serial.println("Outside the permissible range of distances"); noTone(soundbuzzer); } else { Serial.print(distance); Serial.println(" cm"); tone(buzzer, sound); } delay(300); } IT DOSENT WORK!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Anonymous user
5 years ago
it says 'trigPin' was not declared in this scope when I verify it when I used the new code (haven't tried the original code yet).
IoT_hobbyist
2 years ago
You can try code in this tutorials: https://arduinogetstarted.com/tutorials/arduino-ultrasonic-sensor-piezo-buzzer
ESCUALO
5 years ago
no funciona la programacion
simoncrine
5 years ago
since the code didn't work for me her id digrent code you might need to change the wiring a little const int trigPin = 9; const int echoPin = 10; const int LEDG = 13; float duration, distance; const int LEDY = 12; const int LEDR = 11; const int BEEP = 8; void setup() { pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); Serial.begin(9600); pinMode(LEDG,OUTPUT); pinMode(LEDY,OUTPUT); pinMode(LEDR,OUTPUT); pinMode(BEEP,OUTPUT); } void loop() { digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = (duration*.0343)/2; Serial.print("Distance: "); Serial.println(distance); delay(500); if (distance > 50){ digitalWrite (LEDG, HIGH); digitalWrite (BEEP, LOW); }else{ digitalWrite(LEDG, LOW); } if ((distance >= 25) && (distance<= 50)){ digitalWrite (LEDY, HIGH); }else{ digitalWrite(LEDY, LOW); } if(distance < 25){ digitalWrite (LEDR, HIGH); digitalWrite (BEEP, HIGH); delay(1000); digitalWrite (BEEP, LOW); }else{ digitalWrite (LEDR, LOW); } }
Anonymous user
5 years ago
Small things but your Fritzing Drawing does not match your written instruction (which are correct). Some of the code needed tweaking. Beyond that, this has been extremely helpful to me.
Nate_the_Great04
5 years ago
Could I use a 220 ohm resistor for a substitution for the 221 ohm resistor???
Anonymous user
2 years ago
sure you can
Virtual_Eagle
5 years ago
Hey, i’m new to arduino and I saw this project. I’m really excited to do it but I don’t have the equipment for it. Is there a site were I can find this kit?
Virtual_Eagle
2 years ago
Unless there isn’t a kit for this.
Nate_the_Great04
2 years ago
Yes, there is a kit for this. It is called The most complete starter kit. You can get it on amazon. But I don't know if it has a 221 ohm resistor though.
Anonymous user
5 years ago
can you plz tell me the app for yhe coding
Anonymous user
2 years ago
You can do it online at the arduino website
SANDEEPCHAUHAN
5 years ago
#define trigPin 2 #define echoPin 3 #define LEDlampRed 4 #define LEDlampYellow 5 #define LEDlampGreen 6 #define soundbuzzer 7 int sound = 500; void setup() { Serial.begin (9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(LEDlampRed, OUTPUT); pinMode(LEDlampYellow, OUTPUT); pinMode(LEDlampGreen, OUTPUT); pinMode(soundbuzzer, OUTPUT); } void loop() { long durationindigit, distanceincm; digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); durationindigit = pulseIn(echoPin, HIGH); distanceincm = (durationindigit/5) / 29.1; if (distanceincm < 50) { digitalWrite(LEDlampGreen, HIGH); } else { digitalWrite(LEDlampGreen, LOW); } if (distanceincm < 20) { digitalWrite(LEDlampYellow, HIGH); } else { digitalWrite(LEDlampYellow,LOW); } if (distanceincm < 5) { digitalWrite(LEDlampRed, HIGH); sound = 1000; } else { digitalWrite(LEDlampRed,LOW); } if (distanceincm > 5 || distanceincm <= 0){ Serial.println("Outside the permissible range of distances"); noTone(soundbuzzer); } else { Serial.print(distanceincm); Serial.println(" cm"); tone(soundbuzzer, sound); } delay(300); } the code need some slight adjustment as above and it worked .This was my first project for vaibhav chauhan
moi45
2 years ago
ca ne marche pas
Anonymous user
5 years ago
is it possible to make this without the buzzer? if so what would the code then be?
Bravesoldiers
6 years ago
The project is good. I have a doubt whether the circuit works only when we use a yellow led? I tried to change the program to blue but it does not work.
Anonymous user
6 years ago
Awesome project! But I just wanted to ask "distanceincm = (durationindigit/5) / 29.1;" what does the 29.1 means? Thanks for replying
Anonymous user
6 years ago
Could someone make blueprints of the board with 30 spaces? I only have a breadboard that is 30X10. If anyone could do this that would be Great.
Anonymous user
2 years ago
c,dgmjokfnbobmgfbbmlvmbonkm
Anonymous user
6 years ago
WHAT TF IS THIS!!!!!!
tarantula3
2 years ago
Nothing of your concern. Or you may watch this : https://youtu.be/LQGhprwuHe0
Anonymous user
6 years ago
Hi Ivan May I know if I am able to do this with S4A? If possible, can you please advise? Thank you!
MylesBartlett
6 years ago
MaddMatt has fixed the code, but has still left in many errors. I have fixed them, and added more functionality below. The red light flashes, the buzzer triggers with sound 500 at less than 20cm and 1000 at less than 5cm. Distance judging was greatly improved. If the yellow light is on and nothing else is, the ultrasonic sensor is reporting a distance of 0cm as can be seen on the serial monitor. The "Outside the permissible range of distance" message is now banished, to top it all off. Code is below: const int trigPin = 2; const int echoPin = 3; const int LEDlampRed = 4; const int LEDlampYellow = 5; const int LEDlampGreen = 6; const int buzzer = 7; int sound = 500; void setup() { Serial.begin (9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(LEDlampRed, OUTPUT); pinMode(LEDlampYellow, OUTPUT); pinMode(LEDlampGreen, OUTPUT); pinMode(buzzer, OUTPUT); } void loop() { long durationindigit, distanceincm; digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); durationindigit = pulseIn(echoPin, HIGH); distanceincm = (durationindigit * 0.034) / 2; if (distanceincm > 50) { digitalWrite(LEDlampGreen, LOW); digitalWrite(LEDlampYellow, LOW); digitalWrite(LEDlampRed, LOW); noTone(buzzer); } else if (distanceincm <= 50 && distanceincm > 20) { digitalWrite(LEDlampGreen, HIGH); digitalWrite(LEDlampYellow, LOW); digitalWrite(LEDlampRed, LOW); noTone(buzzer); } else if (distanceincm <= 20 && distanceincm > 5) { digitalWrite(LEDlampYellow, HIGH); digitalWrite(LEDlampGreen, HIGH); digitalWrite(LEDlampRed, LOW); tone(buzzer, 500); } else if (distanceincm <= 0) { digitalWrite(LEDlampGreen, LOW); digitalWrite(LEDlampYellow, HIGH); digitalWrite(LEDlampRed, LOW); noTone(buzzer); } else { digitalWrite(LEDlampGreen, HIGH); digitalWrite(LEDlampYellow, HIGH); tone(buzzer, 1000); digitalWrite(LEDlampRed, HIGH); delay(300); digitalWrite(LEDlampRed, LOW); } Serial.print(distanceincm); Serial.println(" cm"); delay(300); }
tarantula3
2 years ago
https://youtu.be/LQGhprwuHe0
tarantula3
6 years ago
Check this out mate: https://www.youtube.com/watch?v=LQGhprwuHe0 Here is another implementation of your project Similar approach but different project
Anonymous user
2 years ago
Just checked out your video... EXTREMELY COOL! I love your projects and your humor ... I've subscribed to your instructables and YOUTUBE
Anonymous user
7 years ago
Thanks for posting! I am scanning projects to see which ones to do. Yours just says security system. It would be nice if you had a little more explanation saying what the expected behavior is.
tarantula3
2 years ago
Check this out mate: https://www.youtube.com/watch?v=LQGhprwuHe0 You may find what you are looking for
Anonymous user
7 years ago
Code has wrongs!
tarantula3
2 years ago
Check this out mate: https://www.youtube.com/watch?v=LQGhprwuHe0
Anonymous user
7 years ago
For some reason it says error i dun know why
Anonymous user
2 years ago
João Oliveira for the WIN
Anonymous user
2 years ago
this code only light on the led , the buzzer does not work
Anonymous user
2 years ago
Why this code..instead of that..plz do reply with explanation
MylesBartlett
2 years ago
The original code of the project creator is incorrect. I have posted audited code.
Anonymous user
2 years ago
Put this code : #define trigPin 7 #define echoPin 6 #define LEDlampRed 9 #define LEDlampYellow 10 #define LEDlampGreen 11 #define soundbuzzer 3 int sound = 500; void setup() { Serial.begin (9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(LEDlampRed, OUTPUT); pinMode(LEDlampYellow, OUTPUT); pinMode(LEDlampGreen, OUTPUT); pinMode(soundbuzzer, OUTPUT); } void loop() { long durationindigit, distanceincm; digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); durationindigit = pulseIn(echoPin, HIGH); distanceincm = (durationindigit/5) / 29.1; digitalWrite(LEDlampGreen, HIGH); digitalWrite(LEDlampYellow, HIGH); digitalWrite(LEDlampRed, HIGH); if (distanceincm < 50) { digitalWrite(LEDlampGreen, HIGH); } else { digitalWrite(LEDlampGreen, LOW); } if (distanceincm < 20) { digitalWrite(LEDlampYellow, HIGH); } else { digitalWrite(LEDlampYellow,LOW); } if (distanceincm < 5) { digitalWrite(LEDlampRed, HIGH); sound = 1000; } else { digitalWrite(LEDlampRed,LOW); } if (distanceincm > 5 || distanceincm <= 0){ Serial.println("Outside the permissible range of distances"); noTone(soundbuzzer); } else { Serial.print(distanceincm); Serial.println(" cm"); tone(soundbuzzer, sound); } delay(300); }
Anonymous user
7 years ago
This was my first project on Arduino and it works really well. as said above the code needs to be changed and also the pin numbers on the instructions are not the same as in the code. need to be changed! Thanks
Anonymous user
7 years ago
Code needs some slight amendments as below: #define trigPin 2 #define echoPin 3 #define LEDlampRed 4 #define LEDlampYellow 5 #define LEDlampGreen 6 #define soundbuzzer 7 int sound = 500; void setup() { Serial.begin (9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(LEDlampRed, OUTPUT); pinMode(LEDlampYellow, OUTPUT); pinMode(LEDlampGreen, OUTPUT); pinMode(soundbuzzer, OUTPUT); } void loop() { long durationindigit, distanceincm; digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); durationindigit = pulseIn(echoPin, HIGH); distanceincm = (durationindigit/5) / 29.1; if (distanceincm < 50) { digitalWrite(LEDlampGreen, HIGH); } else { digitalWrite(LEDlampGreen, LOW); } if (distanceincm < 20) { digitalWrite(LEDlampYellow, HIGH); } else { digitalWrite(LEDlampYellow,LOW); } if (distanceincm < 5) { digitalWrite(LEDlampRed, HIGH); sound = 1000; } else { digitalWrite(LEDlampRed,LOW); } if (distanceincm > 5 || distanceincm <= 0){ Serial.println("Outside the permissible range of distances"); noTone(soundbuzzer); } else { Serial.print(distanceincm); Serial.println(" cm"); tone(soundbuzzer, sound); } delay(300); }
Anonymous user
2 years ago
mr. nate when i use the code only the buzzer , work but no light on can fix it
Nate_the_Great04
2 years ago
yes
Harrisonfamily
2 years ago
Remember to change the pins for this new code, it works great. Thanks
Anonymous user
2 years ago
Try to do it your own ...will help you to clear concepts
Anonymous user
2 years ago
i dont know what to change the pins to tho
onetwofive
2 years ago
can i just simply copy and paste this into the library?
Anonymous user
2 years ago
great! I'm just new to arduino and i thought something was up! thanks!
Krepak
21 Followers
•2 Projects
202
149
rezznor87
10 months ago
It would be really nice if someone could fix this code............ Pretty Crazy this Crap Code has been on Project hub this long.....