How to measure distance with an Arduino and Ultrasonic Sensor // HC-SR04
HTML-код
- Опубликовано: 8 авг 2024
- In this video I will show you a relatively accurate way to measure distance using and Arduino and an HC-SR04 ultrasonic sensor. I will walk you through the theory, setup, and coding using an Arduino.
The following are affiliate links for products that I use in this video. Purchases through these links will result in no increase in price for you, but may result in a small commission for me. These commissions will help me continue to make informative and entertaining videos such as this one. Thank you for your support and thanks for watching!
HC-SR04 Ultrasonic Sensor ebay.to/36ZAYva - Наука
For people who are lazy
const int triggerPin = 9;
const int echoPin = 10;
float pulse_width, distance;
void setup() {
// put your setup code here, to run once:
pinMode(triggerPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pulse_width = pulseIn(echoPin, HIGH);
distance = (pulse_width*.343)/2;
Serial.print("Distance = ");
Serial.println(distance);
Serial.println(" cm");
delay(500);
}
Thanks for watching.
Thanks Bro :)
absolute legend
Thanks, saved me another 5 min of typing lol
king shit
will this work on infrared sensor?
I haven't used infared sensors, so I couldn't tell you for sure. Sorry!
will this project work with arduino nano ??
Yes. Arduino UNO and NANO use the same pinouts, so the code switches over nicely.
Hello sir...i really appreciate your videos....I would like to know if I can contact you thru email? I would like to ask some advice regarding my project....from the 🇵🇭🇵🇭🇵🇭...thank you in advance...
ScienceFunBiz@gmail.com