Looking for Piezo Buzzer Code for Popular Songs

I apologize if there is an OBVIOUS answer to this question that I am too ridiculous to find. I'm doing my first coding project with Arduino [well, with anything] and would like to include a short burst of music through the Piezo buzzer. I've tried writing the code myself from the sheet music, but have discovered that I am not as musical as I thought I was.

Is there a place on this site where I can find already written code for songs? I've done google searches and can only find star wars and mario brothers. surely, somewhere there is code for other music??

Thank you!!

I doubt it. If it's only a few notes i could do it for you.

Remember that Arduino is primarily used by geeks, so there's going to be some bias in what songs are being transcribed. :smiley:

Perhaps there is copyrights problems with many songs.

Small pieces of solutions are all over the internet. I know I have been searching for a long time in the past for certains sounds. Some sounds are not even possible with the Arduino default tone() function, like the R2D2 sound.
But there is so much more to say about it.

For example a PWM library is able to make the Arduino produce sampled sound.
http://playground.arduino.cc/Code/PCMAudio

The standard tone() library is not good with changing the frequency, the toneAC is better but it uses fixed pins.

http://playground.arduino.cc/Code/ToneAC

And there are many wave shields, that are able to play wav files from an SD card.

The obvious answer should be - write a sketch that samples sound and converts for the tone lib.

sample analog port --> use e.g. FFT to pick the main frequency --> create a table of (frequency, duration) pairs

or write a sketch that reads MIDI and sends the tones to a buzzer (or 2?)
would be a nice sketch

I think you can find MIDI versions of most songs. If the the song is "popular" it's usually a copyright violation, but they don't seem to be too strict when it comes to MIDI.

MIDI is basically "sheet music" for computers. It's notes & timing that's played on MIDI instruments or on virtual instruments.

Your computer can play MIDI, but you'd have to get MIDI software to "decode" the notes & timing (so you can see the information). You'd still have to write some code, but it should be easier than reading sheet music. (I don't use MIDI, so I can't recommend software).

Or, maybe there's an Arduino MIDI library that can play MIDI to a piezo??? (That's not the normal way of doing it.)

Caltoa:
Some sounds are not even possible with the Arduino default tone() function, like the R2D2 sound.

Why are the R2D2 sounds not possible with the tone() function? That's exactly what I was looking for when I found this thread. Is there another way to do it generating tones in code? I don't want to have to do something like play back an MP3. That feels like cheating.

thataintworking:
Why are the R2D2 sounds not possible with the tone() function? That's exactly what I was looking for when I found this thread. Is there another way to do it generating tones in code?

I did some googling and found this file containing a R2D2 sound effect played just using the "tone()" function.

These are good, jurs. Thanks!

There's some fun code here that will play RTTL ringtones.

http://forum.arduino.cc/index.php?topic=53358.0

RTTL is kind of a lost art with today's phones, but if you do some digging you can find lots of (tinny) renditions of once popular game, movie, and song titles to play with just a piezo or speaker attached to your arduino.

-transfinite

Thanks, transfinite. That looks really cool too. I had forgotten all about those ringtones we used to get for our old phones before the phones got smart.

I did the Star Spangled Banner! SparkFun provided the base. I added some notes (they only had the eight listed in the header comments) and wrote the "song" (changed the beats and notes vectors)

/*
The Arduino has a built-in command called tone() which clicks
the buzzer at a certain frequency. This sketch knows the
frequencies of the common notes, allowing you to create songs.
We're never going to let you down!

Hardware connections:

The buzzer has two pins. One is positive and one is negative.
The postitive pin is marked by a "+" symbol on both the top
and bottom of the buzzer.

Connect the positive pin to Arduino digital pin 9.
(Note that this must be a PWM pin.)
Connect the negative pin to GND.

Tip: if the buzzer doesn't fit into the breadboard easily,
try rotating it slightly to fit into diagonal holes.

This sketch was written by SparkFun Electronics,
with lots of help from the Arduino community.
(This sketch was originally developed by D. Cuartielles for K3)
This code is completely free for any use.
Visit http://learn.sparkfun.com/products/2 for SIK information.
Visit http://www.arduino.cc to learn about the Arduino.

Version 2.0 6/2012 MDG
*/

/*
This sketch uses the buzzer to play songs.
The Arduino's tone() command will play notes of a given frequency.
We'll provide a function that takes in note characters (a-g),
and returns the corresponding frequency from this table:

note frequency
c 262 Hz
d 294 Hz
e 330 Hz
f 349 Hz
g 392 Hz
a 440 Hz
b 494 Hz
C 523 Hz

For more information, see http://arduino.cc/en/Tutorial/Tone
*/

const int buzzerPin = 9;

// We'll set up an array with the notes we want to play
// change these values to make different songs!

// Length must equal the total number of notes and spaces

const int songLength = 102;

// Notes is an array of text characters corresponding to the notes
// in your song. A space represents a rest (no tone)

char notes[] = "gecegCEDCe#gggEDCbabCCgecgecegCEDCe#gggEDCbabCCgecEEEFGGFEDEFFFEDCbabCe#ggCCCbaaaDFEDCCbggCDEFGCDEFDC "; // a space represents a rest

// Beats is an array of values for each note and rest.
// A "1" represents a quarter-note, 2 a half-note, etc.
// Don't forget that the rests (spaces) need a length as well.

int beats[] = {3, 1, 4, 4, 4, 8, 3, 1, 4, 4, 4, 8, 2, 2, 6, 2, 4, 8, 3, 1, 4, 4, 4, 4, 8, 3,
1, 4, 4, 4, 8, 3, 1, 4, 4, 4, 8, 2, 2, 6, 2, 4, 8, 3, 1, 4, 4, 4, 4, 4, 2, 2, 4, 4, 4, 8, 2,
2, 4, 4, 4, 8, 4, 6, 2, 4, 8, 3, 1, 6, 4, 4, 8, 4, 4, 4, 2, 2, 4, 4, 4, 4, 2, 2, 2, 2, 4, 4,
2, 2, 6, 2, 2, 2, 8, 2, 2, 6, 2, 4, 8, 1};

// The tempo is how fast to play the song.
// To make the song play faster, decrease this value.

int tempo = 150;

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

void loop()
{
int i, duration;

for (i = 0; i < songLength; i++) // step through the song arrays
{
duration = beats * tempo; // length of note/rest in ms

_ if (notes == ' ') // is this a rest?_
* {*
* delay(duration); // then pause for a moment*
* }*
* else // otherwise, play the note*
* {*
_ tone(buzzerPin, frequency(notes*), duration);
delay(duration); // wait for tone to finish*
* }
delay(tempo/10); // brief pause between notes*
* }*_

* // We only want to play the song once, so we'll pause forever:*
// while(true){}
* // If you'd like your song to play over and over,*
* // remove the above statement*
}
int frequency(char note)
{
* // This function takes a note character (a-g), and returns the*
* // corresponding frequency in Hz for the tone() function.*

* int i;*
* const int numNotes = 15; // number of notes we're storing*

* // The following arrays hold the note characters and their*
* // corresponding frequencies. The last "C" note is uppercase*
* // to separate it from the first lowercase "c". If you want to*
* // add more notes, you'll need to use unique characters.*
* // For the "char" (character) type, we put single characters*
* // in single quotes.*
* char names[] = { 'c', 'd', 'e', 'f', '#', 'g', 'a', 'b', 'C', 'D', 'E', 'F', 'G', 'A', 'B' };*
* int frequencies[] = {262, 294, 330, 349, 370, 392, 440, 494, 523, 587, 659, 699, 784, 880, 989};*

* // Now we'll search through the letters in the array, and if*
* // we find it, we'll return the frequency for that note.*

* for (i = 0; i < numNotes; i++) // Step through the notes*
* {*
_ if (names == note) // Is this the one?
* {
return(frequencies); // Yes! Return the frequency*

* }
}
return(0); // We looked through everything and didn't find it,
// but we still need to return a value, so return 0.
}*_

