Play Christmas melody with Arduino and a buzzer

  • avatar
  • 1.5K Views
  • 2 Likes
  • 5 mins read

Buzzer is used to generate sound, beep or even melody of a song. It can be found in alarm devices, computers, timers and confirmation of user input such as a mouse click or keystroke.

A piezo buzzer is not like a regular speaker that you might think of. It uses a material that actually changes shape when you apply electricity to it which in turn creates noise. The faster you bend the material, the higher the pitch of the noise that is produced.

Components

1x

Arduino Nano (or another Arduino module)

Ebay | Banggood | Aliexpress

1x

Mini-breadboard

Ebay | Banggood | Aliexpress

1x

Buzzer

Ebay | Banggood | Aliexpress

Pitches library and melodies

Official HiBit GitHub

Wiring schema

The connection is pretty easy, it only has control signal and GND. Pin D9 will be used to control the tone.

arduino_buzzer.png

Arduino code

Built-in Arduino functions will be used to generate the noise:

#define BUZZER_PIN 9

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

void loop()
{
tone(BUZZER_PIN, 1000); //Send 1KHz sound signal
delay(1000);

noTone(BUZZER_PIN); //Stop sound
delay(1000);
}

Tone() generates a square wave of the specified frequency (and 50% duty cycle) on a pin. A duration can be specified, otherwise the wave continues until a call to noTone(). If you are trying to make tones for the human ear, then values between 2000 and 5000 are where our ears are most tuned.

Only one tone can be generated at a time. If a tone is already playing on a different pin, the call to tone() will have no effect. If the tone is playing on the same pin, the call will set its frequency.

Use of the tone() function will interfere with PWM output on pins 3 and 11 (on boards other than the Mega). Also, it is not possible to generate tones lower than 31Hz.

Christmas melody

The code below uses an extra library available on our GitHub. This file contains all the pitch values for typical notes.

To import it, open the Arduino IDE, go to Sketch > Include Library > Add .ZIP Library and then select the file that you just downloaded.

arduino_import_library.png

Then you can simply use include statement:

#include "pitches.h"

#define BUZZER_PIN 9

int melody[] = {
NOTE_E5, NOTE_E5, NOTE_E5,
NOTE_E5, NOTE_E5, NOTE_E5,
NOTE_E5, NOTE_G5, NOTE_C5, NOTE_D5,
NOTE_E5,
NOTE_F5, NOTE_F5, NOTE_F5, NOTE_F5,
NOTE_F5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5,
NOTE_E5, NOTE_D5, NOTE_D5, NOTE_E5,
NOTE_D5, NOTE_G5
};

int durations[] = {
8, 8, 4,
8, 8, 4,
8, 8, 8, 8,
2,
8, 8, 8, 8,
8, 8, 8, 16, 16,
8, 8, 8, 8,
4, 4
};

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

void loop()
{
int size = sizeof(durations) / sizeof(int);

for (int note = 0; note < size; note++) {
//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 duration = 1000 / durations[note];
tone(BUZZER_PIN, melody[note], duration);

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

//stop the tone playing:
noTone(BUZZER_PIN);
}
}

Note: tone() function uses one of the built in timers on the Arduino’s micro-contoller and works independently of the delay() function.

Credits

Official GitHub: https://github.com/hibit-dev/buzzer

 Join Our Monthly Newsletter

Get the latest news and popular articles to your inbox every month

0 Comments

Leave a Reply

Your email address will not be published.