Тёмный

Ultrasonic Sensor HC-SR04 and Arduino Tutorial 

How To Mechatronics
Подписаться 685 тыс.
Просмотров 1,4 млн
50% 1

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)

Опубликовано:

 

11 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 503   
@HowToMechatronics
@HowToMechatronics 5 лет назад
Please note, circuit diagram at 0:56 should be: 5v to VCC, GND to GND, D10 to Trig, D9 to Echo.
@jacobhn2
@jacobhn2 5 лет назад
@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
@viswanathgoturu
@viswanathgoturu 5 лет назад
Could you please send full circuit diagram by connecting with LCD display
@saurabhsinghjat
@saurabhsinghjat 5 лет назад
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
@gracielaosuna3245
@gracielaosuna3245 5 лет назад
How To Mechatronics hablas español verdad
@icyciho1441
@icyciho1441 5 лет назад
my ultrasonic sensor got fried, i think. lol
@connortierney239
@connortierney239 11 месяцев назад
This video is incredible, code is so much easier to understand with the calculations. What a guy!!
@HowToMechatronics
@HowToMechatronics 11 месяцев назад
Glad it helped!
@gongye5133
@gongye5133 2 года назад
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.
@newhope5729
@newhope5729 Год назад
Why echo pin switches to high the moment we set trig pin to high?
@kingshahzad78
@kingshahzad78 5 лет назад
No Words for the appreciation dear. Hats Off. Keep making these type of tutorials.
@quaxiscorporationforresear5557
You have echo and vcc in series. The red wire should be I the right most pin of your diagram.
@red13emerald
@red13emerald 3 года назад
Even that is swapped, right? Red is vcc, so leftmost pin, and black is gnd, so the rightmost pin
@aumpauskar4653
@aumpauskar4653 2 года назад
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(" "); } ```
@weyprozz-_-891
@weyprozz-_-891 Год назад
toi alors toi merci
@virunanadun4323
@virunanadun4323 9 месяцев назад
thank you very mush brother.I struggled with a wrong code for a week.error was I used ' ' instead of " "
@omarqattosh8418
@omarqattosh8418 2 года назад
very helpful thank you my bro keep it real love from india and battir
@omarqattosh8418
@omarqattosh8418 2 года назад
3rasi
@lovemadeinjapan
@lovemadeinjapan 2 года назад
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.
@Blinkation
@Blinkation 8 лет назад
you can fit the sensor on the arduino without a breadboard: just use pin 11 as vcc
@Blind_Beetle
@Blind_Beetle 10 месяцев назад
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!
@miguelmontalban9580
@miguelmontalban9580 4 года назад
I broke my ultrasonic sensor cause of your diagram 🙃
@meddiys6171
@meddiys6171 6 лет назад
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.
@symbianwater
@symbianwater Год назад
Yes follow the instructions on the site, not here. There you can copy and paste the code too.
@Rfartsjunior
@Rfartsjunior 16 дней назад
Thanks man 👍
@IamMrWu
@IamMrWu 5 месяцев назад
Thanks for sharing the code, but I was hopping to learn the physical setup of the US sensor as well
@ndoWare
@ndoWare 3 года назад
Cool project!. Besides arduino, I have also tried to use 2 esp8266, you can get sensors and displays without cables
@Frolov888
@Frolov888 5 лет назад
Мужик, красавчик, мудро поступил и делаешь контент на английском. Респект!
@m1nus0ne
@m1nus0ne 5 лет назад
Where's the diagram if u connect a lcd
@sommmathechamp5801
@sommmathechamp5801 5 лет назад
Thx dude. I never knew this channel was so helpful. Subbed.
@saurabhsinghjat
@saurabhsinghjat 5 лет назад
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
@justpncil
@justpncil Год назад
Thank you for frying my sensor.
@1itoric
@1itoric 7 лет назад
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
@northshorepx
@northshorepx 8 лет назад
Wonderful. Thank you so much. Very easy to follow and the maths was explained very simply.
@yodycastro
@yodycastro 4 года назад
Great tutorial dude!
@MMMMM...dumber
@MMMMM...dumber 3 года назад
I love the video, much easier to understand than the technical sheet
@BeckyMarshallDesign
@BeckyMarshallDesign 6 лет назад
Thank you! I'll be referencing this in my next video. Please be aware that your fritzing diagram is incorrect.
@anshumans1174
@anshumans1174 5 лет назад
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!
@HowToMechatronics
@HowToMechatronics 5 лет назад
The problem with the LCD might be the contrast. You need to apply an appropriate voltage to the contrast pin.
@abboy3769
@abboy3769 5 лет назад
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
@thiagoabsc
@thiagoabsc 3 года назад
Good and simple... ! Nice video, keep on man!
@drake282
@drake282 5 лет назад
Great tutorial although it would be nice to know how to connect the LCD screen especially where I would put the purple wire...lol
@AlbisGR
@AlbisGR Год назад
hey hotty
@dito9364
@dito9364 6 лет назад
I am a fan of your channel.please send a video of coding Bluetooth module for joystick module
@89elmonster
@89elmonster 5 месяцев назад
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
@quaxiscorporationforresear5557
I thought it was vcc, echo, trigger, gnd. I think your wiring diagram is wrong.
@HetanshuBharadiya
@HetanshuBharadiya 7 лет назад
can you provide a link to the correct one?
@Conor_Toole
@Conor_Toole 5 лет назад
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!
@elissitdesign
@elissitdesign 5 лет назад
You're incorrect. It's VCC, Trig, Echo, Gnd.
@warzone6700
@warzone6700 3 года назад
@@elissitdesign Nah my sensor is VCC, Echo, Trigger, then GND
@corpsecoder5216
@corpsecoder5216 2 года назад
This is awesome very detailed explaination
@AW-xj4un
@AW-xj4un 8 месяцев назад
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.
@juliusugay8810
@juliusugay8810 Год назад
You save my life thank you🥰
@juliusugay8810
@juliusugay8810 Год назад
The new library is very helpful
@autorotate1803
@autorotate1803 7 лет назад
Clear and concise! Thank you!!
@smoofness364
@smoofness364 11 месяцев назад
So cool! Thanks homeboy !!!!
@kanmatt3749
@kanmatt3749 5 лет назад
nice tutorial, easy to understand
@gautamahuja7469
@gautamahuja7469 6 лет назад
awesome work dude. can you please explain why did you set the trigger pin to LOW initially for 2 microseconds ?? thank you
@ArcanePath360
@ArcanePath360 4 года назад
Very to the point. Thanks!
@houthchernchantuen6234
@houthchernchantuen6234 7 лет назад
why you don't tell the connection with LCD pin
@fikrifathoni_te6574
@fikrifathoni_te6574 4 года назад
yess
@ExplozionFPV
@ExplozionFPV 4 года назад
@@fikrifathoni_te6574 same
@phillbon3457
@phillbon3457 4 года назад
Loads of shite on RU-vid for this! Fucking search for it u pleb Scares me that you have 45 likes... hes talking about the Sensor u bellends
@derpaflerpa6974
@derpaflerpa6974 4 года назад
@@phillbon3457 bruh he was just asking you prig
@AlbisGR
@AlbisGR Год назад
i want your kids
@elitecrafter8970
@elitecrafter8970 6 лет назад
Best code ever! Thank you :)
@mini_special
@mini_special 8 лет назад
Excellent tutorial! Thank you very much for your explanation!
@bobarnold4435
@bobarnold4435 8 лет назад
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
@bobarnold4435
@bobarnold4435 8 лет назад
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)?
@dpak85
@dpak85 2 года назад
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
@फणिश्रीःकविरायनि
Excuse me and hello. I tried doing this but found it saying the distance as 0 in all cases
@raymi9612
@raymi9612 4 года назад
Does this work with liquids, like water, for example?
@220219933
@220219933 4 года назад
can you also set the distance fixed while the system is doing other things ? thank you.
@Chanobass
@Chanobass 3 года назад
Hi! Excellent video! How could I run a video on my computer using only the ultrasonic sensor? That when I stand in front of the sensor, a certain video is executed. it's possible? Thank you!
@gracecore6662
@gracecore6662 2 года назад
You can do it by clicking Ctrl+Shift+M
@agranshusingh7223
@agranshusingh7223 7 лет назад
thanks for video I also bought it and it work I got inspired by you thanks
@dankclee
@dankclee 8 лет назад
Hi. The schematic at around 1min appears to be wrong. (Vcc to ground, Ground not connected, power connected to echo)
@elissitdesign
@elissitdesign 5 лет назад
FANTASTIC! I got it to work. I wish the code was a little more intuitive.
@AlbisGR
@AlbisGR Год назад
Hotttt
@MoreKevinLiang
@MoreKevinLiang 8 лет назад
Why do we have to clear the trig pin? And what's the delay for? I commented out the delay but I don't notice a difference.
@ranjeetjoshi1249
@ranjeetjoshi1249 5 лет назад
which connection pins for LCD with IC2 module for SDA and SCL???? what to connect in arduino?
@christianbautch4015
@christianbautch4015 3 года назад
Wouldn't the duration be equal to the delay time that you set the trig pin to high?
@Yashiro-nene_dies
@Yashiro-nene_dies 7 лет назад
Is their an actual practical application for using this or is this more for fun?
@ahmadrosadi8851
@ahmadrosadi8851 5 лет назад
Mr.. please give US the arduino sketch About it
@AlejandroLZuvic
@AlejandroLZuvic 8 лет назад
Great videos. Subscribed. Keep up!
@dreamingnight13
@dreamingnight13 4 года назад
So for 'mm' instead of 'cm' you can just change it to " s = t * 0.34 / 2 " ?
@OpticMoos
@OpticMoos 4 года назад
Op-Onion42 That’ll do it
@meheershukla7861
@meheershukla7861 9 лет назад
please can i ask how did you display the distance on the lcd screen
@allancarrillo2273
@allancarrillo2273 6 лет назад
excelente tutorial!
@indaneym
@indaneym 6 лет назад
I wonder if it's possible to use HC-SR04 for "Flow Meters". Would the flow of the water speed up the ultrasound?
@brianminyalwa9832
@brianminyalwa9832 5 лет назад
can you make an utrasonic water tank lavel indicator? maybe even control a pump with it
@shleshavan1999
@shleshavan1999 7 лет назад
Hi! I am a beginner into this concept and I would like to know which language you used to program the measurement of distance.
@fusseldieb
@fusseldieb 7 лет назад
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#
@saikokamiyt
@saikokamiyt 3 года назад
Hi, im thinking about build this for my car. do i need more then one sensor for front and back or is the angle of this sensor to small?
@LearnWithFardin
@LearnWithFardin 3 года назад
Can I use a temperature module in it to get a highly accurate distance?
@catchgenerics8667
@catchgenerics8667 7 лет назад
Does it have to constantly be connected to the computer to get info from the serial monitor?
@manishwadile8480
@manishwadile8480 6 лет назад
How max distance it will measure can we use in drones to measure height?
@IslamUniverse
@IslamUniverse 7 лет назад
hi.this video is a good illustration.can we find speed of the obstacle?
@blueghost1366
@blueghost1366 7 лет назад
I looked up that this version of sensor is vulernable to interference from other ultrsonic sensors, if that is the case could I use this sensor to create a Local positioning system using a tri-lateration? What are your thoughts? Thanks
@srigayathri7814
@srigayathri7814 4 года назад
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
@deemaal-shami7207
@deemaal-shami7207 8 лет назад
Thank you soooooo much! This really was useful.
@Znkank
@Znkank 5 лет назад
Perfect as always ty!
@mr.secret554
@mr.secret554 4 года назад
My serial monitor only shows this: 0000000000000000000 How can I fix it or where is my false?
@toxic-tr4se
@toxic-tr4se 4 года назад
Mine to
@paulrosenzweig1180
@paulrosenzweig1180 4 года назад
Mine to
@lukaslachmayr7093
@lukaslachmayr7093 4 года назад
@@paulrosenzweig1180 hast du es geschafft, weil ich habe das gleiche Problem( did you fix it and when yes how did you do it?)
@paulrosenzweig1180
@paulrosenzweig1180 4 года назад
Lukas Lachmayr no
@gehmirnichaufnsack1
@gehmirnichaufnsack1 4 года назад
@@lukaslachmayr7093 ​der Fehler ist in den ersten zwei Zeilen => trigPin = 10; echoPin = 9
@eliasrotich9911
@eliasrotich9911 2 года назад
hello can ultrasonic sensor work two task instances i.e to sense an object and an send information to servo motors to be used to turn when it sense an object
@krishj8011
@krishj8011 4 года назад
thanks... very useful
@grevel1376
@grevel1376 6 лет назад
Does it work for things tilted in some directions? Or for ball or hand?
@harshatejaswi1972
@harshatejaswi1972 6 лет назад
Can i use ESP8266 for sms notification? If it possible could you explain or plz share the link of the connections for our project..
@rahulghosh4289
@rahulghosh4289 5 лет назад
Can the trig pins for 2 or more sensors can be in same digital pin?
@ChMuhammadImran
@ChMuhammadImran 8 лет назад
excellent tutorial, How can i make it water proof to use it for my car , on rare bumper
@chimp5820
@chimp5820 3 года назад
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.
@powertekusa
@powertekusa 9 лет назад
Your video was informative however, I would like to ask you if it is possible and how would I change the measurement from CM to inches or feet ?
@beantown_billy2405
@beantown_billy2405 2 года назад
1 inch = 2.54 cm
@tuckerherzberg
@tuckerherzberg Год назад
I have a specific question about a function of this device. Can I email you?
@mams2944
@mams2944 8 лет назад
i want to make the same sensor from the scratch, but i have no idea about programming nor assembly, where should i start from?
@PhG1961
@PhG1961 8 лет назад
Nice video !! I liked it, it was usefull.
@PhG1961
@PhG1961 8 лет назад
Btw, where are you from ?
@SMD1999
@SMD1999 5 лет назад
Where can I get that same LDC display he uses in the video?
@pezzioliveira6
@pezzioliveira6 8 лет назад
Hey, nice tutorial! Do you think this sensor could be used to measure wind speed?
@JunaydKhattak
@JunaydKhattak 8 лет назад
+pezzioliveira6 No, I dont' think so.
@nasali5116
@nasali5116 9 лет назад
This is quite helpful . . thank you
@SeyitCanPir
@SeyitCanPir 3 месяца назад
Your the best
@ruitfsilva
@ruitfsilva 8 лет назад
Great video
@QwertYuiop-dg8yp
@QwertYuiop-dg8yp 4 года назад
Is there something like an expansion of this code on the timetravel? duration=pulsIn(echoPin, HIGH); Because i’m planning to use other radio frequency module but still same concept of getting distance as ultrasonic
@joypramanik6884
@joypramanik6884 3 года назад
what about angled distances can i mount two of them on my car's rear bumper bellow both tail lights and see the distance of a object lying exactly between the sensors ?
@gonzaloibarsingman7379
@gonzaloibarsingman7379 7 лет назад
Thanks for the video! Does it work if the object is not perpendicular to the direction of the sound wave?
@jomac_ph
@jomac_ph 11 месяцев назад
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
@kd7cwg
@kd7cwg 5 лет назад
Would this work to read the level of water in a tank?
@pcbekri340
@pcbekri340 8 месяцев назад
thank you so much
@dprakash31
@dprakash31 7 лет назад
hi I wanted to know if we can measure the distance in mms.. eg 3.3 cm or 10.5 cm as everyone on RU-vid is showing only 0 significant digit after decimal I.e. 3 cm or 10 cm... also are there more accurate sensors for measuring distance and displacement. please reply asap.. thank you. D Prakash
@AqRqTq
@AqRqTq 7 лет назад
+Dejan Nedelkovski I've been trying to to this for a few days, but I keep getting 0 distance on all codes I've used. I've tried the NewPing library but I can't seem to get it to work. Can you please help me?
@CaineTheBuilder
@CaineTheBuilder 6 лет назад
that happened to me too
@GamingKhazana
@GamingKhazana 6 лет назад
Contact me 8530799642
@abdullahalfarabe7679
@abdullahalfarabe7679 6 лет назад
Any solution? It's showing 0 distance :/
@GamingKhazana
@GamingKhazana 6 лет назад
Abdullah Al Farabe contact me 9975844642
@Aryan-dy4sr
@Aryan-dy4sr 6 лет назад
Put the trig pin on 9 and echo pin on 10 and then try.
@navjotsingh8372
@navjotsingh8372 6 лет назад
V good explanation
Далее
Уроки Ардуино. ПИД регулятор
28:57
How to Set Up an Ultrasonic Range Finder on an Arduino
12:30
Laser vs Ultrasonic - TOF10120 vs. HC-SR04
31:09
Просмотров 185 тыс.
Stepper Motors and Arduino - The Ultimate Guide
29:57
Let's build a voltage multiplier!
16:32
Просмотров 2,1 млн