Buzzer Music
Play a simple tune using an Arduino and a Buzzer. Component List:- 1 x Arduino Uno R3
- 1 x Female to Male Jumper Wire
- 1 x Passive Buzzer
Wiring Guide:
- Connect one leg of the buzzer to digital pin 9 on the Arduino.
- Connect the other leg of the buzzer to the ground (GND) on the Arduino using a jumper wire
Code
// Define notes and durations in a 2D array
int melody[][2] = {
{932, 1360}, {932, 125}, {932, 125}, {932, 301}, {932, 301},
{1397, 1360}, {1397, 125}, {1397, 125}, {1397, 301}, {1397, 301},
{1175, 2066}, {1175, 654}, {1245, 654}, {1245, 654}, {1397, 301},
{1245, 301}, {1175, 301}, {1047, 301}, {932, 2066}, {932, 654},
{1047, 654}, {1175, 301}, {1175, 125}, {1175, 125}, {1175, 125},
{932, 125}, {1175, 125}, {1397, 301}, {1397, 125}, {1397, 125},
{1397, 184}, {1175, 125}, {1397, 184}, {1865, 301}, {1865, 125},
{1865, 125}, {1865, 184}, {1397, 184}, {1865, 125}, {1865, 301},
{1397, 301}, {1245, 301}, {1175, 301}, {698, 301}, {784, 301},
{880, 301}, {932, 1713}, {932, 125}, {932, 125}, {932, 301},
{932, 301}, {1397, 1713}, {1397, 125}, {1397, 125}, {1397, 301},
{1397, 301}, {1175, 2066}, {1175, 654}, {1245, 654}, {1245, 654},
{1397, 301}, {1245, 301}, {1175, 301}, {1047, 301}, {932, 2066},
{932, 654}, {1047, 654}, {1175, 301}, {1175, 125}, {1175, 125},
{1175, 125}, {932, 125}, {1175, 125}, {1397, 366}, {1397, 152},
{1397, 152}, {1397, 223}, {1175, 152}, {1397, 223}, {1865, 366},
{1865, 152}, {1865, 152}, {1865, 223}, {1397, 223}, {1865, 152},
{1865, 1112},{1865, 301}, {1397, 301}, {1245, 301}, {1175, 301},
{698, 301}, {784, 301}, {880, 301}, {932, 1713}
};
void song(int buzzerPin) {
int numNotes = sizeof(melody) / sizeof(melody[0]);
for (int i = 0; i < numNotes; i++) {
tone(buzzerPin, melody[i][0]); // Play the note
delay(melody[i][1]); // Wait for the duration
noTone(buzzerPin); // Stop the tone
}
}
void setup() {
song(9);
}
void loop() {
// Empty loop
}
Looking to build this project? Check out our custom, Bahraini Assembled, starter kit here!
https://www.bitware.store/product/arduino-uno-r3-compatible-with-arduino-ide/
[bitware_arabic_portfolio_content]
Project Details | |
| Level: | Beginner (1/5) |
| Category: | Musical |
| Platform: | Arduino |
| Creator: | BitWare Store |