Buzzer

Difference between Active and Passive Buzzer? How to create your own music?

Introduction

The buzzer consists of a vibrating blade with the piezoelectric effect.

Piezoelectricity is a property possessed by certain minerals that deform when subjected to an electric field. It allows the transformation of electrical energy into vibration, enabling the production of simple notes and melodies.

Buzzers can be found in alarm devices, computers, timers, and more. There are two types of buzzers: Active and Passive buzzers.

What is the difference between an Active and Passive Buzzer?

There are some fundamental differences between Active and Passive buzzers:

Active Buzzer: It can generate sound when it receives a continuous voltage.

Passive Buzzer: Functions like a speaker, so an audio signal needs to be provided for it to produce sound.

To differentiate between the two, you can apply a continuous voltage to both buzzers. The one that emits sound will be the active buzzer. However, both buzzers operate similarly, which is why the programs we will propose will be the same for both types.

Electronic Symbol of the Buzzer

The electronic symbol of the buzzer is composed of two circles with a plus sign (+).

Library

To make the program work, you need to install the library dedicated to the buzzer. This library contains all the notes playable by the buzzer. The buzzer library is in .zip format, so to install it, click on “Sketch,” then “Include Library,” and “Add .Zip Library.”

If you encounter any problems installing the library, we recommend checking out our dedicated tutorial.

How to do without the library?

There are other types of possible notes that you can play. For this, you can use the library and input the note reference as in the melody list of the program or directly input the frequency.

By directly inputting the frequency, you no longer need to add the library to your code.

Buzzer Programming

As we have just seen, we can program the buzzer with or without the library.

A) Code with the library

Here is the function to program your buzzer with the library:

  • Pin: The pin to which your buzzer is connected.
  • Note: The note in frequency Hz.
  • Duration: The duration you want the note to be played.

You will find an example of using this function in the programs below.

B) Code without the library

Here is the function to program your buzzer without the library:

  • Buzzer Pin: The pin number where you have connected the buzzer.
  • HIGH/LOW: Used to turn on/off the buzzer.

You will find an example of using this function in the programs below.

Buzzer Circuits

Now, let’s see the circuits you need to create to use the programs. All codes work with these circuits.

A) Passive Buzzer

Here is the schema for the passive buzzer:

B) Active Buzzer

Here is the schema for the active buzzer:

To replicate the schema of the active buzzer on Fritzing, you need to install the active buzzer component. We recommend our Fritzing course if you have difficulty installing the component.

Code for the Buzzer

We will present two codes to make your buzzer work. The first one works with the library, and the second one works without it.

Here you have a sequence of notes from low to high using the library:

Here you have an alarm without the library:

How to play your own music?

As we have seen, we can play multiple notes with our buzzer. But it’s essential to find a good melody. For this, we recommend translating the notes from a sheet music you have chosen into frequency Hz.

For example, we have translated the song “Jingle Bells” as follows: