Introduction: Arduino - Ultrasonic Sensor HC-SR04 With Buzzer


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

Step 1: Hardware Required

Picture of Hardware Required

- Arduino Board

- Ultrasonic Sensor HC-SR04

- Buzzer


- 220ohm Resistor

- Breadboard and Jump Wires

Step 2: Connections

Picture of Connections

HC-SR04 Sensor

- 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

Step 3: Code

Picture of Code

- First you have to define the Trig and Echo pins. In this case they are the pins number 9 and 10 on the Arduino Board and they are named trigPin and echoPin. Then you need a Long variable, named “duration” for the travel time that you will get from the sensor and an integer variable for the distance.

- In the setup you 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.

- If the object is 10 cm away from the sensor, and the speed of the sound is 340 m/s or 0.034 cm/µs the sound wave will need to travel about 294 u seconds. But what you will get from the Echo pin will be double 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 multiply the received travel time value from the echo pin by 0.034 and divide it by 2.

Get the Code


doladola made it! (author)2017-08-11

it was easy for me to do thank you mert for sharing your knowledge with us

RAJESH-MrX (author)2017-07-02


sunlitho (author)2017-05-13

I cannot find the code. Looked everywhere.

ellenflorence (author)2017-04-01

Hi, I am fairly new at working with arduinos. How would I code/connect more LED pins to this setup?

Your help would be greatly appreciated!


AnkerP made it! (author)2016-12-27

I'm a beginner with arduino and this was a pretty easy first project. I really liked it! Thanks for the tutorial.

sleech1 made it! (author)2016-12-22

I made this in an attempt to keep the dog off the counter. Seems to be working!

MertArduino (author)sleech12016-12-22

Fantastic! I liked it. Good idea. :)

harsh-12 (author)2016-12-18

Nice instructable... :)
Please have a look at the below link as well using aurduino

About This Instructable




Bio: For more project and tutorial visit my YouTube channel Mert Arduino and Raspberry Pi
More by MertArduino:XY Plotter Drawing Robot | Arduino | PolargraphArduino - Balance - Balancing Robot | How to Make?Arduino - MPU6050 GY521 - 6 Axis Accelerometer + Gyro (3D Simulation With Processing)
Add instructable to: