How to Play Mulitple Buzzers at Once

Pages
Contributors: Blade
Favorited Favorite 0

Polyphony in Action

Simple electronic sound devices, such as the SparkFun Qwiic Buzzer can output one note at a time. For a simple beep here or a buzz there, this works just fine. However, if your project needs a little more pizzaz than a single note then you’ll have to utilize multiple buzzers at once. This is what is called Polyphony. Polyphony is simply when two or more separate tones or melodies are sounded out simultaneously. As mentioned before, Qwiic Buzzers only make one sound at a time so in order to create a polyphonic melody you would need to utilize multiple Qwiic Buzzers. This involves setting each buzzer to a different I2C address and playing different notes simultaneously, creating more complex and rich soundscapes in your projects.

In this tutorial, we'll connect three Qwiic Buzzers to a RedBoard microcontroller and play a melody, harmony, and bass part of one of the most recognizable tunes of all time; the Super Mario Bros theme!

Hardware Needed

SparkFun Qwiic Cable Kit

SparkFun Qwiic Cable Kit

KIT-15081
$8.95
22
SparkFun RedBoard Plus

SparkFun RedBoard Plus

DEV-18158
$21.50
6
SparkFun Qwiic Buzzer

SparkFun Qwiic Buzzer

BOB-24474
$6.95

 


 

Connecting the Hardware

Connections are breeze with Qwiic:

  1. Connect three SparkFun Qwiic Buzzers to your microcontroller via the Qwiic connectors.
  2. Ensuring each buzzer has a unique I2C address is key to playing the buzzers simultaneously:
    • Default addresses are 0x34, 0x35, and 0x36 for the melody, harmony, and bass buzzers, respectively.
  3. Setup Simple Circuit: Use Qwiic cables to daisy-chain the buzzers to the microcontroller.

 

Qwiic Buzzer Setup

If you've been to SparkFun HQ you'll recognize our giant Nintendo controller.

 


 

Software Setup

Before you begin you'll first need to Install the Arduino IDE and SparkFun Qwiic Buzzer Arduino Library.

Installing the Qwiic Buzzer Library

  • Open the Arduino IDE.
  • Go to Sketch > Include Library > Manage Libraries...
  • Search for "SparkFun Qwiic Buzzer Arduino Library" and install it or install from the link below.
Qwiic Buzzer Arduino Library

 


 

Running the Code

  1. Upload the Code:
    • Connect your microcontroller to your computer.
    • Open the Arduino IDE.
    • Copy and paste the provided code into the IDE.
    • Select the correct board and port.
    • Click Upload.
  2. Observe the Buzzers:
    • Once the code is uploaded, the buzzers will play the Super Mario Bros theme with melody, harmony, and bass parts.

 

/*
* SparkFun Qwiic Buzzer Polyphony Demo
* 
* This example shows off the Qwiic Buzzer's ability
* to comtrol multiple buzzers simultaneously by
* playing a 3-part arrangement of a little bit of
* the Super Mario Bros Theme.
* 
* By Rob Reynolds @ SparkFun Electronics
* February 2024
* 
* Hardware connections:
* Connect three (3) SparkFun Qwiic Buzzers to you
* microcontroller via the Qwiic connectors.
* 
* SparkFun code, firmware, and software is released under the MIT License.
* Please see LICENSE.md for further details.
* 
* This code is released under the Beerware License. If you find this code
* useful, and see me or any other Funion at the local, buy us a beer!
* 
* Distributed as-is, with no warranty * 
* 
*/


#include <SparkFun_Qwiic_Buzzer_Arduino_Library.h>  // Import the library for the Qwiic Buzzer
QwiicBuzzer melodyBuzzer; //0x34 (default)
QwiicBuzzer harmonyBuzzer; //0x35 (previously changed)
QwiicBuzzer bassBuzzer; //0x36  (previously changed)

// notes in the melody:
int melody[] = {
  SFE_QWIIC_BUZZER_NOTE_E5,
  SFE_QWIIC_BUZZER_NOTE_E5,
  SFE_QWIIC_BUZZER_NOTE_E5, 
  SFE_QWIIC_BUZZER_NOTE_C5, 
  SFE_QWIIC_BUZZER_NOTE_E5, 
  SFE_QWIIC_BUZZER_NOTE_G5,
  SFE_QWIIC_BUZZER_NOTE_REST,  // silence (aka "rest") 
  SFE_QWIIC_BUZZER_NOTE_G4, 
  SFE_QWIIC_BUZZER_NOTE_REST,  // silence (aka "rest")
  // "A" section starts here***********************************
  SFE_QWIIC_BUZZER_NOTE_C5, 
  SFE_QWIIC_BUZZER_NOTE_REST,  // silence (aka "rest")
  SFE_QWIIC_BUZZER_NOTE_G4,
  SFE_QWIIC_BUZZER_NOTE_REST,  // silence (aka "rest")
  SFE_QWIIC_BUZZER_NOTE_E4,
  SFE_QWIIC_BUZZER_NOTE_REST,  // silence (aka "rest")
  SFE_QWIIC_BUZZER_NOTE_A4,
  SFE_QWIIC_BUZZER_NOTE_B4,
  SFE_QWIIC_BUZZER_NOTE_AS4,
  SFE_QWIIC_BUZZER_NOTE_A4,
  SFE_QWIIC_BUZZER_NOTE_G4, //TRIPLETS START
  SFE_QWIIC_BUZZER_NOTE_E5,
  SFE_QWIIC_BUZZER_NOTE_G5,
  SFE_QWIIC_BUZZER_NOTE_A5,
  SFE_QWIIC_BUZZER_NOTE_F5,
  SFE_QWIIC_BUZZER_NOTE_G5,
  SFE_QWIIC_BUZZER_NOTE_E5,
  SFE_QWIIC_BUZZER_NOTE_C5,
  SFE_QWIIC_BUZZER_NOTE_D5,
  SFE_QWIIC_BUZZER_NOTE_B4,
  SFE_QWIIC_BUZZER_NOTE_REST,
  // "A" section repeat starts here ***********************
  SFE_QWIIC_BUZZER_NOTE_C5, 
  SFE_QWIIC_BUZZER_NOTE_REST,  
  SFE_QWIIC_BUZZER_NOTE_G4,
  SFE_QWIIC_BUZZER_NOTE_REST,  
  SFE_QWIIC_BUZZER_NOTE_E4,
  SFE_QWIIC_BUZZER_NOTE_REST,  
  SFE_QWIIC_BUZZER_NOTE_A4,
  SFE_QWIIC_BUZZER_NOTE_B4,
  SFE_QWIIC_BUZZER_NOTE_AS4,
  SFE_QWIIC_BUZZER_NOTE_A4,
  SFE_QWIIC_BUZZER_NOTE_G4, //TRIPLETS START
  SFE_QWIIC_BUZZER_NOTE_E5,
  SFE_QWIIC_BUZZER_NOTE_G5,
  SFE_QWIIC_BUZZER_NOTE_A5,
  SFE_QWIIC_BUZZER_NOTE_F5,
  SFE_QWIIC_BUZZER_NOTE_G5,
  SFE_QWIIC_BUZZER_NOTE_E5,
  SFE_QWIIC_BUZZER_NOTE_C5,
  SFE_QWIIC_BUZZER_NOTE_D5,
  SFE_QWIIC_BUZZER_NOTE_B4,
  SFE_QWIIC_BUZZER_NOTE_REST,
  //"B" Section starts here**************************
  SFE_QWIIC_BUZZER_NOTE_REST,  //measure 7
  SFE_QWIIC_BUZZER_NOTE_G5,
  SFE_QWIIC_BUZZER_NOTE_FS5,
  SFE_QWIIC_BUZZER_NOTE_F5,
  SFE_QWIIC_BUZZER_NOTE_DS5,
  SFE_QWIIC_BUZZER_NOTE_REST,  
  SFE_QWIIC_BUZZER_NOTE_E5,
  SFE_QWIIC_BUZZER_NOTE_REST,  //measure 8
  SFE_QWIIC_BUZZER_NOTE_GS4,
  SFE_QWIIC_BUZZER_NOTE_A4,
  SFE_QWIIC_BUZZER_NOTE_C5,
  SFE_QWIIC_BUZZER_NOTE_REST,
  SFE_QWIIC_BUZZER_NOTE_A4,
  SFE_QWIIC_BUZZER_NOTE_C5,
  SFE_QWIIC_BUZZER_NOTE_D5,
  SFE_QWIIC_BUZZER_NOTE_REST,  //measure 9
  SFE_QWIIC_BUZZER_NOTE_G5,
  SFE_QWIIC_BUZZER_NOTE_FS5,
  SFE_QWIIC_BUZZER_NOTE_F5,
  SFE_QWIIC_BUZZER_NOTE_DS5,
  SFE_QWIIC_BUZZER_NOTE_REST,
  SFE_QWIIC_BUZZER_NOTE_E5,
  SFE_QWIIC_BUZZER_NOTE_REST,  //measure 10
  SFE_QWIIC_BUZZER_NOTE_C6,
  SFE_QWIIC_BUZZER_NOTE_C6,
  SFE_QWIIC_BUZZER_NOTE_C6,
  SFE_QWIIC_BUZZER_NOTE_REST,
  SFE_QWIIC_BUZZER_NOTE_REST,  //measure 11
  SFE_QWIIC_BUZZER_NOTE_G5,
  SFE_QWIIC_BUZZER_NOTE_FS5,
  SFE_QWIIC_BUZZER_NOTE_F5,
  SFE_QWIIC_BUZZER_NOTE_DS5,
  SFE_QWIIC_BUZZER_NOTE_REST,
  SFE_QWIIC_BUZZER_NOTE_E5,
  SFE_QWIIC_BUZZER_NOTE_REST,  //measure 12
  SFE_QWIIC_BUZZER_NOTE_GS4,
  SFE_QWIIC_BUZZER_NOTE_A4,
  SFE_QWIIC_BUZZER_NOTE_C5,
  SFE_QWIIC_BUZZER_NOTE_REST,
  SFE_QWIIC_BUZZER_NOTE_A4,
  SFE_QWIIC_BUZZER_NOTE_C5,
  SFE_QWIIC_BUZZER_NOTE_D5,
  SFE_QWIIC_BUZZER_NOTE_REST,  //measure 13
  SFE_QWIIC_BUZZER_NOTE_DS5,
  SFE_QWIIC_BUZZER_NOTE_REST,
  SFE_QWIIC_BUZZER_NOTE_D5,
  SFE_QWIIC_BUZZER_NOTE_REST,
  SFE_QWIIC_BUZZER_NOTE_C5,    //measure 14
  SFE_QWIIC_BUZZER_NOTE_REST,
  SFE_QWIIC_BUZZER_NOTE_REST,
  SFE_QWIIC_BUZZER_NOTE_REST,
  SFE_QWIIC_BUZZER_NOTE_REST,
};

