Circuit 06 – Piezo Tunes

Having explored Motion, Light and Electrons we are now going to look at Sound using a simple circuit consisting of a single Piezo element.  Even though the output of the Arduino is digital because of its speed it is possible to produce music by pulsing a piezo element at the right frequency.The tone of each note is calculated using:

timeHigh = period / 2 = 1 / (2 * toneFrequency)

which allows us to produce the following notes:

c261 Hz38301915
d294 Hz34001700
e329 Hz30381519
f349 Hz28641432
g392 Hz25501275
a440 Hz22721136
b493 Hz20281014
C523 Hz1912956

The following code will play Twinkle Twinkle little star. The four functions are generic but the four lines at the top are what determines the tune played.  For example, the following is the first line from Happy Birthday

char notes[] = {"ccdcfeccdcgf "};
int beats[] = {1,1,1,1,1,2,1,1,1,1,1,2,4};