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

Поділитися
Вставка
  • Опубліковано 18 гру 2024

КОМЕНТАРІ • 94

  • @sanjusingha1714
    @sanjusingha1714 Місяць тому +11

    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.

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

  • @VENU_GAMING
    @VENU_GAMING Годину тому

    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

  • @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 3 місяці тому

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

    • @SR-ok3su
      @SR-ok3su 21 день тому

      No

  • @amrabdlkadersabry5649
    @amrabdlkadersabry5649 2 місяці тому

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

  • @anithasshenoy6662
    @anithasshenoy6662 9 місяців тому

    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  9 місяців тому +1

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

    • @anithasshenoy6662
      @anithasshenoy6662 9 місяців тому

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

  • @kapil409
    @kapil409 Рік тому +1

    Ser_open() : can't set com- state showing uploading error

    • @techathome
      @techathome  Рік тому

      Unplug and replug again and try.
      Also plug it to different com port.

  • @khannreactions3311
    @khannreactions3311 2 роки тому

    is there any way using vb form to connect that?

  • @kirans9395
    @kirans9395 Рік тому +1

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

    • @krk101
      @krk101 8 місяців тому +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.

  • @tharunraj814
    @tharunraj814 9 місяців тому

    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  9 місяців тому

      unplug and re-plug the board

  • @yourevolution7850
    @yourevolution7850 9 місяців тому +1

    Iska value itna fluctuate knyu kar rha he?

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

  • @ramybrahim3188
    @ramybrahim3188 Рік тому +1

    Thank you
    From Morocco

  • @mrwildmouth5876
    @mrwildmouth5876 Рік тому

    Which board are you using ?

  • @easybreezy999
    @easybreezy999 Місяць тому

    Can u give any video about obstacles avoiding robot car

    • @techathome
      @techathome  Місяць тому

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

  • @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 - ua-cam.com/video/fyL1h62QHJs/v-deo.html
      Lcd2 - ua-cam.com/video/LyuybcoNEsw/v-deo.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

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

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

  • @electro2585
    @electro2585 Рік тому +1

    Your formula is wrong and is not working properly

  • @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 : ua-cam.com/play/PL4B0LEKY-jrT_fjOGkX_NZ52ZHZf8s9S-.html

  • @muninggamer7621
    @muninggamer7621 10 місяців тому

    how to increase the range of the ultrasonic sensor?

    • @techathome
      @techathome  10 місяців тому

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

  • @VivekBJha
    @VivekBJha 6 місяців тому

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

    • @techathome
      @techathome  6 місяців тому

      Can u send mail: deepakhd20@gmail.com

  • @sreekanthneelam5268
    @sreekanthneelam5268 2 місяці тому

    Code rar file is not opening

    • @techathome
      @techathome  2 місяці тому

      You have to extract it

  • @shivufpv6248
    @shivufpv6248 4 роки тому +3

    Thanks bro 👍👊👌

  • @bozakardude
    @bozakardude Рік тому

    the thing that shows the distance doesnt show up

  • @ushakiran8285
    @ushakiran8285 Рік тому

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

  • @ravikumarkankanala4092
    @ravikumarkankanala4092 4 місяці тому

    Sir sound is not coming

    • @techathome
      @techathome  4 місяці тому

      Check the connections and jumper wires.

  • @hahahahahaha664
    @hahahahahaha664 6 місяців тому

    what is pinmode for??

    • @techathome
      @techathome  6 місяців тому +1

      This is used to set the mode of the pin. Either you want to use particular pin as input or output.
      ua-cam.com/video/EJEz6t5SpMw/v-deo.html

  • @TestCreate-v6g
    @TestCreate-v6g 5 місяців тому

    Cant find the google drive

    • @techathome
      @techathome  5 місяців тому

      Can u send mail request: deepakhd20@gmail.com

  • @pizafox12
    @pizafox12 5 місяців тому

    What program do you use?

    • @techathome
      @techathome  5 місяців тому

      Arduino
      The link for code is in description

  • @shyamsundar7784
    @shyamsundar7784 Рік тому +2

    Good explanation

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

  • @sanjusingha1714
    @sanjusingha1714 Місяць тому

    No voice : 3:51 -5:45

    • @techathome
      @techathome  Місяць тому

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

    • @sanjusingha1714
      @sanjusingha1714 Місяць тому

      @techathome oh.. so that's the reason

  • @godgajet6763
    @godgajet6763 Рік тому

    help not working

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

  • @RarosKitchen
    @RarosKitchen 21 день тому

    OUTPUT SHOWS ONLY ZERO

    • @techathome
      @techathome  21 день тому

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

  • @nandikolasravani2498
    @nandikolasravani2498 Рік тому +3

    Code please

    • @techathome
      @techathome  Рік тому +1

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

  • @devchaudhary5892
    @devchaudhary5892 Рік тому

    I need ckt diagram

    • @techathome
      @techathome  Рік тому

      Download the compressed file in description box and extract it

  • @chandru6022
    @chandru6022 10 місяців тому

    is it working

  • @atiiveerjain510
    @atiiveerjain510 4 місяці тому

    Thanks

  • @adithyanvj2919
    @adithyanvj2919 2 роки тому

    subscribed :-)

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