// Notes in the harmony
int harmony[] = {
  SFE_QWIIC_BUZZER_NOTE_FS4,
  SFE_QWIIC_BUZZER_NOTE_FS4,
  SFE_QWIIC_BUZZER_NOTE_FS4, 
  SFE_QWIIC_BUZZER_NOTE_FS4, 
  SFE_QWIIC_BUZZER_NOTE_FS4, 
  SFE_QWIIC_BUZZER_NOTE_B4,
  SFE_QWIIC_BUZZER_NOTE_REST,   
  SFE_QWIIC_BUZZER_NOTE_G3, 
  SFE_QWIIC_BUZZER_NOTE_REST,  
  // "A" section starts here*********************************
  SFE_QWIIC_BUZZER_NOTE_E4, 
  SFE_QWIIC_BUZZER_NOTE_REST,  
  SFE_QWIIC_BUZZER_NOTE_C4,
  SFE_QWIIC_BUZZER_NOTE_REST,  
  SFE_QWIIC_BUZZER_NOTE_G3,
  SFE_QWIIC_BUZZER_NOTE_REST,  
  SFE_QWIIC_BUZZER_NOTE_C4,
  SFE_QWIIC_BUZZER_NOTE_D4,
  SFE_QWIIC_BUZZER_NOTE_CS4,
  SFE_QWIIC_BUZZER_NOTE_C4,
  SFE_QWIIC_BUZZER_NOTE_C4, //TRIPLETS START
  SFE_QWIIC_BUZZER_NOTE_G4,
  SFE_QWIIC_BUZZER_NOTE_B4,
  SFE_QWIIC_BUZZER_NOTE_C5,
  SFE_QWIIC_BUZZER_NOTE_A4,
  SFE_QWIIC_BUZZER_NOTE_B4,
  SFE_QWIIC_BUZZER_NOTE_A4,
  SFE_QWIIC_BUZZER_NOTE_E4,
  SFE_QWIIC_BUZZER_NOTE_F4,
  SFE_QWIIC_BUZZER_NOTE_D4,
  SFE_QWIIC_BUZZER_NOTE_REST,
  // "A" section repeat starts here*********************************
  SFE_QWIIC_BUZZER_NOTE_E4, 
  SFE_QWIIC_BUZZER_NOTE_REST,  
  SFE_QWIIC_BUZZER_NOTE_C4,
  SFE_QWIIC_BUZZER_NOTE_REST,  
  SFE_QWIIC_BUZZER_NOTE_G3,
  SFE_QWIIC_BUZZER_NOTE_REST,  
  SFE_QWIIC_BUZZER_NOTE_C4,
  SFE_QWIIC_BUZZER_NOTE_D4,
  SFE_QWIIC_BUZZER_NOTE_CS4,
  SFE_QWIIC_BUZZER_NOTE_C4,
  SFE_QWIIC_BUZZER_NOTE_C4, //TRIPLETS START
  SFE_QWIIC_BUZZER_NOTE_G4,
  SFE_QWIIC_BUZZER_NOTE_B4,
  SFE_QWIIC_BUZZER_NOTE_C5,
  SFE_QWIIC_BUZZER_NOTE_A4,
  SFE_QWIIC_BUZZER_NOTE_B4,
  SFE_QWIIC_BUZZER_NOTE_A4,
  SFE_QWIIC_BUZZER_NOTE_E4,
  SFE_QWIIC_BUZZER_NOTE_F4,
  SFE_QWIIC_BUZZER_NOTE_D4,
  SFE_QWIIC_BUZZER_NOTE_REST,
  //"B" Section starts here**************************
  SFE_QWIIC_BUZZER_NOTE_REST,  //measure 7
  SFE_QWIIC_BUZZER_NOTE_E5,
  SFE_QWIIC_BUZZER_NOTE_DS5,
  SFE_QWIIC_BUZZER_NOTE_D5,
  SFE_QWIIC_BUZZER_NOTE_B4,
  SFE_QWIIC_BUZZER_NOTE_REST,
  SFE_QWIIC_BUZZER_NOTE_C5,
  SFE_QWIIC_BUZZER_NOTE_REST,  //measure 8
  SFE_QWIIC_BUZZER_NOTE_E4,
  SFE_QWIIC_BUZZER_NOTE_F4,
  SFE_QWIIC_BUZZER_NOTE_G4,
  SFE_QWIIC_BUZZER_NOTE_REST,
  SFE_QWIIC_BUZZER_NOTE_C4,
  SFE_QWIIC_BUZZER_NOTE_E4,
  SFE_QWIIC_BUZZER_NOTE_F4,
  SFE_QWIIC_BUZZER_NOTE_REST,  //measure 9
  SFE_QWIIC_BUZZER_NOTE_E5,
  SFE_QWIIC_BUZZER_NOTE_DS5,
  SFE_QWIIC_BUZZER_NOTE_D5,
  SFE_QWIIC_BUZZER_NOTE_B4,
  SFE_QWIIC_BUZZER_NOTE_REST,
  SFE_QWIIC_BUZZER_NOTE_C5,
  SFE_QWIIC_BUZZER_NOTE_REST,  //measure 10
  SFE_QWIIC_BUZZER_NOTE_G5,
  SFE_QWIIC_BUZZER_NOTE_G5,
  SFE_QWIIC_BUZZER_NOTE_G5,
  SFE_QWIIC_BUZZER_NOTE_REST,
  SFE_QWIIC_BUZZER_NOTE_REST,  //measure 11
  SFE_QWIIC_BUZZER_NOTE_E5,
  SFE_QWIIC_BUZZER_NOTE_DS5,
  SFE_QWIIC_BUZZER_NOTE_D5,
  SFE_QWIIC_BUZZER_NOTE_B4,
  SFE_QWIIC_BUZZER_NOTE_REST,
  SFE_QWIIC_BUZZER_NOTE_C5,
  SFE_QWIIC_BUZZER_NOTE_REST,  //measure 12
  SFE_QWIIC_BUZZER_NOTE_E4,
  SFE_QWIIC_BUZZER_NOTE_F4,
  SFE_QWIIC_BUZZER_NOTE_G4,
  SFE_QWIIC_BUZZER_NOTE_REST,
  SFE_QWIIC_BUZZER_NOTE_C4,
  SFE_QWIIC_BUZZER_NOTE_E4,
  SFE_QWIIC_BUZZER_NOTE_F4,
  SFE_QWIIC_BUZZER_NOTE_REST,  //measure 13
  SFE_QWIIC_BUZZER_NOTE_GS4,
  SFE_QWIIC_BUZZER_NOTE_REST,
  SFE_QWIIC_BUZZER_NOTE_F4,
  SFE_QWIIC_BUZZER_NOTE_REST,
  SFE_QWIIC_BUZZER_NOTE_E4,    //measure 14
  SFE_QWIIC_BUZZER_NOTE_REST,
  SFE_QWIIC_BUZZER_NOTE_REST,
  SFE_QWIIC_BUZZER_NOTE_REST,
  SFE_QWIIC_BUZZER_NOTE_REST,
};

