Ultrasonic Sensor HC-SR04 and Arduino Tutorial
HTML-код
- Опубликовано: 3 окт 2024
- Please note, circuit diagram at 0:56 should be: 5v to VCC, GND to GND, D10 to Trig, D9 to Echo.
howtomechatron... ► More details, circuit schematics and codes from my official website.
In this Arduino Tutorial you will learn how to use the HC-SR04 Ultrasonic sensor. It can measure distance from 2 cm to 4 meters with a ranging accuracy up to 3mm. The working principle of this module is quite simple.
Visit HowToMechatronics.com for more Tutorials, Tips, Projects and How It Works videos:
► howtomechatron...
Like my page on Facebook:
► / howtomechatronics
Add me on Google+:
►plus.google.co...
Music: Aduro by Jens Kiilstofte (machinimasound.com/music)
Please note, circuit diagram at 0:56 should be: 5v to VCC, GND to GND, D10 to Trig, D9 to Echo.
@Mikolaj Kraszewski You may well have several sensors, you just have the first sensor finished before you start the next sensor maybe there should be a delay between the two sensor code
Could you please send full circuit diagram by connecting with LCD display
i m little confused you sent a 10 microseconds long pulse, doesn't matter where it goes it will be 10 sec long and " pulsein" is calculating the time from high(when the pulse enters) to low (when pulse ends) and it is 10 micro seconds.
but it is calculating the time from transmitting the pulse to receiving the pulse
How To Mechatronics hablas español verdad
my ultrasonic sensor got fried, i think. lol
This video is incredible, code is so much easier to understand with the calculations. What a guy!!
Glad it helped!
No Words for the appreciation dear. Hats Off. Keep making these type of tutorials.
The code
```
const int trigPin = 9;
const int echoPin = 10;
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration*0.034/2;
Serial.print("Distance: ");
Serial.print(distance);
Serial.print("
");
}
```
toi alors toi merci
thank you very mush brother.I struggled with a wrong code for a week.error was I used ' ' instead of " "
You have echo and vcc in series. The red wire should be I the right most pin of your diagram.
Even that is swapped, right? Red is vcc, so leftmost pin, and black is gnd, so the rightmost pin
very helpful
thank you my bro
keep it real
love from india and battir
3rasi
The code works fine, however the circuitry you have provided in the video is connected wrong.Other than that thank you for the explanation, greatly appreciated!
The code works, but is not the best to combine with other stuff. It is really advisable to connect the TRIG to pin 3 and let an interrupt do the measuring in the background, so you don't have puseIn halting your code completely while sensing.
I broke my ultrasonic sensor cause of your diagram 🙃
Yes follow the instructions on the site, not here. There you can copy and paste the code too.
Wonderful. Thank you so much. Very easy to follow and the maths was explained very simply.
Cool project!. Besides arduino, I have also tried to use 2 esp8266, you can get sensors and displays without cables
I love the video, much easier to understand than the technical sheet
Where's the diagram if u connect a lcd
Great job SIR, for those who ask about LCD connection please see the code (2.3.4.5.6.7 on arduino pins = 4.6.11.12.13.14 on LCD) thanks.
Maybe just one thing to add up, the moment you set trig pin to HIGH, echo pin will be switched to HIGH as well, until it detects the back wave. That's why the period of echo pin input on HIGH mode equals to the time ultrasonic wave travels.
Why echo pin switches to high the moment we set trig pin to high?
you can fit the sensor on the arduino without a breadboard: just use pin 11 as vcc
Thanks for sharing the code, but I was hopping to learn the physical setup of the US sensor as well
Мужик, красавчик, мудро поступил и делаешь контент на английском. Респект!
Thx dude. I never knew this channel was so helpful. Subbed.
Thank you for the video! Worked perfectly from the first time. I've just bought my uno 3 and unpacked it 5 minutes ago and this was firs project i did.
Pozdrav iz uk :)
Ivan
Best code ever! Thank you :)
Clear and concise! Thank you!!
Thank you! I'll be referencing this in my next video. Please be aware that your fritzing diagram is incorrect.
Great tutorial dude!
Excellent tutorial! Thank you very much for your explanation!
Hello. I built the regular circuit and it is working great! I tried to watch your LCD tutorial and use the code give, but the LCD is just turning on and not displaying anything. I would really appreciate it if you or someone else could paste a diagram of the circuit used to build the circuit with the LCD display. Thanks alot for the help and the great video!
The problem with the LCD might be the contrast. You need to apply an appropriate voltage to the contrast pin.
This is awesome very detailed explaination
Very to the point. Thanks!
Good and simple... ! Nice video, keep on man!
Thanks man 👍
i m little confused you sent a 10 microseconds long pulse, doesn't matter where it goes it will be 10 sec long and " pulsein" is calculating the time from high(when the pulse enters) to low (when pulse ends) and it is 10 micro seconds.
but it is calculating the time from transmitting the pulse to receiving the pulse
thanks for video I also bought it and it work I got inspired by you thanks
I came up with this project idea. I was like I finally invent something to be proud of hahah. when I started working on this project, I found out that this project is already done by this gentleman. anyway good job boss
I'm running this on Arduino Giga and IDE2.2.1. The serial monitor is showing 0 every other line. Any idea why?
Good video and very helpful.
I am a fan of your channel.please send a video of coding Bluetooth module for joystick module
Thank you for frying my sensor.
no, because it goes from sender to receiver then bounce back to the sender, then
2d= vt
d= (vt)/2
You save my life thank you🥰
The new library is very helpful
awesome work dude. can you please explain why did you set the trigger pin to LOW initially for 2 microseconds ??
thank you
nice tutorial, easy to understand
So cool! Thanks homeboy !!!!
Perfect as always ty!
Is their an actual practical application for using this or is this more for fun?
excelente tutorial!
I like your technique of highlighting code as you step through it. You have a good "classroom" approach; I wish everyone did this; it would allow absorbing info easier on us students. Tkx My problem: I'd like to use the HC-SR04 Ultrasonic sensor to shoot through a 7 cm opening (mason jar) and sense the liquid (alcohol) level in it from 3 cm (top of jar) to 20 cm (bottom of jar). Will the sides of the jar interfere with the sound pulse? Thanks
Thanks for the quick reply - so, if I understand you, the sides of the jar would obscure any reflexion from the liquid; the technology would not work inside a jar. Can you suggest any alternative that does not involve contact with the liquid (phytosanitary) or modification of the liquid container (using plain old mason jar)?
I have something more or less similar to this. It works in terms of reading cms but when the results are really inconsistent for me. If i have an object 20 cm awaay it will sometimes print out 20cm but mostly it will print out 1cm or 2 as if theres something messing with it. What could be the cause?
Awesome video thank you
Thank you soooooo much! This really was useful.
We can use
Duration/29/2
This is quite helpful . . thank you
I thought it was vcc, echo, trigger, gnd. I think your wiring diagram is wrong.
can you provide a link to the correct one?
just don't connect echo to power, instead connect vcc to power (don't connect vcc to gnd) and connect gnd to gnd on the arduino. echo and trig should be connected to 10 and 9. other than that good video!
You're incorrect. It's VCC, Trig, Echo, Gnd.
@@elissitdesign Nah my sensor is VCC, Echo, Trigger, then GND
Sir..if distance are different centimetre .the speed will be a same sir ..I'm mean s= 340m/s it will be a constant value for different cm away from an sensor or object.. Sir
Great videos. Subscribed. Keep up!
Excuse me and hello. I tried doing this but found it saying the distance as 0 in all cases
Very cool, thank you for sharing!
My serial monitor only shows this: 0000000000000000000
How can I fix it or where is my false?
Mine to
Mine to
@@paulrosenzweig1180 hast du es geschafft, weil ich habe das gleiche Problem( did you fix it and when yes how did you do it?)
Lukas Lachmayr no
@@lukaslachmayr7093 der Fehler ist in den ersten zwei Zeilen => trigPin = 10; echoPin = 9
which connection pins for LCD with IC2 module for SDA and SCL???? what to connect in arduino?
hi.this video is a good illustration.can we find speed of the obstacle?
So for 'mm' instead of 'cm' you can just change it to " s = t * 0.34 / 2 " ?
Op-Onion42 That’ll do it
why you don't tell the connection with LCD pin
yess
@@fikrifathoni_te6574 same
Loads of shite on RUclips for this!
Fucking search for it u pleb
Scares me that you have 45 likes... hes talking about the Sensor u bellends
@@phillbon3457 bruh he was just asking you prig
i want your kids
loved it...Thank you sir
at 4:07, if the bounced sound waves cause the echo pin to go high that means the echo pin will trigger after the sound waves already bounced off the surface.. Don't you need the echo pin start the timing at the same time the trig pin sets off the pulse???????
Wouldn't the duration be equal to the delay time that you set the trig pin to high?
Your the best
Your V and GND are opposites, fried up my sensor before I noticed. WTH bro, could've edited the video but you chose to upload it anyway
Hey, nice tutorial! Do you think this sensor could be used to measure wind speed?
+pezzioliveira6 No, I dont' think so.
Nice video !! I liked it, it was usefull.
Btw, where are you from ?
very very helpful .... thanks a lot
Great tutorial although it would be nice to know how to connect the LCD screen especially where I would put the purple wire...lol
hey hotty
Very nice tutorial..
can you make an utrasonic water tank lavel indicator?
maybe even control a pump with it
Do you think this is possible to use on an agriculture sprayer on the boom? The boom need to raise and lower automatically depending on fields level.
The sprayer travels forward in a speed of about 7km/h.
Tanks for Great intro in arduino world
thanks... very useful
excellent tutorial,
How can i make it water proof to use it for my car , on rare bumper
Thanks for the video! Does it work if the object is not perpendicular to the direction of the sound wave?
How max distance it will measure can we use in drones to measure height?
Hi. The schematic at around 1min appears to be wrong. (Vcc to ground, Ground not connected, power connected to echo)
please can i ask how did you display the distance on the lcd screen
Sir ,The distance formula given by you is not showing accurate distance
Does this work with liquids, like water, for example?
V good explanation
Hi! I am a beginner into this concept and I would like to know which language you used to program the measurement of distance.
That's a formula, not a program. The program used to program the Arduino, you can download at the Arduino.cc site and it is "Arduino" language, similar to C#
thank you so much
can you also set the distance fixed while the system is doing other things ?
thank you.
I wonder if it's possible to use HC-SR04 for "Flow Meters". Would the flow of the water speed up the ultrasound?
Thank you very much.
Great video
Mr.. please give US the arduino sketch About it
have a pseudocode for this project?
that thing burned my hands
IT JUST KEEPS ON BURNING ME ANYTIME I GIVE IT POWER AND GROUND
Thank you!
Just wondering; if there were two objects, could we distinguish between the two objects?
not really using this sort of thing not with out very complex codes
Vidim da si Dejan, pa reko da ne pisem na engleskom. Sve sam uradio kako treba i nece da mi radi. Pise mi da je Distance=0 i tako non stop
I'm using Uno,and ultrasonic sensor similar like yours. I'm watched andther videos and solve a problem. I used your code but i put pins different.And it's works.Thanks.
whether the wave exits the trig leg and enters the echo leg
Does it work for things tilted in some directions? Or for ball or hand?
Hi I think my neighbour is using ultrasound against me and it disrupts my sleep and gives sharp bursts of migraines. Does this sound familiar and do you have any ideas of what I an do because I reported it but its hard to prove.
thank you !
in arduino uno i m out of pins what to do now ? should i purchase arduino mega?
Does it have to constantly be connected to the computer to get info from the serial monitor?
Could you somehow code a trigger for when the distance is shorter than a certain length, then have a count of how many times that happens on the lcd