Arduino - Buzzer Library
When using a piezo buzzer, beginners usually run into the trouble that buzzer code blocks other Arduino code because of delay() function.
The ezBuzzer (easy buzzer) library is designed to solve that problems and make it easy to use for not only beginners but also experienced users. It is created by ArduinoGetStarted.com and enhanced with cross-platform support.
Library Features
- Supports both active and passive buzzers
- Supports active HIGH and active LOW buzzers
- Supports generating a beep with customizable frequency
- Supports playing a melody
- Supports manual ON/OFF control
- Supports stopping playing a melody
- All functions are non-blocking (no delay() function)
- Cross-platform compatible (all Arduino architectures)
※ NOTE THAT:
If this library is useful for your work, you can say thanks to us by buying us a coffee: PayPal.Me/arduinogetstarted
How To Install Library
- Navigate to the Libraries icon on the left bar of the Arduino IDE.
- Search “ezBuzzer”, then find the buzzer library by ArduinoGetStarted
- Click Install button to install ezBuzzer library.

Or you can download it on Github
Examples
Functions
- ezBuzzer(pin, buzzerType, activeLevel)
- stop()
- turnON()
- turnOFF()
- beep(time)
- beep(time, delay)
- beep(time, delay, frequency)
- playMelody(melody, noteDurations, length)
- setBuzzerType(type)
- setBeepFrequency(frequency)
- getState()
- loop()
References
ezBuzzer()
Description
This function creates a new instance of the ezBuzzer class that represents a particular buzzer attached to your Arduino board.
Syntax
Parameters
- pin: int - Arduino's pin that connects to the buzzer.
- buzzerType: int - Type of buzzer: BUZZER_TYPE_ACTIVE (default) or BUZZER_TYPE_PASSIVE.
- activeLevel: int - Active level: HIGH (default) or LOW.
Return
A new instance of the ezBuzzer class.
Example
stop()
Description
This function stops playing beep or melody. It will set the state of buzzer to BUZZER_IDLE and turn off the buzzer.
Syntax
Parameters
None
Return
None
turnON()
Description
This function turns the buzzer ON continuously. For active buzzers, it sets the pin to active level. For passive buzzers, it starts a tone at the default beep frequency.
Syntax
Parameters
None
Return
None
turnOFF()
Description
This function turns the buzzer OFF. Same as stop().
Syntax
Parameters
None
Return
None
beep()
Description
This function generates a beep. The beep is run on background without blocking other Arduino code. When a beep is ended, the function will set the state of buzzer to BUZZER_IDLE.
Syntax
Parameters
- beepTime: unsigned long - the time buzzer beeps (in milliseconds).
- delay: unsigned long - the time delay before beeping (in milliseconds).
- frequency: int - the frequency for passive buzzer beep (in Hz).
Return
None
playMelody()
Description
This function plays a melody. The melody is run on background without blocking other Arduino code. When a melody is ended, the function will set the state of buzzer to BUZZER_IDLE. Best used with passive buzzers.
Syntax
Parameters
- melody: int melody[] - An int array stores note's information.
- noteDurations: int noteDurations[] - An int array stores note's duration.
- length: int - The number of notes stores in melody[] array.
Return
None
setBuzzerType()
Description
This function sets the buzzer type after initialization.
Syntax
Parameters
- type: int - BUZZER_TYPE_ACTIVE or BUZZER_TYPE_PASSIVE.
Return
None
setBeepFrequency()
Description
This function sets the default beep frequency for passive buzzers.
Syntax
Parameters
- frequency: int - The frequency in Hz (e.g., 2000 for 2kHz).
Return
None
getState()
Description
This function returns the state of the buzzer.
Syntax
Parameters
None
Return
One of the following states of buzzer:
- BUZZER_IDLE
- BUZZER_BEEP_DELAY
- BUZZER_BEEPING
- BUZZER_MELODY
loop()
Description
This function does play beep, melody on background. It MUST be called in the main loop() function.
Syntax
Parameters
None
Return
None