// notes in the bass:
int bass[] = {
  SFE_QWIIC_BUZZER_NOTE_D3,
  SFE_QWIIC_BUZZER_NOTE_D3,
  SFE_QWIIC_BUZZER_NOTE_D3, 
  SFE_QWIIC_BUZZER_NOTE_D3, 
  SFE_QWIIC_BUZZER_NOTE_D3, 
  SFE_QWIIC_BUZZER_NOTE_G3,
  SFE_QWIIC_BUZZER_NOTE_REST,   
  SFE_QWIIC_BUZZER_NOTE_G2, 
  SFE_QWIIC_BUZZER_NOTE_REST,  
  // "A" section starts here**********************************
  SFE_QWIIC_BUZZER_NOTE_G3, 
  SFE_QWIIC_BUZZER_NOTE_REST,  
  SFE_QWIIC_BUZZER_NOTE_E3,
  SFE_QWIIC_BUZZER_NOTE_REST,  
  SFE_QWIIC_BUZZER_NOTE_C3,
  SFE_QWIIC_BUZZER_NOTE_REST,  
  SFE_QWIIC_BUZZER_NOTE_F3,
  SFE_QWIIC_BUZZER_NOTE_G3,
  SFE_QWIIC_BUZZER_NOTE_FS3,
  SFE_QWIIC_BUZZER_NOTE_F3,
  SFE_QWIIC_BUZZER_NOTE_E3, //TRIPLETS START
  SFE_QWIIC_BUZZER_NOTE_C4,
  SFE_QWIIC_BUZZER_NOTE_E4,
  SFE_QWIIC_BUZZER_NOTE_F4,
  SFE_QWIIC_BUZZER_NOTE_D4,
  SFE_QWIIC_BUZZER_NOTE_E4,
  SFE_QWIIC_BUZZER_NOTE_C4,
  SFE_QWIIC_BUZZER_NOTE_A3,
  SFE_QWIIC_BUZZER_NOTE_B3,
  SFE_QWIIC_BUZZER_NOTE_G3,
  SFE_QWIIC_BUZZER_NOTE_REST,
  // "A" section starts here**********************************
  SFE_QWIIC_BUZZER_NOTE_G3, 
  SFE_QWIIC_BUZZER_NOTE_REST,  
  SFE_QWIIC_BUZZER_NOTE_E3,
  SFE_QWIIC_BUZZER_NOTE_REST,  
  SFE_QWIIC_BUZZER_NOTE_C3,
  SFE_QWIIC_BUZZER_NOTE_REST,  
  SFE_QWIIC_BUZZER_NOTE_F3,
  SFE_QWIIC_BUZZER_NOTE_G3,
  SFE_QWIIC_BUZZER_NOTE_FS3,
  SFE_QWIIC_BUZZER_NOTE_F3,
  SFE_QWIIC_BUZZER_NOTE_E3, //TRIPLETS START
  SFE_QWIIC_BUZZER_NOTE_C4,
  SFE_QWIIC_BUZZER_NOTE_E4,
  SFE_QWIIC_BUZZER_NOTE_F4,
  SFE_QWIIC_BUZZER_NOTE_D4,
  SFE_QWIIC_BUZZER_NOTE_E4,
  SFE_QWIIC_BUZZER_NOTE_C4,
  SFE_QWIIC_BUZZER_NOTE_A3,
  SFE_QWIIC_BUZZER_NOTE_B3,
  SFE_QWIIC_BUZZER_NOTE_G3,
  SFE_QWIIC_BUZZER_NOTE_REST,
  // "B" section starts here ***************************************
  // Numbers indicate note durations, just for my reference when writing
  SFE_QWIIC_BUZZER_NOTE_C3,    //4  measure 7
  SFE_QWIIC_BUZZER_NOTE_REST,  //8 
  SFE_QWIIC_BUZZER_NOTE_G3,    //8
  SFE_QWIIC_BUZZER_NOTE_REST,  //8
  SFE_QWIIC_BUZZER_NOTE_REST,  //8
  SFE_QWIIC_BUZZER_NOTE_C4,    //4
  SFE_QWIIC_BUZZER_NOTE_REST,  //8
  SFE_QWIIC_BUZZER_NOTE_F3,    //4  measure 8
  SFE_QWIIC_BUZZER_NOTE_REST,  //16
  SFE_QWIIC_BUZZER_NOTE_REST,  //16
  SFE_QWIIC_BUZZER_NOTE_C4,    //8
  SFE_QWIIC_BUZZER_NOTE_C4,    //8
  SFE_QWIIC_BUZZER_NOTE_REST,  //8
  SFE_QWIIC_BUZZER_NOTE_F3,    //8
  SFE_QWIIC_BUZZER_NOTE_REST,  //8
  SFE_QWIIC_BUZZER_NOTE_C3,    //4  measure 9
  SFE_QWIIC_BUZZER_NOTE_REST,  //8
  SFE_QWIIC_BUZZER_NOTE_G3,    //8
  SFE_QWIIC_BUZZER_NOTE_REST,  //4
  SFE_QWIIC_BUZZER_NOTE_REST,  //4
  SFE_QWIIC_BUZZER_NOTE_G3,    //8
  SFE_QWIIC_BUZZER_NOTE_C4,    //8
  SFE_QWIIC_BUZZER_NOTE_REST,  //2  measure 10
  SFE_QWIIC_BUZZER_NOTE_REST,  //4
  SFE_QWIIC_BUZZER_NOTE_REST,  //4
  SFE_QWIIC_BUZZER_NOTE_REST,  //4
  SFE_QWIIC_BUZZER_NOTE_G3,    //4
  SFE_QWIIC_BUZZER_NOTE_C3,    //4  measure 11
  SFE_QWIIC_BUZZER_NOTE_REST,  //8
  SFE_QWIIC_BUZZER_NOTE_G3,    //8
  SFE_QWIIC_BUZZER_NOTE_REST,  //4
  SFE_QWIIC_BUZZER_NOTE_REST,  //4
  SFE_QWIIC_BUZZER_NOTE_C4,    //4
  SFE_QWIIC_BUZZER_NOTE_REST,  //4
  SFE_QWIIC_BUZZER_NOTE_F3,    //4  measure 12
  SFE_QWIIC_BUZZER_NOTE_REST,  //8
  SFE_QWIIC_BUZZER_NOTE_REST,  //4
  SFE_QWIIC_BUZZER_NOTE_C4,    //8
  SFE_QWIIC_BUZZER_NOTE_C4,    //4
  SFE_QWIIC_BUZZER_NOTE_REST,  //4
  SFE_QWIIC_BUZZER_NOTE_F3,    //4
  SFE_QWIIC_BUZZER_NOTE_REST,  //4
  SFE_QWIIC_BUZZER_NOTE_C3,    //4  measure 13
  SFE_QWIIC_BUZZER_NOTE_GS3,   //4
  SFE_QWIIC_BUZZER_NOTE_REST,  //8
  SFE_QWIIC_BUZZER_NOTE_AS3,   //4
  SFE_QWIIC_BUZZER_NOTE_REST,  //8
  SFE_QWIIC_BUZZER_NOTE_C4,    //4  measure 14
  SFE_QWIIC_BUZZER_NOTE_REST,  //8
  SFE_QWIIC_BUZZER_NOTE_G3,    //8
  SFE_QWIIC_BUZZER_NOTE_G3,    //4
  SFE_QWIIC_BUZZER_NOTE_C3,    //4
};


