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.
Arduino buzzer library

Or you can download it on Github

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

ezBuzzer(pin)
ezBuzzer(pin, buzzerType)
ezBuzzer(pin, buzzerType, activeLevel)

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

// Active buzzer, active HIGH (most common) ezBuzzer buzzer1(7); // Active buzzer, active LOW (Multi-Function Shield) ezBuzzer buzzer2(3, BUZZER_TYPE_ACTIVE, LOW); // Passive buzzer, active HIGH ezBuzzer buzzer3(9, BUZZER_TYPE_PASSIVE);

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

buzzer.stop();

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

buzzer.turnON();

Parameters

None

Return

None

turnOFF()

Description

This function turns the buzzer OFF. Same as stop().

Syntax

buzzer.turnOFF();

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

buzzer.beep(beepTime);
buzzer.beep(beepTime, delay);
buzzer.beep(beepTime, delay, frequency);

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

buzzer.playMelody(melody, noteDurations, length);

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

buzzer.setBuzzerType(type);

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

buzzer.setBeepFrequency(frequency);

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

buzzer.getState();

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

buzzer.loop();

Parameters

None

Return

None

The Best Arduino Starter Kit

See Also

※ OUR MESSAGES