Skip to content

Arduino library to play a melody in piezo passive buzzers

Notifications You must be signed in to change notification settings

jmelabs/BuzzerMelody

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Arduino BuzzerMelody Library

Overview

This library provides a simple solution for playing one or more melodies on piezoelectric passive buzzers using the arduino core tone function in a non-blocking way.

Get Started

To use more than one BuzzerMelody instance, the Tone.cpp Arduino core function needs to be updated.

Each BuzzerMelody object will use a different Arduino Timer peripheral. So, the maximum number of melodies allowed to be played in parallel will depend on your Arduino/MCU platform. See the list below for a quick summary:

Arduino Platform (MCU) Number of Timers (Names) Datasheet Link
Arduino Uno Rev 3 (ATmega328P) 3 (Timer0, Timer1, Timer2) ATmega328P Datasheet
Arduino Mega (ATmega2560) 6 (Timer0, Timer1, Timer2, Timer3, Timer4, Timer5) ATmega2560 Datasheet
Arduino Leonardo (ATmega32U4) 4 (Timer0, Timer1, Timer3, Timer4) ATmega32U4 Datasheet
Arduino Nano (ATmega328P) 3 (Timer0, Timer1, Timer2) ATmega328P Datasheet
ESP32 Dev Module 4 (TIMER0, TIMER1, TIMER2, TIMER3 per core) ESP32 Datasheet

Timer 0 is usually used by the millis() function, so it is recommended to avoid using that.

If you are using this library with other libraries, check if there are any conflicts with the timers used.

Once the tone function modification is implemented, you don't to need to worry about anything in the main application.

How to use

Add the <BuzzerMelody.h> file to your code.

Create a BuzzerMelody object passing the following as arguments:

  • Buzzer Pin
  • Array of notes (see example).
  • The total number of notes.

Single Melody example

Main file

#include <Arduino.h>

#include <BuzzerMelody.h>
#include "SuperMarioBrosTheme.h"


const uint8_t BUZZER_0_PIN = 9;

BuzzerMelody buzzerMelody0(BUZZER_0_PIN, NUM_OF_NOTES0, melody0);


void setup() {
  buzzerMelody0.play();
}

void loop() {
  buzzerMelody0.loop();
}

Super Mario Bros melody header file

A melody shall be composed by an array of notes.

#include <Arduino.h>
#include "notes.h"

const Note melody0[] PROGMEM = {
	{ NOTE_E6, 143 }, // Bar 1 | E in octave 6 16th Note
	{ NOTE_E6, 286 }, // Bar 1 | E in octave 6 Eighth Note
	{ NOTE_E6, 286 }, // Bar 1 | E in octave 6 Eighth Note
	{ NOTE_C6, 143 }, // Bar 1 | C in octave 6 16th Note
    ...

A Note is a struct defined in the notes.h file composed by a frequency and a duration (in ms).

typedef struct {
  uint16_t frequency;
  uint16_t duration;
} Note;


Written by Jean Elias

About

Arduino library to play a melody in piezo passive buzzers

Resources

Stars

Watchers

Forks

Packages

No packages published