Arduino UNO R4 - Ultrasonic Sensor - Piezo Buzzer
We'll learn how to control a piezo buzler using Arduino UNO R4 and an ultrasonic sensor.
- When the object is near the ultrasonic sensor, emit a sound.
- When the object is away from the ultrasonic sensor, stop the sound.
- When the object is near the ultrasonic sensor, play a song melody.
Hardware Preparation
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
Overview of Piezo Buzzer and Ultrasonic Sensor
Learn about piezo buzzers and ultrasonic sensors, including their pinouts, functions, and programming, in the tutorials provided.
This guide uses a buzzer that requires 3-5V, but you can also modify it for a 12V buzzer. Learn more about this in the Arduino UNO R4 - Buzzer tutorial.
Wiring Diagram
- The wiring diagram between Arduino UNO R4, ultrasonic sensor, and piezo buzzer

This image is created using Fritzing. Click to enlarge image
- The wiring diagram between Arduino UNO R4, ultrasonic sensor, and piezo buzzer module

This image is created using Fritzing. Click to enlarge image
See The best way to supply power to the Arduino Uno R4 and other components.
Arduino UNO R4 Code - Simple Sound
Detailed Instructions
Follow these instructions step by step:
- If this is your first time using the Arduino Uno R4 WiFi/Minima, refer to the tutorial on setting up the environment for Arduino Uno R4 WiFi/Minima in the Arduino IDE.
- Wire the components according to the provided diagram.
- Connect the Arduino Uno R4 board to your computer using a USB cable.
- Launch the Arduino IDE on your computer.
- Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port.
- Copy and paste the above code into the Arduino IDE
- Press the Upload button in the Arduino IDE to transfer the code to the Arduino UNO R4
- Wave your hand near the sensor
- Hear the sound from the piezo buzzer
Code Explanation
Look at the comment lines in the source code for a step-by-step explanation!
Arduino UNO R4 Code - Melody
Detailed Instructions
- Copy the code and open it using Arduino IDE.
- Make a file named pitches.h in Arduino IDE by:
- Clicking on the button below the serial monitor icon and selecting New Tab, or using the Ctrl+Shift+N keys.
- Provide the file name pitches.h and press the OK button.
- Copy the code below and paste it into the file named pitches.h you created.
- Press the Upload button in the Arduino IDE to send code to the Arduino UNO R4.
- Wave your hand near the sensor.
- Hear the melody from the piezo buzzer.
- The given code is for educational purposes. The ultrasonic sensor is very sensitive to noise. For practical uses of the ultrasonic sensor, you should apply noise filtering. Learn about how to filter noise for the ultrasonic sensor.


Code Explanation
Look at each explanation written in the comments within the source code!
※ NOTE THAT: