Ultrasonic Sensor HC-SR04 and Arduino Tutorial

preview_player
Показать описание
Please note, circuit diagram at 0:56 should be: 5v to VCC, GND to GND, D10 to Trig, D9 to Echo.

In this Arduino Tutorial you will learn how to use the HC-SR04 Ultrasonic sensor. It can measure distance from 2 cm to 4 meters with a ranging accuracy up to 3mm. The working principle of this module is quite simple.

Like my page on Facebook:

Add me on Google+:

Рекомендации по теме
Комментарии
Автор

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

HowToMechatronics
Автор

This video is incredible, code is so much easier to understand with the calculations. What a guy!!

connortierney
Автор

The code
```
const int trigPin = 9;
const int echoPin = 10;

long duration;
int distance;

void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}

void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);
distance = duration*0.034/2;

Serial.print("Distance: ");
Serial.print(distance);
Serial.print("\n");
}
```

aumpauskar
Автор

The code works, but is not the best to combine with other stuff. It is really advisable to connect the TRIG to pin 3 and let an interrupt do the measuring in the background, so you don't have puseIn halting your code completely while sensing.

lovemadeinjapan
Автор

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

kingshahzad
Автор

Maybe just one thing to add up, the moment you set trig pin to HIGH, echo pin will be switched to HIGH as well, until it detects the back wave. That's why the period of echo pin input on HIGH mode equals to the time ultrasonic wave travels.

gongye
Автор

You have echo and vcc in series. The red wire should be I the right most pin of your diagram.

quaxiscorporationforresear
Автор

The code works fine, however the circuitry you have provided in the video is connected wrong.Other than that thank you for the explanation, greatly appreciated!

Blind_Beetle
Автор

Yes follow the instructions on the site, not here. There you can copy and paste the code too.

symbianwater
Автор

Where's the diagram if u connect a lcd

mnusne
Автор

Cool project!. Besides arduino, I have also tried to use 2 esp8266, you can get sensors and displays without cables

ndoWare
Автор

you can fit the sensor on the arduino without a breadboard: just use pin 11 as vcc

Blinkation
Автор

I broke my ultrasonic sensor cause of your diagram 🙃

miguelmontalban
Автор

Great job SIR, for those who ask about LCD connection please see the code (2.3.4.5.6.7 on arduino pins = 4.6.11.12.13.14 on LCD) thanks.

meddiys
Автор

very helpful
thank you my bro
keep it real
love from india and battir

omarqattosh
Автор

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

Frolov
Автор

Wonderful. Thank you so much. Very easy to follow and the maths was explained very simply.

northshorepx
Автор

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

saurabhsinghjat
Автор

Thank you! I'll be referencing this in my next video. Please be aware that your fritzing diagram is incorrect.

BeckyMarshallDesign
Автор

Thanks for sharing the code, but I was hopping to learn the physical setup of the US sensor as well

IamMrWu