Using Ultrasonic Distance Sensor HC-SR04 with Buzzer, LED and Arduino

preview_player
Показать описание

In this Arduino Tutorial we will learn how the HC-SR04 Ultrasonic Sensor works and how to use it with Buzzer and LED.

Required Hardware
Arduino Board:
Ultrasonic Sensor HC-SR04 :
Breadboard:
Jumper Wires:

Recommended Items:

Connections:
The HC-SR04 Ultrasonic Module has 4 pins, Ground, VCC, Trig and Echo. The Ground and the VCC pins of the module needs to be connected to the Ground and the 5 volts pins on the Arduino Board respectively and the trig and echo pins to any Digital I/O pin on the Arduino Board.

The HC-SR04 sensor attach to the Breadboard
The Sensor VCC connect to the Arduino Board +5V
The Sensor GND connect to the Arduino Board GND
The Sensor Trig connect to the Arduino Board Digital I/O 9
The Sensor Echo connect to the Arduino Board Digital I/O 10

Buzzer and LED
The Buzzer attach to the Breadboard
The Buzzer long leg (+) connect to the Arduino Board Digital 11
The Buzzer short leg (-) connect to the Arduino Board GND
The LED attach to the Breadboard
The Resistor connect to the LED long leg (+)
The Resistor other leg (from LED's long leg) connect to the Arduino Board Digital 13
The LED short leg (-) connect to the Arduino Board GND

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

i had this project as an exam in my informatics class, and it worked and i got straight A. thank you.

Kona-bona
Автор

3 years later and we are still building this. I set safetyDistance to 25. With only LED and no buzzer. Detects my cat coming in the room. Pretty cool - I guess!

mrbowlingalley
Автор

really good video man, code is great and when i tried it worked the first time, keep the great work bro

alvaronicolasgamarrapena
Автор

If anyone has any problems to get the code...just copy mine.


// defines pins numbers const int trigPin = 9; const int echoPin = 10; const int buzzer = 11; const int ledPin = 13; // defines variables long duration; int distance; int safetyDistance; void setup() { pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output pinMode(echoPin, INPUT); // Sets the echoPin as an Input pinMode(buzzer, OUTPUT); pinMode(ledPin, OUTPUT); Serial.begin(9600); // Starts the serial communication } void loop() { // Clears the trigPin digitalWrite(trigPin, LOW); delayMicroseconds(2); // Sets the trigPin on HIGH state for 10 micro seconds digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Reads the echoPin, returns the sound wave travel time in microseconds duration = pulseIn(echoPin, HIGH); // Calculating the distance distance= duration*0.034/2; safetyDistance = distance; if (safetyDistance <= 5){ digitalWrite(buzzer, HIGH); digitalWrite(ledPin, HIGH); } else{ digitalWrite(buzzer, LOW); digitalWrite(ledPin, LOW); } // Prints the distance on the Serial Monitor Serial.print("Distance: "); Serial.println(distance); }


Dont thank me, just subscribe to pewdiepie

AR-dyfu
Автор

What a great detailed guideline! I followed step by step and it work. I will use it for my final electrical engineering project. Thanks a lot!

lecongquyen
Автор

NICE!!! Saved me a thousand dlrs in school fees

muhammadfirdauz
Автор

thank you so much it worked with me.

your fan from middle east
اخوك من الشرق الاوسط

AVolt
Автор

OMG it works
thank you so much, please keep sharing projects like this.

shlb
Автор

// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
const int buzzer = 11;
const int ledPin = 13;

// defines variables
long duration;
int distance;
int safetyDistance;


void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(buzzer, OUTPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // Starts the serial communication
}


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

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance
distance= duration*0.034/2;

safetyDistance = distance;
if (safetyDistance <= 5){
  digitalWrite(buzzer, HIGH);
  digitalWrite(ledPin, HIGH);
}
else{
  digitalWrite(buzzer, LOW);
  digitalWrite(ledPin, LOW);
}

// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}

jwf
Автор

Thank you very much, i saw many vids but any of them din't work but your worked thank you very much.

theelectromaker
Автор

got the uno and this is a great tutorial. thanks.

Ganondalf_the_grey
Автор

Hello. There are already a lot of these examples on youtube. In your circuit the lights are either on or off. The sound either squeaks or it doesn't. I wish you had made a circuit where the intensity of sound and light increases as you get closer. :(

melihapa
Автор

wow...u did it ....u saved me....i used it in science project...

chaturbhujbiyani
Автор

It came in a kit, first thing that I acutally got to work with my UNO. Thanks for thie vid.

quentinb
Автор

hi, can you explain where did you get this value 0.034?

Edited:
i did a google search, its the speed of sound (343 m / s)

LhorKhan
Автор

Great project! Why is my buzzer clicking instead of buzzing though?

dylansworld
Автор

I lke the video because is short and easy to understand

mohdnaqibhaiqal
Автор

Why my led keeps on and the sensor is not working 😣

nurinqasrina
Автор

Thank you very much i am starting to learn arduino and thanks to you i can learn the hardware and then i will work on software

AdityaYadav-mkye
Автор

Thank you! This tutorial was very good and helped me a lot

marcelnalewajko
welcome to shbcf.ru