ezBuzzer Library - Melody Example
This example uses a piezo buzzer:
- Plays a melody once on background each time a button is pressed
- Stops playing a melody when another button is pressed
- Without using delay() function, this is a non-blocking example
Hardware Required
Additionally, some links direct to products from our own brand, DIYables .
About ezBuzzer Library
- Refer to Arduino - ezBuzzer Library Reference
Wiring Diagram

Arduino Code
Quick Steps
- Install ezBuzzer library. See How To
- Connect Arduino to PC via USB cable
- Open Arduino IDE, select the right board and port
- On Arduino IDE, Go to File Examples ezBuzzer Melody example
- Click Upload button on Arduino IDE to upload code to Arduino

- Press and release the START button, You will hear "Jingle Bells" song
- Press and release the STOP button, The song is stopped immediately
If you do not press STOP button, the song will be played to the end of song.
Code Explanation
Read the line-by-line explanation in comment lines of source code!
※ NOTE THAT:
Important: Buzzer Configuration
This example uses the following buzzer configuration:
- Buzzer Type: BUZZER_TYPE_PASSIVE (passive buzzer for melody playback)
- Active Level: HIGH (buzzer sounds when pin is HIGH)
Note: Melody playback requires a passive buzzer because it needs to generate different frequencies. If you only have an active buzzer, you can only use the beep functionality (see Beep example).
Please check your buzzer specifications and adjust the constructor accordingly:
- If you have an active LOW passive buzzer, use: ezBuzzer buzzer(BUZZER_PIN, BUZZER_TYPE_PASSIVE, LOW);
※ NOTE THAT:
The above example code demonstrates how to use the ezBuzzer library to make a beep on Piezo Buzzer each time button is pressed. In practice, You may need to debounce for the button. Debouncing for the button is not easy for beginners. Fortunately, thanks to the ezButton library, We can do it easily.