Skip to main content

2.4 Buzzer Beep

placeholder
Example demonstration video

The goal of this example is to introduce you to sound generation using a passive buzzer and the tone() function.
A passive buzzer can produce sounds of different frequencies, meaning you can play tones, melodies, or sound effects.
In this example, your Soldered NULA MINI board will play a short melody when powered on.

In this documentation you will learn:

  • How to connect and control a passive buzzer.
  • How to generate tones of different frequencies using tone().
  • How to stop the sound using noTone().
  • How to play a simple melody using arrays.

Hardware required

  • 1× Soldered NULA MINI board
  • 1× Passive buzzer
  • 1× Breadboard
  • Jumper wires
  • USB-C cable
placeholder
All components needed for this example

Putting the components together

1. Connect the buzzer

Buzzer PinNULA MINI Pin
Positive (+)IO5
Negative (–)GND
ℹ️

Make sure you’re using a passive buzzer, not an active one.
A passive buzzer can produce a range of frequencies, while an active buzzer can only beep at one fixed pitch.

placeholder
Passive buzzer connection to IO5 and GND

Understanding sound and frequency

Sound is produced by vibrations in the air.
When we rapidly switch a digital pin between HIGH and LOW (creating a square wave) at a certain frequency, the buzzer vibrates and produces a tone.

NoteFrequency (Hz)
C4262
D4294
E4330
F4349
G4392
A4440
B4494
C5523
ℹ️

Higher frequency leads to higher pitch while lower frequency leads to a deeper tone.


How it works

  • Each note's frequency is stored in the melody[] array.
  • The corresponding duration is stored in the noteDuration[] array.
  • The loop plays each note in order using tone().
  • At the end of the melody, buzzer is silenced using noTone().

Code

/*
This is a variable to which we assign the number of the pin that we connected the buzzer to.
In this example, we’ll use IO5 as the output pin. If you want, you can use any other digital pin that has ADC capabilities.
*/
const int BUZZER_PIN = 5;

/*
We will use two arrays — one for note frequencies (in Hertz) and one for note durations.
These define a short melody that the buzzer will play.
*/
int melody[] = { 262, 294, 330, 349, 392, 440, 494, 523 }; // C4 to C5
int noteDuration[] = { 4, 4, 4, 4, 4, 4, 4, 2 }; // Quarter notes (last one is half note)

void setup() {
/*
The tone() function generates a square wave of a given frequency (and duration)
on a specific pin, which makes the buzzer produce sound.
*/
pinMode(BUZZER_PIN, OUTPUT);
Serial.begin(115200);
Serial.println("Playing melody...");

// Loop through the melody and play each note
for (int i = 0; i < 8; i++) {
int duration = 1000 / noteDuration[i]; // Convert duration type to milliseconds
tone(BUZZER_PIN, melody[i], duration); // Play the note
delay(duration * 1.3); // Wait a bit before the next note
}

// Stop tone when done
noTone(BUZZER_PIN);
Serial.println("Melody finished!");
}

void loop() {
// Nothing happens here — melody plays once in setup()
}

Full example

placeholder
Full example video

Check out the full example code on the link below:

2.4_Buzzer_Beep.ino

Example that shows how to use the tone() function to play a simple melody on a passive buzzer connected to IO5.