Home All Projects
Easy

Ultrasonic sensor with Arduino - Complete Guide

Learn how to use the ultrasonic sensor (HC-SR04) with Arduino. This tutorial will teach you what it is, how to assemble the sensor on Arduino, and, at the end, you'll implement it into an alarm system project.

Ultrasonic sensor with Arduino - Complete Guide

Demo Video

Video

▶ Click to play project demo

341,913 views
56 likes

Components, Tools and Machines

1x Arduino uno Board
-
1x Ultrasonic Sensor - HC-SR04
-
1x Active Buzzer 5V (HXD)
-
1x Breadboard 100x160
-
1x jumper wires for arduino
-

Apps and platforms

1x Arduino IDE
Official Site

Project description

I've recently posted a video on YouTube explaining everything you can read in this article. If you are interesting at watching it, here is the video.

The ultrasonic sensor is a device that can measure distances using sound waves . It works in a similar way than bats and dolphins - by emitting sound waves and listening them bound back .

The sensor consists of two primary components: a transmitter and a receiver . The transmitter is responsible for emitting a high-frequency sound. In essence, ultrasonic refers to frequencies beyond the range of the human hearing - so something higher than 20kHz.

When the sound wave hits an object, it bounces back like echo. This returning wave is detected by the receiver. The sensor will use the micro-controller (Arduino) internal clock to find out how much it took for the sound to bounce back. This small clock turns on when a high-frequency wave is emitted and turns off when its echo is detected.

Using code, we can write a program that will store the timing between those two events into a variable. Then, we can use this information to calculate the distance between the sensor and the object.

You may be thinking: how can we find the distance if all we know is the timing?

Well, as you know, velocity is distance divided by time. Based on this equation, if we multiply the velocity by the time, we'll find the distance . I told you the sensor emits sound waves, so the velocity we need is the speed of sound in air (340 m/s).

Velocity = Distance / Time
Distance = Velocity * Time

But if we multiply this speed with the timing we found, we'll discover a value that's twice the real distance. That's happens because the sound hit the object and came back, in other words, it traveled the same path twice. Then to find the real distance, multiply the speed of sound with the timing and divide the result by two.

Distance = (Velocity * Time) / 2

Project

In this article, I will show you how to build an alarm system using Arduino and the ultrasonic sensor. If you don't have all components or would like to test before assembling anything, I created a simulation of this alarm system on Tinkercad . You can run it directly on your browser by clicking here .

Moving on, for this project, you'll need: an Arduino board, a breadboard, a bunch of jumper wires, a buzzer,and an ultrasonic sensor.

Assembly

Firstly, attach the ultrasonic sensor and the buzzer on the breadboard. Then connect the VCC and GND pins on Arduino 5V and ground ports.

After that connect the trigger pin to port 9, the echo pin to port 10, and the buzzer to port 8. Also attach the buzzer GND to Arduino GND.

At the end, upload the alarm-system-arduino.ino file that I provided on this article, and now our alarm system is finished.

Code explanation

At first, we defined the constants and variables we'll need. Then, at the setup function, we configured some important aspects of the code - like defining the echo pin as INPUT and the trigger and buzzer pins as OUTPUT. We also started the serial communication - and it'll be important to visualize the distances being measured.

At the end, there's the loop function. It starts by turning off the emitter and then activating it for 10 milliseconds before turning it off again. This piece of code will generate the wave sound that will be bounced back by the near objects.

At line 28 we find how much time the sensor took to detect the echo. This information is crucial for calculating the distance at line 29. In this case, we found the distance in centimeters. After that, from lines 30 to 35 we print the distance.

The last part of the code is an if-statement that turns on the buzzer when the object is at 50 centimeters or closer from the sensor. In case this statement is false, the buzzer turns down.

And now it's time to discover whether it'll work. So upload the code to your Arduino board and have fun.

Code

🔒 Unlock Code

Support to get the Source Code for this project

Project Reference Code: ultrasonic-sensor-with-arduino-complete-guide-284faf-en
79 THB
PromptPay QR Code

Verified user reviews

รีวิวและความคิดเห็นจากผู้ใช้จริง

ล็อกอินด้วยบัญชีบนเว็บนี้แล้วให้คะแนนหรือคอมเมนต์ได้เลย ระบบเก็บผ่าน Supabase ไม่ต้องใช้ GitHub แล้ว

กำลังโหลดรีวิว...