# arduino hc-sr04

com. In this tutorial, you will learn how to use the HC SR04 Ultrasonic Sensor.. It can measure distance from 2 centimetres up to 4 meters with a raging accuracy up to 3 mm.. The working principle of this module is quite simple.. It emits an ultra sound at 40000 Hz which travels through the air, and if there is an object or obstacle on it’s, it will bounce back to the module.. Considering the travel time and the speed of the sound, you can calculate the distance.. The module has 4 pins Ground, VCC Trig and a Echo The Ground and the VCC pins of the module needs to be connected to the Ground and 5 volts pin on the Arduino Board, respectively and Trig, and Echo pins to any digital inputoutput pin on the Arduino Board. In order to generate the ultrasound you’ll need to set the Trig on a HIGH state for 10 microseconds. That will send out an 8 cycle, sonic burst, which will travel at the speed of sound and it will be received in the Echo pin.. The Echo pin will output the time in microseconds the soundly traveled.. So, for example, if the object is 10 cm away from the sensor and the speed of sound is 340 meters per second or 0034 centimeters per microsecond. The sound wave will need to travel about 294 microseconds., But what you will get from the Echo pin will be doubled that number, because the sound wave needs to travel forward and bounce backward.

. So, in order to get the distance in cm, we need to multiplied the received travel time value from the echo pin by 0034 and divided by 2.. Okay, now let’s make the code. Here’s an example code for this tutorial.. First, we have to define the Trig and the Echo. In this case. There are the pins number 9 and 10 Arduino Board and they are named trigPin and echoPin.. Then we need a long variable. I will name it quotdurationquot for the travel time that we will get from the sensor and integer variable for the distance.. In the setup we have to define the trigPin as an output and the echoPin as an input and also start the serial communication for showing the results on the Serial Monitor. In the loop. First. We have to make sure that the trigPin is clear, so we will set that pin on a low state for just 2 microseconds. Now for generating the ultrasound wave. We have to set the Trig pin on a high state for 10 microseconds and we will do that with these three lines. Here. Now using the pulseIn function, we will read the travel time and put that value into the variable quotdurationquot. This function has two parameters. The first one is the name of the echo pin and for the second one we write either HIGH or LOW.. In our case, HIGH means that the pulsIn function we’ll wait for the pin to go HIGH, caused by the bounced sound wave and it will start timing.

. Then it will wait for the pin to go LOW when the sound wave will end, which will stop the timing.. At the end, the function will return the length of the pulse in microseconds.. Now, for getting the distance, we will multiply the duration by 0034 and divided by 2. As we explained this equasion previously.. At the end, we will print the value of the distance on the Serial Monitor What we will get as a result should look something like this. I also made an example: where a displayed the distance on an LCD. You can find the source code of this example from my website.. There is a link of it in the description of this video. Thanks for watching and for more tutorials visit.

# arduino hc-sr04 Social

official.arduino
2019-10-02T19:32:52+0000

👋 Want to bring Tony Stark-like gesture control to your projects? Learn how with the BLE-enabled MKR WiFi 1010 and Nano 33 BLE Sense boards using the ArduinoBLE library.
official.arduino
2019-10-02T19:08:01+0000

Arduin-yo ho ho! A fairground favorite, the pirate ship is a fun way to explore the oscillation of a pendulum. How much fun, you ask? Access our Science Kit Physics Lab preview and see for yourself: http://bit.ly/2oC6L0Q

🔬 Now that you’ve gotten your feet wet, dive into the kit and enjoy all nine experiments. Order yours today: http://bit.ly/2MnQ7fr

totibest
Thu Aug 27 02:23:58 +0000 2009

Sensor de aparcamiento con Arduino y el HC-SR04 https://t.co/BvLD7ndwQa vía @hwlibre

https://t.co/BvLD7ndwQa

10 adet Ultrasonik Modülü HC-SR04 Mesafe Arduino HC SR04 için Dönüştürücü Sensör Ölçüm

Tres sensores ultrasónicos

(Visited 141 times, 1 visits today)

## Comment (28)

1. How To Mechatronics says:

Please note, circuit diagram at 0:56 should be: 5v to VCC, GND to GND, D10 to Trig, D9 to Echo.

1. viswanath goturu says:

Could you please send full circuit diagram by connecting with LCD display

2. Saurabh Singh Jat says:

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

3. Graciela Osuna says:

How To Mechatronics hablas español verdad

4. Icy CiHo says:

motherf-

5. Icy CiHo says:

my ultrasonic sensor got fried, i think. lol

2. Jasiel Witherspoon says:

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

3. lukas lea says:

my arduino just spits out 14 on this code and my other bit of code i made someone help

4. Rahul Ghosh says:

Can the trig pins for 2 or more sensors can be in same digital pin?

5. Suren B. says:

The serial always displays: Distance: 0

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

7. Ranjeet Joshi says:

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

8. Lincoln Stanley says:

is there a way to play music through this and make a directional speaker?

1. Gonçalo Alegria says:

The sensors are piezoelectric plates, very tiny and they wouldn’t produce a nice sound. They are “tuned” to higher frequencies. Like a tweeter is tuned to a range of 2kHz to 20kHz a ultrasonic sensor is “tuned” to 25kHz, 40kHz, 125kHz (as an example). They are narrower in their range.

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

10. Frolov888 says:

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

11. Znkank says:

Perfect as always ty!

12. Mr. Dynamic says:

Can i use Arduino Nano

1. How To Mechatronics says:

Yes, you can use any Arduino board.

2. Mr. Dynamic says:

@How To Mechatronics circuit dig. For nano

13. Ryuuken24 says:

const int trigPin = PIN;
const int echoPin = PIN;
long duration;
int distanceCm;

void setup() {

Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

}

void loop()

{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distanceCm = duration * (0.034/2);
Serial.print(“Distance: “); // Prints string “Distance”
Serial.println(distanceCm); // Prints the distance value from the sensor
Serial.println(“cm”);
delay(2000);

14. RelaEarth says:

good one

Where’s the diagram if u connect a lcd

16. Nicko Manalansan says:

17. Huubgaming says:

I had to watch it without audio so didn’t here anything but does it have a minimum and/or maximum range? Thank you I am going to use this in a school project.

18. Zubin Jacob says:

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

19. sridevi elayaraja says:

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

20. mas bro says:

why this mechatronics, not electronics