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.
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
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?
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 ?
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.
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.
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
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
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
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
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.
♥
What is round strip distance
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
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?
This issue is because, signals from sensor are not reaching Arduino pins,
- Check wire
- Check connections
- Replace sensor
You can also check if you have mentioned the echo pin as INPUT or OUTPUT
Thank you
From Morocco
Welcome
Thanks bro 👍👊👌
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
Which code are you using?
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 ?
Check the continuity of jumper wires, sometimes they will be faulty.
@@techathome I removed it from breadboard and connected directly. Now it is working. thks
How can i use ultra sonic sensor without delay in the code
Which board are you using ?
Arduino Uno
is there any way using vb form to connect that?
I didn't get output what is the reason
What are you getting ?
make another 20 parts, very nice videos
Thank you. We will make more..
There are around 30 video as of now : ruclips.net/p/PL4B0LEKY-jrT_fjOGkX_NZ52ZHZf8s9S-
Can u give any video about obstacles avoiding robot car
As of now we have not done any video on this. Will try to make it.
Why have you divided by?
Due to 2 microsecond delay?
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.
what is pinmode for??
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
Do you all were able to here audio after 3:42
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.
@@techathome unable to hear after 3:42
There is no audio description still. Maybe you can edit or add text description to the video.
No
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
Can u send request on mail, I am not completely clear with your project requirements: deepakhd20@gmail.com
Iska value itna fluctuate knyu kar rha he?
Good explanation
Thank you
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
unplug and re-plug the board
What program do you use?
Arduino
The link for code is in description
how to increase the range of the ultrasonic sensor?
It is not possible to increase the range with software.
It depends on the specifications of ultrasonic sensor.
Code rar file is not opening
You have to extract it
Bro.
I am getting some values and o in between the values ..
If you are not inside its range,
It will give some random values
Cant find the google drive
Can u send mail request: deepakhd20@gmail.com
Sir sound is not coming
Check the connections and jumper wires.
I'm getting distance 0 on serial monitor everytime. What to do?
Check the pins connections of ultrasonic sensor and also replace the jumper wires
@@techathome ok I will try
@@Bigbrainie Did u solved it..
@@KnowPlants-5 yes
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 :-)
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
@@techathome Thanks bro, tutorial 17 helped me!
Welcome
@@techathome hello sir.
@@techathome code pahije hota ya vdo cha
the thing that shows the distance doesnt show up
I didn't get your question.
Your formula is wrong and is not working properly
is it working
yes
Please can you give me pins in written form file is not opening
Can u send mail: deepakhd20@gmail.com
can you explain the formula bro
why should we divede it by 9?
subscribed :-)
Thanks
Welcome
in second last line you haven't use semi colon ;
Oh, my be we have to update the code file
No voice : 3:51 -5:45
Due to copyright issues on background music, audio has been removed.
@techathome oh.. so that's the reason
help not working
Check the connections
Bro don't use distance command
OUTPUT SHOWS ONLY ZERO
Might be connection issue.
Check the jumper wires also, some wires will be faulty.
Bro some 800 , 1000 values are getting displayed how to avoid those values .?
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
I need ckt diagram
Download the compressed file in description box and extract it
Code please
I have provided the zip file link in description, download and extract.
There is no file at your google drive code section . Im reporting
I have provided zip file : drive.google.com/file/d/1nBpfZG43UC30DHSi5e6XjFo3WjFhL3s1/view
Download and extract it, you will get code and circuit.
@@techathomeit's showing that the file is unsupported!
Use winRaR software to extract on computer.