// note durations: 4 = quarter note, 8 = eighth note, etc.:
int marioNoteDurations[] = {
  8, 4, 4, 8, 4,    4, 4, 4, 4,
  4, 8, 4, 8, 4,    8, 4, 4, 8, 4,    6, 6, 6, 4, 8, 4,    4, 8, 8, 4, 8,
  4, 8, 4, 8, 4,    8, 4, 4, 8, 4,    6, 6, 6, 4, 8, 4,    4, 8, 8, 4, 8,  //start "B" section next line
  4, 8, 8, 8, 8, 8, 8,    8, 8, 8, 8, 8, 8, 8, 8,    4, 8, 8, 8, 8, 8, 8,    8, 4, 8, 4, 4,
  4, 8, 8, 8, 8, 8, 8,    8, 8, 8, 8, 8, 8, 8, 8,    4, 4, 8, 4, 8,     4, 8, 8, 4, 4
  // The extended spaces above aren't necessary, I use them to separate measures
  // simply to help me keep the music more clear. Very helpful for debugging!
};

void setup() {
  Serial.begin(115200);
  Serial.println("Qwiic Buzzer Super Mario Bros Sample");
  Wire.begin(); //Join I2C bus
  melodyBuzzer.begin(0x34);
  harmonyBuzzer.begin(0x35);
  bassBuzzer.begin(0x36);

/*  
  // These are good for testing, I commented them out when I moved the project to stand-alone

  //check if buzzer will connect over I2C
  if (buzzer1.begin() == false) {
    Serial.println("Buzzer 1 did not connect! Freezing.");
    while (1);
  }

  else if (buzzer2.begin() == false) {
    Serial.println("Buzzer 2 did not connect! Freezing.");
    while (1);
  }

  else if (buzzer3.begin() == false) {
    Serial.println("Buzzer 3 did not connect! Freezing.");
    while (1);
  }

*/
  Serial.println("Buzzers connected.");

  Serial.println("Buzzing Melody now...");
  play_melody();
}

