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)

Комментарии • 502

  • @HowToMechatronics
    @HowToMechatronics  5 лет назад +141

    Please note, circuit diagram at 0:56 should be: 5v to VCC, GND to GND, D10 to Trig, D9 to Echo.

    • @jacobhn2
      @jacobhn2 5 лет назад +2

      @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 лет назад +6

      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 лет назад +8

      my ultrasonic sensor got fried, i think. lol

  • @connortierney239
    @connortierney239 11 месяцев назад +10

    This video is incredible, code is so much easier to understand with the calculations. What a guy!!

  • @kingshahzad78
    @kingshahzad78 5 лет назад +6

    No Words for the appreciation dear. Hats Off. Keep making these type of tutorials.

  • @aumpauskar4653
    @aumpauskar4653 2 года назад +10

    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 Год назад +1

      toi alors toi merci

    • @virunanadun4323
      @virunanadun4323 9 месяцев назад +1

      thank you very mush brother.I struggled with a wrong code for a week.error was I used ' ' instead of " "

  • @quaxiscorporationforresear5557
    @quaxiscorporationforresear5557 8 лет назад +50

    You have echo and vcc in series. The red wire should be I the right most pin of your diagram.

    • @red13emerald
      @red13emerald 3 года назад +3

      Even that is swapped, right? Red is vcc, so leftmost pin, and black is gnd, so the rightmost pin

  • @omarqattosh8418
    @omarqattosh8418 2 года назад +1

    very helpful
    thank you my bro
    keep it real
    love from india and battir

  • @Blind_Beetle
    @Blind_Beetle 10 месяцев назад +1

    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!

  • @lovemadeinjapan
    @lovemadeinjapan 2 года назад +4

    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.

  • @miguelmontalban9580
    @miguelmontalban9580 4 года назад +2

    I broke my ultrasonic sensor cause of your diagram 🙃

  • @symbianwater
    @symbianwater Год назад +1

    Yes follow the instructions on the site, not here. There you can copy and paste the code too.

  • @northshorepx
    @northshorepx 8 лет назад +2

    Wonderful. Thank you so much. Very easy to follow and the maths was explained very simply.

  • @ndoWare
    @ndoWare 3 года назад +4

    Cool project!. Besides arduino, I have also tried to use 2 esp8266, you can get sensors and displays without cables

  • @MMMMM...dumber
    @MMMMM...dumber 2 года назад

    I love the video, much easier to understand than the technical sheet

  • @m1nus0ne
    @m1nus0ne 5 лет назад +15

    Where's the diagram if u connect a lcd

  • @meddiys6171
    @meddiys6171 6 лет назад +2

    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.

  • @gongye5133
    @gongye5133 2 года назад +16

    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?

  • @Blinkation
    @Blinkation 8 лет назад +10

    you can fit the sensor on the arduino without a breadboard: just use pin 11 as vcc

  • @IamMrWu
    @IamMrWu 5 месяцев назад

    Thanks for sharing the code, but I was hopping to learn the physical setup of the US sensor as well

  • @Frolov888
    @Frolov888 5 лет назад

    Мужик, красавчик, мудро поступил и делаешь контент на английском. Респект!

  • @sommmathechamp5801
    @sommmathechamp5801 5 лет назад +2

    Thx dude. I never knew this channel was so helpful. Subbed.

  • @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

  • @elitecrafter8970
    @elitecrafter8970 6 лет назад +2

    Best code ever! Thank you :)

  • @autorotate1803
    @autorotate1803 7 лет назад +1

    Clear and concise! Thank you!!

  • @BeckyMarshallDesign
    @BeckyMarshallDesign 6 лет назад +1

    Thank you! I'll be referencing this in my next video. Please be aware that your fritzing diagram is incorrect.

  • @yodycastro
    @yodycastro 4 года назад +1

    Great tutorial dude!

  • @mini_special
    @mini_special 8 лет назад +4

    Excellent tutorial! Thank you very much for your explanation!

  • @anshumans1174
    @anshumans1174 5 лет назад +2

    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.

  • @corpsecoder5216
    @corpsecoder5216 2 года назад

    This is awesome very detailed explaination

  • @ArcanePath360
    @ArcanePath360 4 года назад +1

    Very to the point. Thanks!

  • @thiagoabsc
    @thiagoabsc 3 года назад

    Good and simple... ! Nice video, keep on man!

  • @Rfartsjunior
    @Rfartsjunior 7 дней назад +1

    Thanks man 👍

  • @saurabhsinghjat
    @saurabhsinghjat 5 лет назад +4

    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

  • @agranshusingh7223
    @agranshusingh7223 7 лет назад

    thanks for video I also bought it and it work I got inspired by you thanks

  • @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

  • @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.

  • @dito9364
    @dito9364 6 лет назад

    I am a fan of your channel.please send a video of coding Bluetooth module for joystick module

  • @justpncil
    @justpncil Год назад

    Thank you for frying my sensor.

  • @JohnCarlo-i9f
    @JohnCarlo-i9f 21 день назад

    no, because it goes from sender to receiver then bounce back to the sender, then
    2d= vt
    d= (vt)/2

  • @juliusugay8810
    @juliusugay8810 Год назад

    You save my life thank you🥰

  • @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

  • @kanmatt3749
    @kanmatt3749 5 лет назад

    nice tutorial, easy to understand

  • @smoofness364
    @smoofness364 11 месяцев назад

    So cool! Thanks homeboy !!!!

  • @Znkank
    @Znkank 5 лет назад

    Perfect as always ty!

  • @Yashiro-nene_dies
    @Yashiro-nene_dies 7 лет назад

    Is their an actual practical application for using this or is this more for fun?

  • @allancarrillo2273
    @allancarrillo2273 6 лет назад +1

    excelente tutorial!

  • @bobarnold4435
    @bobarnold4435 8 лет назад +2

    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)?

  • @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

  • @deemaal-shami7207
    @deemaal-shami7207 8 лет назад

    Thank you soooooo much! This really was useful.

  • @mandakinibhil575
    @mandakinibhil575 5 лет назад +1

    We can use
    Duration/29/2

  • @nasali5116
    @nasali5116 9 лет назад

    This is quite helpful . . thank you

  • @quaxiscorporationforresear5557
    @quaxiscorporationforresear5557 8 лет назад +94

    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 лет назад +9

      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 лет назад +1

      You're incorrect. It's VCC, Trig, Echo, Gnd.

    • @warzone6700
      @warzone6700 3 года назад +1

      @@elissitdesign Nah my sensor is VCC, Echo, Trigger, then GND

  • @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

  • @AlejandroLZuvic
    @AlejandroLZuvic 8 лет назад

    Great videos. Subscribed. Keep up!

  • @फणिश्रीःकविरायनि

    Excuse me and hello. I tried doing this but found it saying the distance as 0 in all cases

  • @brendanzink2780
    @brendanzink2780 6 лет назад

    Very cool, thank you for sharing!

  • @mr.secret554
    @mr.secret554 4 года назад +10

    My serial monitor only shows this: 0000000000000000000
    How can I fix it or where is my false?

    • @toxic-tr4se
      @toxic-tr4se 4 года назад +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

  • @ranjeetjoshi1249
    @ranjeetjoshi1249 5 лет назад +1

    which connection pins for LCD with IC2 module for SDA and SCL???? what to connect in arduino?

  • @IslamUniverse
    @IslamUniverse 6 лет назад

    hi.this video is a good illustration.can we find speed of the obstacle?

  • @dreamingnight13
    @dreamingnight13 4 года назад +2

    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

  • @houthchernchantuen6234
    @houthchernchantuen6234 7 лет назад +47

    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 года назад +1

      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

    • @derpaflerpa6974
      @derpaflerpa6974 4 года назад

      @@phillbon3457 bruh he was just asking you prig

    • @AlbisGR
      @AlbisGR Год назад

      i want your kids

  • @mdominic7
    @mdominic7 8 лет назад

    loved it...Thank you sir

  • @ashmiashiq1855
    @ashmiashiq1855 4 года назад

    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???????

  • @christianbautch4015
    @christianbautch4015 3 года назад +1

    Wouldn't the duration be equal to the delay time that you set the trig pin to high?

  • @SeyitCanPir
    @SeyitCanPir 3 месяца назад

    Your the best

  • @jomac_ph
    @jomac_ph 10 месяцев назад +1

    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

  • @pezzioliveira6
    @pezzioliveira6 8 лет назад +2

    Hey, nice tutorial! Do you think this sensor could be used to measure wind speed?

    • @JunaydKhattak
      @JunaydKhattak 8 лет назад

      +pezzioliveira6 No, I dont' think so.

  • @PhG1961
    @PhG1961 8 лет назад

    Nice video !! I liked it, it was usefull.

    • @PhG1961
      @PhG1961 8 лет назад

      Btw, where are you from ?

  • @asseilalhlafi128
    @asseilalhlafi128 9 лет назад

    very very helpful .... thanks a lot

  • @drake282
    @drake282 5 лет назад +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

  • @guywatcho1641
    @guywatcho1641 9 лет назад +1

    Very nice tutorial..

  • @brianminyalwa9832
    @brianminyalwa9832 5 лет назад

    can you make an utrasonic water tank lavel indicator?
    maybe even control a pump with it

  • @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

  • @krishj8011
    @krishj8011 4 года назад

    thanks... very useful

  • @ChMuhammadImran
    @ChMuhammadImran 8 лет назад

    excellent tutorial,
    How can i make it water proof to use it for my car , on rare bumper

  • @gonzaloibarsingman7379
    @gonzaloibarsingman7379 7 лет назад

    Thanks for the video! Does it work if the object is not perpendicular to the direction of the sound wave?

  • @manishwadile8480
    @manishwadile8480 6 лет назад +1

    How max distance it will measure can we use in drones to measure height?

  • @dankclee
    @dankclee 8 лет назад

    Hi. The schematic at around 1min appears to be wrong. (Vcc to ground, Ground not connected, power connected to echo)

  • @meheershukla7861
    @meheershukla7861 9 лет назад +2

    please can i ask how did you display the distance on the lcd screen

  • @vikasverma8668
    @vikasverma8668 4 года назад

    Sir ,The distance formula given by you is not showing accurate distance

  • @raymi9612
    @raymi9612 4 года назад

    Does this work with liquids, like water, for example?

  • @navjotsingh8372
    @navjotsingh8372 6 лет назад

    V good explanation

  • @shleshavan1999
    @shleshavan1999 7 лет назад +1

    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 лет назад +1

      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#

  • @pcbekri340
    @pcbekri340 8 месяцев назад

    thank you so much

  • @220219933
    @220219933 4 года назад

    can you also set the distance fixed while the system is doing other things ?
    thank you.

  • @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?

  • @piuschris
    @piuschris 9 лет назад

    Thank you very much.

  • @ruitfsilva
    @ruitfsilva 8 лет назад

    Great video

  • @ahmadrosadi8851
    @ahmadrosadi8851 5 лет назад +14

    Mr.. please give US the arduino sketch About it

  • @haiqaliskandaryahaya2598
    @haiqaliskandaryahaya2598 4 года назад +1

    have a pseudocode for this project?

  • @funnyman4585
    @funnyman4585 2 года назад +12

    that thing burned my hands

    • @lasanza1438
      @lasanza1438 5 месяцев назад

      IT JUST KEEPS ON BURNING ME ANYTIME I GIVE IT POWER AND GROUND

  • @Lockey007
    @Lockey007 6 лет назад

    Thank you!

  • @SharathSatheesh
    @SharathSatheesh 7 лет назад +1

    Just wondering; if there were two objects, could we distinguish between the two objects?

    • @munch15a
      @munch15a 7 лет назад

      not really using this sort of thing not with out very complex codes

  • @petric001
    @petric001 8 лет назад

    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

    • @petric001
      @petric001 8 лет назад

      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.

  • @qobilasqarov1470
    @qobilasqarov1470 2 года назад

    whether the wave exits the trig leg and enters the echo leg

  • @grevel1376
    @grevel1376 6 лет назад

    Does it work for things tilted in some directions? Or for ball or hand?

  • @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.

  • @grahinho
    @grahinho 7 лет назад

    thank you !

  • @gauranshjuneja8855
    @gauranshjuneja8855 5 лет назад +1

    in arduino uno i m out of pins what to do now ? should i purchase arduino mega?

  • @catchgenerics8667
    @catchgenerics8667 7 лет назад

    Does it have to constantly be connected to the computer to get info from the serial monitor?

  • @lukevogt3067
    @lukevogt3067 4 года назад

    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