Doctor who theme......kinda(still working on it)

// Adafruit Circuit Playground - Melody
// 2017-01-29 Version 1.3 by Kristopher Ellis
// Adapted from melody by Tom Igoe on arduino.cc
// Uses the CircuitPlayground library to easily use the full functionality of the board

#include <Adafruit_CircuitPlayground.h>
#include "pitches.h"

const int numNotes = 57; // number of notes we are playing
int melody[] = { // specific notes in the melody
NOTE_B3, NOTE_C5, NOTE_B5, NOTE_D5, NOTE_A4, NOTE_B4, NOTE_B5, NOTE_G4, NOTE_B4, NOTE_D4, NOTE_C4, NOTE_B4, NOTE_C5, NOTE_B5, 0, NOTE_C5, NOTE_B5, NOTE_G4, NOTE_B5, NOTE_FS4, NOTE_G4, NOTE_A5, NOTE_B5, NOTE_G3, NOTE_D4, NOTE_E4, NOTE_D4, NOTE_C4, NOTE_D4, NOTE_G3, NOTE_E4, NOTE_D4, NOTE_C4, NOTE_G3, NOTE_B4, NOTE_A4, NOTE_C5, NOTE_B5, NOTE_G4, NOTE_B5, NOTE_FS4, NOTE_G4, NOTE_A5, NOTE_B5, NOTE_G3, NOTE_D4, NOTE_E4, NOTE_D4, NOTE_C4, NOTE_D4, NOTE_G3, NOTE_E4, NOTE_D4, NOTE_C4, NOTE_G3, NOTE_B4, NOTE_A4 };

int noteDurations[] = { // note durations: 4 = quarter note, 8 = eighth note, etc.:
4, 1, 4, 4, 4, 1, 4, 4, 2, 2, 8, 1, 8, 3, 2, 4, 1, 4, 4, 3, 8, 9, 1, 4, 4, 5, 8, 8, 4, 4, 4, 8, 8, 3, 8, 1, 4, 1, 4, 4, 3, 8, 9, 1, 4, 4, 5, 8, 8, 4, 4, 4, 8, 8, 3, 8, 1 };

void setup() {
CircuitPlayground.begin(); // initialize the CP library
}

void loop() {
if(CircuitPlayground.rightButton()) { // play when we press the right button
for (int thisNote = 0; thisNote < numNotes; thisNote++) { // play notes of the melody

int noteDuration = 1000 / noteDurations[thisNote];
CircuitPlayground.playTone(melody[thisNote], noteDuration);

int pauseBetweenNotes = 50;
delay(pauseBetweenNotes);
}
}
}

Just going back a few posts

Or, maybe there's an Arduino MIDI library that can play MIDI to a piezo??? (That's not the normal way of doing it.)

MD_MIDIFile is a library that will read a MIDI file on an SD card and pass the main code MIDI messages (eg, note on/off) at the right time through a callback. You should be able to use the tone() function to play the note. Follow the links to the code repository in my signature block below.

fountainpen:
Is there a place on this site where I can find already written code for songs? I've done google searches and can only find star wars and mario brothers. surely, somewhere there is code for other music??

Get yourself a FAT formatted SD card and MP3 player module for Arduino, put MP3 files with the songs on the SD card, connect the MP3 module to your Arduino and run some example code written for the MP3 module!

Directly playabe piezo buzzer melodies for use with the Arduino tone() function are not too much in circulation nowadays.

If you search, you can find some melodies to play short musical note sequences from films and TV series of the 70s, 80s and 90s as well as some tunes of pop songs from that time. Maybe some Christmas songs and a few folk songs, too. Not too many in circulation, I think. Nothing from the current pop music charts.

So if you are looking for musical notes of TV series "Dallas" or "Denver clan" or "Bonanza" or films like "James Bond", "Batman",or "Mission Impossible", you might be lucky to find the notes to play with a piezo buzzer. Older melodies might be easier to find than "popular" ones.

You can play .WAV files off of a microSD card if you have the right components.