Ultrasonic Sensor HC-SR04 and Arduino Tutorial

แชร์
ฝัง
  • เผยแพร่เมื่อ 25 ก.ค. 2015
  • Please note, circuit diagram at 0:56 should be: 5v to VCC, GND to GND, D10 to Trig, D9 to Echo.
    howtomechatronics.com/tutoria... ► 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:
    ► howtomechatronics.com/
    Like my page on Facebook:
    ► / howtomechatronics
    Add me on Google+:
    ►plus.google.com/+Howtomechatr...
    Music: Aduro by Jens Kiilstofte (machinimasound.com/music)

ความคิดเห็น • 498

  • @HowToMechatronics
    @HowToMechatronics  5 ปีที่แล้ว +137

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

    • @gracielaosuna3245
      @gracielaosuna3245 4 ปีที่แล้ว

      How To Mechatronics hablas español verdad

    • @icyciho1441
      @icyciho1441 4 ปีที่แล้ว +7

      my ultrasonic sensor got fried, i think. lol

  • @northshorepx
    @northshorepx 8 ปีที่แล้ว +2

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

  • @connortierney239
    @connortierney239 6 หลายเดือนก่อน +5

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

  • @MMMMM...dumber
    @MMMMM...dumber 2 ปีที่แล้ว

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

  • @kingshahzad78
    @kingshahzad78 4 ปีที่แล้ว +4

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

  • @autorotate1803
    @autorotate1803 6 ปีที่แล้ว

    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.

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

  • @ndoWare
    @ndoWare 2 ปีที่แล้ว +4

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

  • @mini_special
    @mini_special 7 ปีที่แล้ว +4

    Excellent tutorial! Thank you very much for your explanation!

  • @elitecrafter8970
    @elitecrafter8970 6 ปีที่แล้ว +2

    Best code ever! Thank you :)

  • @meddiys6171
    @meddiys6171 5 ปีที่แล้ว +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.

  • @Blinkation
    @Blinkation 7 ปีที่แล้ว +10

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

  • @Frolov888
    @Frolov888 4 ปีที่แล้ว

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

  • @Znkank
    @Znkank 4 ปีที่แล้ว

    Perfect as always ty!

  • @gautamahuja7469
    @gautamahuja7469 5 ปีที่แล้ว

    awesome work dude. can you please explain why did you set the trigger pin to LOW initially for 2 microseconds ??
    thank you

  • @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 2 ปีที่แล้ว +3

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

  • @sommmathechamp5801
    @sommmathechamp5801 4 ปีที่แล้ว +2

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

  • @allancarrillo2273
    @allancarrillo2273 6 ปีที่แล้ว +1

    excelente tutorial!

  • @gonzaloibarsingman7379
    @gonzaloibarsingman7379 6 ปีที่แล้ว

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

  • @deemaal-shami7207
    @deemaal-shami7207 8 ปีที่แล้ว

    Thank you soooooo much! This really was useful.

  • @ArcanePath360
    @ArcanePath360 4 ปีที่แล้ว +1

    Very to the point. Thanks!

  • @thiagoabsc
    @thiagoabsc 3 ปีที่แล้ว

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

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

  • @mdominic7
    @mdominic7 8 ปีที่แล้ว

    loved it...Thank you sir

  • @brendanzink2780
    @brendanzink2780 6 ปีที่แล้ว

    Very cool, thank you for sharing!

  • @nasali5116
    @nasali5116 8 ปีที่แล้ว

    This is quite helpful . . thank you

  • @agranshusingh7223
    @agranshusingh7223 7 ปีที่แล้ว

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

  • @IamMrWu
    @IamMrWu 17 วันที่ผ่านมา

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

  • @1itoric
    @1itoric 6 ปีที่แล้ว

    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

  • @meheershukla7861
    @meheershukla7861 8 ปีที่แล้ว +2

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

  • @yodycastro
    @yodycastro 3 ปีที่แล้ว +1

    Great tutorial dude!

  • @AlejandroLZuvic
    @AlejandroLZuvic 8 ปีที่แล้ว

    Great videos. Subscribed. Keep up!

  • @corpsecoder5216
    @corpsecoder5216 2 ปีที่แล้ว

    This is awesome very detailed explaination

  • @omarqattosh8418
    @omarqattosh8418 2 ปีที่แล้ว +1

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

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

  • @kanmatt3749
    @kanmatt3749 4 ปีที่แล้ว

    nice tutorial, easy to understand

  • @Lockey007
    @Lockey007 6 ปีที่แล้ว

    Thank you!

  • @ChMuhammadImran
    @ChMuhammadImran 7 ปีที่แล้ว

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

  • @IslamUniverse
    @IslamUniverse 6 ปีที่แล้ว

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

  • @blueghost1366
    @blueghost1366 6 ปีที่แล้ว

    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

  • @QwertYuiop-dg8yp
    @QwertYuiop-dg8yp 3 ปีที่แล้ว

    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

  • @grahinho
    @grahinho 6 ปีที่แล้ว

    thank you !

  • @89elmonster
    @89elmonster 17 ชั่วโมงที่ผ่านมา

    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

  • @grevel1376
    @grevel1376 6 ปีที่แล้ว

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

  • @guywatcho1641
    @guywatcho1641 8 ปีที่แล้ว +1

    Very nice tutorial..

  • @catchgenerics8667
    @catchgenerics8667 7 ปีที่แล้ว

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

  • @220219933
    @220219933 4 ปีที่แล้ว

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

  • @Blind_Beetle
    @Blind_Beetle 5 หลายเดือนก่อน +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!

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

  • @asseilalhlafi128
    @asseilalhlafi128 8 ปีที่แล้ว

    very very helpful .... thanks a lot

  • @zjacob60
    @zjacob60 4 ปีที่แล้ว

    just have a question, how would we be able to calculate the minimum distance reading or the resolution of this sensor?

  • @huotveasna5804
    @huotveasna5804 7 ปีที่แล้ว +1

    Hi, if I want to use 7 segment display, how I have to make it? Thanks

  • @joypramanik6884
    @joypramanik6884 2 ปีที่แล้ว

    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 ?

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

  • @christianbautch4015
    @christianbautch4015 2 ปีที่แล้ว +1

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

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

  • @aumpauskar4653
    @aumpauskar4653 ปีที่แล้ว +6

    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 4 หลายเดือนก่อน +1

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

  • @dhyaneshwargopianand9215
    @dhyaneshwargopianand9215 5 ปีที่แล้ว

    Can this waves are able to calculate angle of deflection of the medium

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

  • @siopaaooo
    @siopaaooo 4 ปีที่แล้ว

    Hi may I ask if the distance measured can be send through GSM to phone?

  • @juliusugay8810
    @juliusugay8810 ปีที่แล้ว

    You save my life thank you🥰

    • @juliusugay8810
      @juliusugay8810 ปีที่แล้ว

      The new library is very helpful

  • @lukevogt3067
    @lukevogt3067 3 ปีที่แล้ว

    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

  • @dito9364
    @dito9364 6 ปีที่แล้ว

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

  • @brianminyalwa9832
    @brianminyalwa9832 5 ปีที่แล้ว

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

  • @Jay-dl7to
    @Jay-dl7to 7 ปีที่แล้ว

    Can I directly connect the LCD with a LCD shield directly to the arduino uno

  • @wemakes2014
    @wemakes2014 4 ปีที่แล้ว

    Sir will you make a video that how to connect this sensor

  • @symbianwater
    @symbianwater 9 หลายเดือนก่อน +1

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

  • @AW-xj4un
    @AW-xj4un 3 หลายเดือนก่อน

    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.

  • @petipeti2997
    @petipeti2997 6 ปีที่แล้ว

    thank you :)

  • @gauranshjuneja8855
    @gauranshjuneja8855 5 ปีที่แล้ว +1

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

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

  • @manishwadile8480
    @manishwadile8480 6 ปีที่แล้ว +1

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

  • @dpak85
    @dpak85 ปีที่แล้ว

    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

  • @LearnWithFardin
    @LearnWithFardin 2 ปีที่แล้ว

    Can I use a temperature module in it to get a highly accurate distance?

  • @AbhishekKumar1902
    @AbhishekKumar1902 7 ปีที่แล้ว +2

    excellent tutorial

  • @m1nus0ne
    @m1nus0ne 4 ปีที่แล้ว +15

    Where's the diagram if u connect a lcd

  • @smoofness364
    @smoofness364 6 หลายเดือนก่อน

    So cool! Thanks homeboy !!!!

  • @piuschris
    @piuschris 8 ปีที่แล้ว

    Thank you very much.

  • @SMD1999
    @SMD1999 5 ปีที่แล้ว

    Where can I get that same LDC display he uses in the video?

  • @bobarnold4435
    @bobarnold4435 7 ปีที่แล้ว +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 7 ปีที่แล้ว

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

  • @dhackens
    @dhackens 7 ปีที่แล้ว

    Thanks I found this helpful :)

  • @the_noob_2136
    @the_noob_2136 5 ปีที่แล้ว

    I want to know that ,will this code going to run on MSP430 launch pad ?

  • @mandakinibhil575
    @mandakinibhil575 5 ปีที่แล้ว +1

    We can use
    Duration/29/2

  • @psaikiranyadav
    @psaikiranyadav 6 ปีที่แล้ว

    how the change in program when i eliminate lcd and place three 8 segmented display

  • @eliasrotich9911
    @eliasrotich9911 ปีที่แล้ว

    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

  • @MacRithvik
    @MacRithvik 7 ปีที่แล้ว

    Is there a way I can make the sensor produce higher frequency ultrasound ?
    If not, where can I find a sensor capable of producing around 96 KHz or even 161 KHz ?

  • @ranjeetjoshi1249
    @ranjeetjoshi1249 4 ปีที่แล้ว +1

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

  • @muhamadhaziq4839
    @muhamadhaziq4839 6 ปีที่แล้ว

    sir, where can i get this full model? Thank you

  • @navjotsingh8372
    @navjotsingh8372 5 ปีที่แล้ว

    V good explanation

  • @KennyGreenBean
    @KennyGreenBean 3 ปีที่แล้ว

    how do you do the wiring for the display thing?

  • @dheerajs_r679
    @dheerajs_r679 5 ปีที่แล้ว

    does the code used by you can be worked in arduino 1.8.8?

  • @pcbekri340
    @pcbekri340 2 หลายเดือนก่อน

    thank you so much

  • @miguelmontalban9580
    @miguelmontalban9580 4 ปีที่แล้ว +1

    I broke my ultrasonic sensor cause of your diagram 🙃

  • @Chanobass
    @Chanobass 2 ปีที่แล้ว

    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 ปีที่แล้ว +1

      You can do it by clicking Ctrl+Shift+M

  • @hamzakhattak5365
    @hamzakhattak5365 5 ปีที่แล้ว

    Can you provide the wiring diagram of LCD with this project?

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

  • @icantfindmycigarette
    @icantfindmycigarette 5 ปีที่แล้ว

    Hi! Here because of a science project, can I have the list of materials used?

  • @qobilasqarov1470
    @qobilasqarov1470 ปีที่แล้ว

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

  • @alexflorea43
    @alexflorea43 7 ปีที่แล้ว

    how do you make the com23 tab tell you what number ou want, i know how to pull it up, but dont know how to use it