void loop() {
  // do nothing here
  // we just play the melody once during setup above.
}

void play_melody()
{
    // iterate over the notes of the melody:
  for (int thisNote = 0; thisNote < 103; thisNote++) {  // 51 for A section only (repeated), 97 for entire demo for total melody/harmony notes, add 8 rests to bass

    // to calculate the note duration, take one second divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int melodyNoteDuration = 1000 / marioNoteDurations[thisNote];  // change number to change tempo. <1000 = faster; >1000 = slower

    melodyBuzzer.configureBuzzer(melody[thisNote], melodyNoteDuration, SFE_QWIIC_BUZZER_VOLUME_MAX);
    melodyBuzzer.on();

    harmonyBuzzer.configureBuzzer(harmony[thisNote], melodyNoteDuration, SFE_QWIIC_BUZZER_VOLUME_MAX);
    harmonyBuzzer.on();

    bassBuzzer.configureBuzzer(bass[thisNote], melodyNoteDuration, SFE_QWIIC_BUZZER_VOLUME_MAX);
    bassBuzzer.on();



    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = melodyNoteDuration * 1.30;
    delay(pauseBetweenNotes);
  }
}

Code Explanation

The code for this project can be a little intense as each individual sound that is made over 3 buzzers is a line of code. Our very own Rob Reynolds went through the painstaking process of working with figuring out each tone and octive needed for the Super Mario Brother's theme music. You can read all about his efforts here.

Raise your Buzzer Projects to the Next Level

Key Points in the Code

  1. Making sure the library is included: #include <SparkFun_Qwiic_Buzzer_Arduino_Library.h>
  2. Initialize Buzzers: Define three and assign each for melody, harmony, and bass.
  3. Play Melody Function: Configure each buzzer to play its respective part in the song.