Arduino Buzzer Tutorial – Playing Tones and Melodies

The Arduino Buzzer is a simple audio output device that produces tones, beeps, and alerts. It’s commonly used in alarms, notifications, timers, and interactive electronics to add sound feedback.

In this tutorial, you’ll learn how to connect a buzzer to Arduino, generate tones using code, and create sound patterns. By the end, you’ll be able to use the Arduino Buzzer in projects like alarms, music players, and sound-based alerts.

How the Buzzer Works

There are two types of buzzers: active buzzers and passive buzzers. Most active buzzers work at a voltage range of 3.3V – 5V and generate sound at a fixed frequency. You can increase or decrease the volume, but you cannot change the sound frequency.

On the other hand, there is the passive buzzer. Passive buzzers can generate sound over a wide frequency range, from ~31 Hz to ~20 kHz. They require a fixed-frequency signal to produce a specific tone. This tone can be changed by adjusting the input signal frequency. We can use a PWM signal or the Arduino tone() function to generate this type of input signal and create different tones.

Ads

Arduino Buzzer Circuit

A buzzer has two legs. The positive side is marked with a ⨁ sign. Connect the positive pin to any Arduino digital pin, and the ground pin to the Arduino ground.

If you have an Arduino buzzer module, it has three pins: positive, negative, and signal. In that case, connect the positive and negative pins to the Arduino 5V and ground pins, respectively. Then connect the signal pin to any Arduino digital pin.

Arduino Buzzer Circuit

Arduino Buzzer Code

A buzzer can be played in many ways. You can generate a simple bip-bip sound using the digitalWrite() function.

The code below uses the digitalWrite() function to turn the buzzer on for 500 ms and then turn it off for 50 ms, which generates a bip-bip sound.

const int buzzerPin = 9;
void setup(){ 
  pinMode(buzzerPin, OUTPUT);
}

void loop(){
  digitalWrite(buzzerPin, HIGH);
  delay(500);
  digitalWrite(buzzerPin, LOW);
  delay(500);
}

We can control the intensity of the tone using a PWM signal. analogWrite(pin, 127) will turn the buzzer on at half intensity.

Note: The analogWrite() function will only work with Arduino PWM pins. You can identify these pins by the ‘~’ marking on the board.

Ads
const int buzzerPin = 9;
void setup(){ 
  pinMode(buzzerPin, OUTPUT);
}

void loop(){
  analogWrite(buzzerPin, 127);
  delay(500);
  analogWrite(buzzerPin,0);
  delay(500);
}

Upload the code to the Arduino and listen to how it sounds.

Buzzer with tone() function

It becomes more interesting when we use the tone() function with a buzzer. We get more control over the buzzer sound when using the tone() function.

The basic syntax for the tone() and noTone() functions is given below.

tone(pin, frequency);  // or
tone(pin, frequency, duration);

noTone(pin);

Parameters

  • pin → The Arduino pin on which we generate the tone.
  • frequency → The frequency of the tone in hertz (Hz).
  • duration (optional) → How long the tone should play, in milliseconds. If omitted, the tone continues until noTone(pin) is called.

A call to the tone() function generates a tone on the specified pin, and a call to the noTone() function stops that tone.

Arduino Code for Passive Buzzer

A simple sketch using the tone() function is given below.

const int buzzerPin = 9;
void setup(){ 
  pinMode(buzzerPin, OUTPUT);
}
void loop(){
  tone(buzzerPin, 500, 500);
  delay(1000);
}

Upload the code to the Arduino, and you will hear a 500 Hertz tone.

Some useful Buzzer Sounds

Here are a few example buzzer codes that generate useful sounds.

Alarm / Warning Sound

const int buzzerPin = 9;

void setup() {
  // Nothing to initialize
}

void loop() {
  // Simple repetitive beep pattern
  
  for (int i = 0; i < 2; i++) {
    tone(buzzerPin, 1000);  // 1000 Hz tone
    delay(300);             // ON for 500 ms
    noTone(buzzerPin);
    delay(300);             // OFF for 200 ms
  }

  // Pause before repeating the alarm
  delay(500);
}

Aubulance siren

const int buzzerPin = 9;

void setup() {
  // Nothing to initialize
}

void loop() {
  // Sweep up
  for (int freq = 600; freq <= 1200; freq += 10) {
    tone(buzzerPin, freq);
    delay(5);
  }

  // Sweep down
  for (int freq = 1200; freq >= 600; freq -= 10) {
    tone(buzzerPin, freq);
    delay(5);
  }

  // Short pause before next siren cycle
  delay(200);
}

Police siren

const int buzzerPin = 9;

void setup() {
  pinMode(buzzerPin, OUTPUT);
}

void loop() {
  // Sweep up
  for (int freq = 1000; freq <= 2000; freq += 10) {
    tone(buzzerPin, freq);
    delay(5);
  }

  // Sweep down
  for (int freq = 2000; freq >= 1000; freq -= 10) {
    tone(buzzerPin, freq);
    delay(5);
  }
}

Play Melodies using the Buzzer

The code below will play the melody of the “Happy Birthday to You” song.

int buzzerPin = 9;
int numOfNotes = 28;  // number of notes

