Ultrasonic sensor HC-SR04 with Arduino(code explained) Distance Measuring Senosr -Arduino tutorial 9

Поделиться
HTML-код
  • Опубликовано: 29 янв 2025

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

  • @sanjusingha1714
    @sanjusingha1714 2 месяца назад +14

    Full Code:-
    int trig = 7;
    int echo = 6;
    int timeInMicro;
    int distanceInCm;
    void setup()
    {
    Serial.begin(9600); // Initialize serial communication
    pinMode(trig, OUTPUT);
    pinMode(echo, INPUT);
    }
    void loop()
    {
    digitalWrite(trig, LOW);
    delayMicroseconds(2);
    digitalWrite(trig, HIGH);
    delayMicroseconds(10);
    digitalWrite(trig, LOW);
    timeInMicro = pulseIn(echo, HIGH);
    distanceInCm = timeInMicro / 29 / 2;
    Serial.println(distanceInCm);
    delay(100);
    }
    Aim of this Full code:
    Sends a sound pulse.->Measures the time for the pulse to return.->Calculates the distance based on the speed of sound.->Displays the distance to the object in centimeters.
    Lines :-
    digitalWrite(trig, LOW);
    delayMicroseconds(2);
    digitalWrite(trig, HIGH);
    delayMicroseconds(10);
    digitalWrite(trig, LOW);
    Explaination
    This part of the code is used to send a trigger signal to the ultrasonic sensor so that it emits an ultrasonic pulse.
    Here's a breakdown of each part:
    1. digitalWrite(trig, LOW);: Sets the trig pin to LOW (0 volts) to ensure it starts with no signal.
    2. delayMicroseconds(2);: Pauses the program for 2 microseconds to give a short delay after setting the trigger to LOW.
    3. digitalWrite(trig, HIGH);: Sets the trig pin to HIGH (5 volts). This action starts the ultrasonic sensor's pulse, causing it to emit a burst of ultrasonic sound waves.
    4. delayMicroseconds(10);: Holds the trig pin HIGH for 10 microseconds. This is the duration of the ultrasonic pulse sent by the sensor.
    5. digitalWrite(trig, LOW);: Sets the trig pin back to LOW, ending the pulse. The sensor then waits for the sound waves to bounce back.
    In summary, this sequence of commands creates a brief 10-microsecond pulse that tells the ultrasonic sensor to emit sound waves for distance measurement.
    Lines:-
    timeInMicro=pulseIn(echo,HIGH);
    distanceInCm=timeInMico/29/2;
    Serial.println(distanceInCm);
    delay(100);
    Explaination:-
    This part of the code measures the time it takes for the ultrasonic pulse to travel to an object and back, then calculates and displays the distance in centimeters.
    Here's a breakdown of each line:
    1. timeInMicro = pulseIn(echo, HIGH);
    This line waits for a pulse to be received on the echo pin and measures how long the pin stays HIGH (in microseconds). This time represents how long it took for the sound wave to travel to the object and bounce back to the sensor.
    2. distanceInCm = timeInMicro / 29 / 2;
    Here, the measured time timeInMicro is converted into distance.
    Dividing by 29 gives the distance in centimeters for a round trip (since sound travels at roughly 29 microseconds per centimeter).
    Dividing by 2 gives the one-way distance from the sensor to the object.
    3. Serial.println(distanceInCm);
    This line prints the calculated distance (in centimeters) to the Serial Monitor, allowing you to see the distance measurement on your computer.
    4. delay(100);
    This line pauses the program for 100 milliseconds before starting the next measurement. This delay prevents the sensor from continuously reading too quickly.
    In summary, these lines measure the time of the ultrasonic pulse, calculate the distance to the object, and display it on the Serial Monitor.
    Formulae:-
    Speed of Sound: 340m/s = 29microsecon
    Distance in Cms=microseconds/29/2
    Explaination of formulae:-
    This formula calculates the distance to an object based on the time sound takes to travel to it and return, using the known speed of sound.
    Explanation in Simple Terms
    1. Speed of Sound:
    Sound travels at a speed of 340 meters per second (m/s).
    This is equivalent to 29 microseconds per centimeter. In other words, it takes 29 microseconds (millionths of a second) for sound to travel 1 cm.
    2. Distance Formula:
    When we send an ultrasonic pulse from a sensor, it travels to an object and bounces back. The sensor measures the total travel time (round-trip) in microseconds.
    We use this time to calculate the distance to the object.
    3. Distance Calculation in Centimeters:
    Distance = (time in microseconds) / 29 / 2
    / 29 converts the total time into the round-trip distance in centimeters, since it takes 29 microseconds for sound to travel 1 cm.
    / 2 gives the one-way distance to the object (since the measured time includes the trip to the object and back).
    Example Calculation
    If the measured time is 580 microseconds, then:
    1. Round-trip Distance:
    -> 580 microseconds/29 = 20cm (round-trip)
    2. One-way Distance:
    20 cm / 2 = 10 cm
    So, the object is 10 cm away from the sensor.

    • @techathome
      @techathome  2 месяца назад

    • @SR-ok3su
      @SR-ok3su 2 месяца назад

      What is round strip distance

  • @YouTubeRynox
    @YouTubeRynox Месяц назад +1

    Code:
    int trig=7;
    int echo=6;
    long x;
    long y;
    void setup()
    {
    Serial.begin(9600);
    pinMode(7,OUTPUT);
    pinMode(6,INPUT);
    }
    void loop()
    {
    digitalWrite(trig,LOW);
    delayMicroseconds(2);
    digitalWrite(trig,HIGH);
    delayMicroseconds(10);
    digitalWrite(trig,LOW);
    x = pulseIn(echo,HIGH);
    y = ((x/29)/2);
    if (y

  • @meskhetian642
    @meskhetian642 2 года назад +3

    Hello, I have the same problem with LET’S UNBOX!, as you have stated, I checked the pins and replaced all the jumper wires, but it still does not work, it keeps repeating “0”. Would you possibly know what else is the problem?

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

      This issue is because, signals from sensor are not reaching Arduino pins,
      - Check wire
      - Check connections
      - Replace sensor

    • @YugChaturvedi-f7w
      @YugChaturvedi-f7w 19 дней назад

      You can also check if you have mentioned the echo pin as INPUT or OUTPUT

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

    Thank you
    From Morocco

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

    Thanks bro 👍👊👌

  • @Lak2606
    @Lak2606 Месяц назад

    the maximum my number is going to is 3, what is the error, code is working it goes down to 1 when i bring near but maximum is 3

    • @techathome
      @techathome  28 дней назад

      Which code are you using?

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

    Thanks for this video. I am not getting anything from the pulseIn() . Hence the distance is showing zero. I have two new modules. Both show the same result. What could be the reason ?

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

      Check the continuity of jumper wires, sometimes they will be faulty.

    • @anithasshenoy6662
      @anithasshenoy6662 10 месяцев назад

      @@techathome I removed it from breadboard and connected directly. Now it is working. thks

  • @amrabdlkadersabry5649
    @amrabdlkadersabry5649 4 месяца назад

    How can i use ultra sonic sensor without delay in the code

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

    Which board are you using ?

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

    is there any way using vb form to connect that?

  • @Pushpashreeanandan
    @Pushpashreeanandan 20 дней назад

    I didn't get output what is the reason

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

    make another 20 parts, very nice videos

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

      Thank you. We will make more..
      There are around 30 video as of now : ruclips.net/p/PL4B0LEKY-jrT_fjOGkX_NZ52ZHZf8s9S-

  • @easybreezy999
    @easybreezy999 2 месяца назад

    Can u give any video about obstacles avoiding robot car

    • @techathome
      @techathome  2 месяца назад

      As of now we have not done any video on this. Will try to make it.

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

    Why have you divided by?
    Due to 2 microsecond delay?

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

      We need half of the time taken by the sound. We get the total time, sensor to object and back. But we only need time taken to reach the object. So it's half.

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

    what is pinmode for??

    • @techathome
      @techathome  8 месяцев назад +1

      This is used to set the mode of the pin. Either you want to use particular pin as input or output.
      ruclips.net/video/EJEz6t5SpMw/видео.html

  • @yamanjain9346
    @yamanjain9346 Год назад +2

    Do you all were able to here audio after 3:42

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

      I checked now,
      Everything was fine before,
      Last week there was some copyright issue for background music.
      So we removed that.
      Somehow this explanation voice also got deleted.

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

      @@techathome unable to hear after 3:42

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

      There is no audio description still. Maybe you can edit or add text description to the video.

    • @SR-ok3su
      @SR-ok3su 2 месяца назад

      No

  • @Isanjay.13
    @Isanjay.13 Год назад

    Bro its emergency can we write the same program for the project train accident prevention if not can u pls say the procedures how to write tomorrow is my science expo only the coding is pending can u pls help bro plssss

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

      Can u send request on mail, I am not completely clear with your project requirements: deepakhd20@gmail.com

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

    Iska value itna fluctuate knyu kar rha he?

  • @shyamsundar7784
    @shyamsundar7784 Год назад +2

    Good explanation

  • @tharunraj814
    @tharunraj814 10 месяцев назад

    Sketch uses 2526 bytes (7%) of program storage space. Maximum is 32256 bytes.
    Global variables use 188 bytes (9%) of dynamic memory, leaving 1860 bytes for local variables. Maximum is 2048 bytes.
    avrdude: ser_open(): can't open device "\\.\COM5": Access is denied.
    Failed uploading: uploading error: exit status 1

    • @techathome
      @techathome  10 месяцев назад

      unplug and re-plug the board

  • @pizafox12
    @pizafox12 7 месяцев назад

    What program do you use?

    • @techathome
      @techathome  7 месяцев назад

      Arduino
      The link for code is in description

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

    how to increase the range of the ultrasonic sensor?

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

      It is not possible to increase the range with software.
      It depends on the specifications of ultrasonic sensor.

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

    Code rar file is not opening

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

      You have to extract it

  • @KnowPlants-5
    @KnowPlants-5 2 года назад

    Bro.
    I am getting some values and o in between the values ..

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

      If you are not inside its range,
      It will give some random values

  • @TestCreate-v6g
    @TestCreate-v6g 7 месяцев назад

    Cant find the google drive

    • @techathome
      @techathome  7 месяцев назад

      Can u send mail request: deepakhd20@gmail.com

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

    Sir sound is not coming

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

      Check the connections and jumper wires.

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

    I'm getting distance 0 on serial monitor everytime. What to do?

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

      Check the pins connections of ultrasonic sensor and also replace the jumper wires

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

      @@techathome ok I will try

    • @KnowPlants-5
      @KnowPlants-5 2 года назад

      @@Bigbrainie Did u solved it..

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

      @@KnowPlants-5 yes

  • @Bigbrainie
    @Bigbrainie 3 года назад +2

    Can we print the distance on lcd instead of serial? Where should I make the changes in code?
    If possible, please make a video.
    I am a beginner :-)

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

      We have done tutorial on that, you can refer to below two video links:
      Lcd1 - ruclips.net/video/fyL1h62QHJs/видео.html
      Lcd2 - ruclips.net/video/LyuybcoNEsw/видео.html

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

      @@techathome Thanks bro, tutorial 17 helped me!

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

      Welcome

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

      ​@@techathome hello sir.

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

      ​​@@techathome code pahije hota ya vdo cha

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

    the thing that shows the distance doesnt show up

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

    Your formula is wrong and is not working properly

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

    is it working

  • @VivekBJha
    @VivekBJha 7 месяцев назад

    Please can you give me pins in written form file is not opening

    • @techathome
      @techathome  7 месяцев назад

      Can u send mail: deepakhd20@gmail.com

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

    can you explain the formula bro
    why should we divede it by 9?

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

    subscribed :-)

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

    Thanks

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

    in second last line you haven't use semi colon ;

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

      Oh, my be we have to update the code file

  • @sanjusingha1714
    @sanjusingha1714 2 месяца назад

    No voice : 3:51 -5:45

    • @techathome
      @techathome  2 месяца назад

      Due to copyright issues on background music, audio has been removed.

    • @sanjusingha1714
      @sanjusingha1714 2 месяца назад

      @techathome oh.. so that's the reason

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

    help not working

  • @RarosKitchen
    @RarosKitchen 2 месяца назад

    OUTPUT SHOWS ONLY ZERO

    • @techathome
      @techathome  2 месяца назад

      Might be connection issue.
      Check the jumper wires also, some wires will be faulty.

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

    Bro some 800 , 1000 values are getting displayed how to avoid those values .?

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

      If you go beyond the range it gives random values sometimes. This is basic code to understand the working. Use ultrasonic library, this issue might resolve. Download and install this one: downloads.arduino.cc/libraries/github.com/ErickSimoes/Ultrasonic-3.0.0.zip

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

    I need ckt diagram

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

      Download the compressed file in description box and extract it

  • @nandikolasravani2498
    @nandikolasravani2498 Год назад +3

    Code please

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

      I have provided the zip file link in description, download and extract.

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

    There is no file at your google drive code section . Im reporting

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

      I have provided zip file : drive.google.com/file/d/1nBpfZG43UC30DHSi5e6XjFo3WjFhL3s1/view
      Download and extract it, you will get code and circuit.

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

      ​@@techathomeit's showing that the file is unsupported!

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

      Use winRaR software to extract on computer.