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

  Рет қаралды 138,071

Tech at Home

Tech at Home

Күн бұрын

Пікірлер: 95
@sanjusingha1714
@sanjusingha1714 2 ай бұрын
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 Ай бұрын
What is round strip distance
@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
@meskhetian642
@meskhetian642 2 жыл бұрын
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 жыл бұрын
This issue is because, signals from sensor are not reaching Arduino pins, - Check wire - Check connections - Replace sensor
@VENU_GAMING
@VENU_GAMING 13 күн бұрын
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
@anithasshenoy6662
@anithasshenoy6662 10 ай бұрын
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 ай бұрын
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
@yamanjain9346
@yamanjain9346 Жыл бұрын
Do you all were able to here audio after 3:42
@techathome
@techathome Жыл бұрын
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 Жыл бұрын
@@techathome unable to hear after 3:42
@BromideBride
@BromideBride 4 ай бұрын
There is no audio description still. Maybe you can edit or add text description to the video.
@SR-ok3su
@SR-ok3su Ай бұрын
No
@amrabdlkadersabry5649
@amrabdlkadersabry5649 3 ай бұрын
How can i use ultra sonic sensor without delay in the code
@kapil409
@kapil409 Жыл бұрын
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.
@ramybrahim3188
@ramybrahim3188 Жыл бұрын
Thank you From Morocco
@techathome
@techathome Жыл бұрын
Welcome
@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.
@itsSatvik59
@itsSatvik59 Жыл бұрын
make another 20 parts, very nice videos
@techathome
@techathome Жыл бұрын
Thank you. We will make more.. There are around 30 video as of now : kzbin.info/aero/PL4B0LEKY-jrT_fjOGkX_NZ52ZHZf8s9S-
@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
@kirans9395
@kirans9395 Жыл бұрын
Why have you divided by? Due to 2 microsecond delay?
@krk101
@krk101 8 ай бұрын
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.
@khannreactions3311
@khannreactions3311 2 жыл бұрын
is there any way using vb form to connect that?
@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
@Bigbrainie
@Bigbrainie 3 жыл бұрын
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 жыл бұрын
We have done tutorial on that, you can refer to below two video links: Lcd1 - kzbin.info/www/bejne/nKqvYptsZ7Z7gNU Lcd2 - kzbin.info/www/bejne/gqrYqpWZpLN4qdk
@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
@yourevolution7850
@yourevolution7850 9 ай бұрын
Iska value itna fluctuate knyu kar rha he?
@shivufpv6248
@shivufpv6248 4 жыл бұрын
Thanks bro 👍👊👌
@mrwildmouth5876
@mrwildmouth5876 Жыл бұрын
Which board are you using ?
@techathome
@techathome Жыл бұрын
Arduino Uno
@electro2585
@electro2585 Жыл бұрын
Your formula is wrong and is not working properly
@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
@hahahahahaha664
@hahahahahaha664 7 ай бұрын
what is pinmode for??
@techathome
@techathome 7 ай бұрын
This is used to set the mode of the pin. Either you want to use particular pin as input or output. kzbin.info/www/bejne/e3uoq2mqarijg9k
@muninggamer7621
@muninggamer7621 11 ай бұрын
how to increase the range of the ultrasonic sensor?
@techathome
@techathome 11 ай бұрын
It is not possible to increase the range with software. It depends on the specifications of ultrasonic sensor.
@KnowPlants-5
@KnowPlants-5 2 жыл бұрын
Bro. I am getting some values and o in between the values ..
@techathome
@techathome 2 жыл бұрын
If you are not inside its range, It will give some random values
@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
@ushakiran8285
@ushakiran8285 Жыл бұрын
can you explain the formula bro why should we divede it by 9?
@sreekanthneelam5268
@sreekanthneelam5268 2 ай бұрын
Code rar file is not opening
@techathome
@techathome 2 ай бұрын
You have to extract it
@ravikumarkankanala4092
@ravikumarkankanala4092 4 ай бұрын
Sir sound is not coming
@techathome
@techathome 4 ай бұрын
Check the connections and jumper wires.
@TestCreate-v6g
@TestCreate-v6g 6 ай бұрын
Cant find the google drive
@techathome
@techathome 6 ай бұрын
Can u send mail request: deepakhd20@gmail.com
@bozakardude
@bozakardude Жыл бұрын
the thing that shows the distance doesnt show up
@techathome
@techathome Жыл бұрын
I didn't get your question.
@shyamsundar7784
@shyamsundar7784 Жыл бұрын
Good explanation
@techathome
@techathome Жыл бұрын
Thank you
@pizafox12
@pizafox12 6 ай бұрын
What program do you use?
@techathome
@techathome 6 ай бұрын
Arduino The link for code is in description
@narullah69
@narullah69 2 жыл бұрын
in second last line you haven't use semi colon ;
@techathome
@techathome 2 жыл бұрын
Oh, my be we have to update the code file
@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
@RarosKitchen
@RarosKitchen Ай бұрын
OUTPUT SHOWS ONLY ZERO
@techathome
@techathome Ай бұрын
Might be connection issue. Check the jumper wires also, some wires will be faulty.
@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
@techathome
@techathome Жыл бұрын
Check the connections
@Millionaire_001
@Millionaire_001 Жыл бұрын
Bro don't use distance command
@nandikolasravani2498
@nandikolasravani2498 Жыл бұрын
Code please
@techathome
@techathome Жыл бұрын
I have provided the zip file link in description, download and extract.
@atiiveerjain510
@atiiveerjain510 4 ай бұрын
Thanks
@techathome
@techathome 4 ай бұрын
Welcome
@chandru6022
@chandru6022 10 ай бұрын
is it working
@techathome
@techathome 10 ай бұрын
yes
@devchaudhary5892
@devchaudhary5892 Жыл бұрын
I need ckt diagram
@techathome
@techathome Жыл бұрын
Download the compressed file in description box and extract it
@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.
IL'HAN - Qalqam | Official Music Video
03:17
Ilhan Ihsanov
Рет қаралды 700 М.
REAL or FAKE? #beatbox #tiktok
01:03
BeatboxJCOP
Рет қаралды 18 МЛН
UFC 310 : Рахмонов VS Мачадо Гэрри
05:00
Setanta Sports UFC
Рет қаралды 1,2 МЛН
HC-SR04 Ultrasonic Distance Sensor and Arduino (Lesson #9)
5:28
Science Buddies
Рет қаралды 153 М.
Ultrasonic Sensor HC-SR04 and Arduino Tutorial
5:16
How To Mechatronics
Рет қаралды 1,4 МЛН
How To Use Ultrasonic Sensors with Arduino! + Project Idea!
4:09
How to Connect Ultrasonic Sensor HCSR04 with Arduino || HINDI
7:24
ENGINEER THIS ELECTRONICS
Рет қаралды 23 М.
IL'HAN - Qalqam | Official Music Video
03:17
Ilhan Ihsanov
Рет қаралды 700 М.