char notes[] = "GGAGcB GGAGdc GGxecBA yyecdc";
int beats[] = { 2, 2, 8, 8, 8, 16, 1, 2, 2, 8, 8, 8, 16, 1,
                2, 2, 8, 8, 8, 8, 16, 1, 2, 2, 8, 8, 8, 16 };
int tempo = 150;

void playTone(int freqVal, int dur) {
  for (long i = 0; i < dur * 1000L; i += freqVal * 2) {
    digitalWrite(buzzerPin, HIGH);
    delayMicroseconds(freqVal);
    digitalWrite(buzzerPin, LOW);
    delayMicroseconds(freqVal);
  }
}

void playNote(char note, int dur) {
  char noteMap[] = { 'C', 'D', 'E', 'F', 'G', 'A', 'B',
                     'c', 'd', 'e', 'f', 'g', 'a', 'b',
                     'x', 'y' };
  int freq[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014,
                 956, 834, 765, 593, 468, 346, 224,
                 655, 715 };
  int spd = 5;

  for (int i = 0; i < 17; i++) {
    if (noteMap[i] == note) {
      int noteDur = dur / spd;
      playTone(freq[i], noteDur);
    }
  }
}

void setup() {
  pinMode(buzzerPin, OUTPUT);
}

void loop() {
  for (int i = 0; i < numOfNotes; i++) {
    if (notes[i] == ' ') {
      delay(beats[i] * tempo);  // rest
    } else {
      playNote(notes[i], beats[i] * tempo);
    }
    delay(tempo);  // short pause between notes
  }
}

Nokia Tone

/* 
  Nokia Tune
  Connect a piezo buzzer or speaker to pin 9 or select a new pin.
  More songs available at https://github.com/robsoncouto/arduino-songs                                            
                                              
                                              Robson Couto, 2019
*/
#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 4978
#define REST      0


// change this to make the song slower or faster
int tempo = 180;

// change this to whichever pin you want to use
int buzzer = 9;

// notes of the moledy followed by the duration.
// a 4 means a quarter note, 8 an eighteenth , 16 sixteenth, so on
// !!negative numbers are used to represent dotted notes,
// so -4 means a dotted quarter note, that is, a quarter plus an eighteenth!!
int melody[] = {

  // Nokia Ringtone 
  // Score available at https://musescore.com/user/29944637/scores/5266155
  
  NOTE_E5, 8, NOTE_D5, 8, NOTE_FS4, 4, NOTE_GS4, 4, 
  NOTE_CS5, 8, NOTE_B4, 8, NOTE_D4, 4, NOTE_E4, 4, 
  NOTE_B4, 8, NOTE_A4, 8, NOTE_CS4, 4, NOTE_E4, 4,
  NOTE_A4, 2, 
};

// sizeof gives the number of bytes, each int value is composed of two bytes (16 bits)
// there are two values per note (pitch and duration), so for each note there are four bytes
int notes = sizeof(melody) / sizeof(melody[0]) / 2;

// this calculates the duration of a whole note in ms
int wholenote = (60000 * 4) / tempo;

int divider = 0, noteDuration = 0;

void setup() {
  // iterate over the notes of the melody.
  // Remember, the array is twice the number of notes (notes + durations)
  for (int thisNote = 0; thisNote < notes * 2; thisNote = thisNote + 2) {

    // calculates the duration of each note
    divider = melody[thisNote + 1];
    if (divider > 0) {
      // regular note, just proceed
      noteDuration = (wholenote) / divider;
    } else if (divider < 0) {
      // dotted notes are represented with negative durations!!
      noteDuration = (wholenote) / abs(divider);
      noteDuration *= 1.5; // increases the duration in half for dotted notes
    }

    // we only play the note for 90% of the duration, leaving 10% as a pause
    tone(buzzer, melody[thisNote], noteDuration * 0.9);

    // Wait for the specief duration before playing the next note.
    delay(noteDuration);

    // stop the waveform generation before the next note.
    noTone(buzzer);
  }
}

void loop() {
  // no need to repeat the melody.
}

You can find more melodies at the links below.

github.com/hibit-dev/buzzer
github.com/AbhishekGhosh/Arduino-Buzzer-Tone-Codes
github.com/xitanggg/-Pirates-of-the-Caribbean-Theme-Song

Conclusion

In this tutorial, you learned how to create a tone using a buzzer and Arduino. You also learned about the Arduino tone() function. I hope you can now easily include a buzzer in your projects.

I hope you found this tutorial helpful. Share it with relevant people or groups. Feel free to comment below if you have any doubts or suggestions.

You can also subscribe to our mailing list to receive the weekly newsletter directly in your inbox.

Help me to Build more Projects for You!

At CircuitGeeks, we're passionate about creating exciting electronics projects and sharing our knowledge with the world. Our projects are free and open to everyone, but we would need your support to keep the creativity flowing!

If you enjoy our work and find our projects valuable, please consider supporting us on Buymeacoffee. By buying us a coffee, you help us buy more components and keep our projects going strong.

We truly appreciate your contribution!

1 thought on “Arduino Buzzer Tutorial – Playing Tones and Melodies”

  1. Is there a way to use both tone() and analogWrite() in the same loop? I would like to control the volume as well as the frequency.

    Reply

Leave a Comment