QC

Play a Christmas tune with Arduino and a buzzer

Christmas melody generated with a piezo buzzer and Arduino

Devices and components

Arduino-Nano

[HB] Mini breadboard

[HB] Piezo buzzer

[HB] Son Dupont

Software and tools

Arduino IDE

Project description

Wiring diagram

Arduino code

#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);

}

Christmas melody

#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);

}

}

HiBit - Buzzer

Official HiBit repository for the buzzer

Downloadable files

Buzzer

Wiring Diagram for Buzzer and Arduino Nano

arduino_buzzer.png

Documentation

Play a Christmas tune with Arduino and a buzzer

Main article

https://www.hibit.dev/posts/31/play-christmas-melody-with-arduino-and-a-buzzer




Note: Content and images are from: https://projecthub.arduino.cc/, with some modifications.
If you want it removed due to copyright reasons, please leave a comment. Thank you.
I want to share this article more widely so that everyone knows about Arduino and your project.

Easy remote control

Remotely control an LED with ease. Devices and components Arduino Nano 33 BLE with headers Box 525 Resistors precision 1% - 17 values Breadb...