Jingle Bells with a Buzzer and Arduino
How to make your Arduino play Jingle Bells with a little buzzer and a 220ohm resistor.
Christmas is coming and everyone is about to put wonderful colored trees in their houses. This year we bought a new tree but we didn't change all the decorating parts like balls and lights. We also had a little red and white box shaped like Santa Claus' head that used to play Christmas jingles with some sort of a buzzer (so monotone sounds were produced). But the problem is that it broke down since it was probably too old.
Since I had an Arduino UNO at hand, I built a similar thing with it.
Here you can find the Github repo for the code.
Parts
- Arduino Uno (or whatever version you might already have)
- A buzzer
- A 220Ω resistor (but you don't need it if you want to destroy your buzzer!)
- A case, wires, and a little breadboard
Wiring
Let's get dirty
I started developing it with the buzzer and the resistor on the breadboard. (as you can see from the photo I thought to use a potentiometer to adjust the volume, but the one that I have is 50K and it's just. too much for the buzzer)
I took a case and I made two holes on it (one for the wirings and another for the 5v power supply) and I put the Arduino inside it and the breadboard outside.
On Github's readme, you can find two other ideas I left there if you're keen to improve this project.
The code
This isn't the first time that I make something like this. The first time I've used a buzzer, I've translated the imperial march of Star Wars notes to frequencies in an Arduino sketch, calculating manually the pauses and adding them line by line. I lost that piece of code and now I coded this song in a cleaner way using cycles and arrays. Take a look:
int buzzerPin = 8;
int tempo = 200;
char notes[] = "eeeeeeegcde fffffeeeeddedg";
int duration[] = {1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2};
void playTheShit(char note, int duration) {
char notesName[] = { 'c', 'd', 'e', 'f', 'g' };
int tones[] = { 261, 293, 329, 349, 392 };
for (int i = 0; i < sizeof(tones); i++) {
// Bind the note took from the char array to the array notesName
if (note == notesName[i]) {
// Bind the notesName to tones
tone(buzzerPin, tones[i], duration);
}
}
}
void setup() {
pinMode(buzzerPin, OUTPUT);
}
void loop() {
// Scan each char from "notes"
for (int i = 0; i < sizeof(notes)-1; i++) {
if (notes[i] == ' ') {
// If find a space it rests
delay(duration[i] * tempo);
} else {
playTheShit(notes[i], duration[i] * tempo);
}
// Pauses between notes
delay((tempo*2)*duration[i]);
}
}
And that is all
I hope that if you did or will do something along the lines of this little thing, you'll link it to me through social networks, but most importantly I wish you a Merry Christmas!
Enjoy 🎅🎄