Introduction: Arduino Euphony From Sheet Music

It's december again, already christmas. This flavour is (in)renowned for those jingling ornaments, gift cards, you name it. Have it off information technology or hate it, it's jolly perfoliate to make one of those with Your ducky air.

In this instructor you will study how to transmute a monophonic melody to code!

(since it's christmas, the example will be well-known)

Supplies

An Arduino (compatible) board

Piezo buzzer (with proper device driver circuit)

Music to play

Mistreat 1: Convince From Notes to Frequency

All note has a frequency. Higher frequency = higher sound.

You can check every note, for model present: online signal generator. Also, this site can play you the sine wave version and the square wave verision overly. Set about used thereto - most of the Arduino boards tin can only play square waves.

It would be much easier if someone had written pour down all the frequencies for every the notes! Oh wait, they did. Now we have to "decode" the frequency cardinal canorous note at a clock. Do you cognize that weirdo f? That's the "violin key" - that means we know the absolute value of the notes, not just the relative. I enclosed a trifle cheat sheet to speed up the decoding process. Start decoding the notes.

Dance step 2: Note Lengths

You don't take to play a Federal Reserve note forever!

All note has a relation distance, and the music has a tempo. M.M. - beat per minute, or how many quarter notes in 4/4 music.

Some music to put under the values in perspective:

  • Taylor Swift: Willow tree - 78 Metronome marking
  • Billie Eilish: Dont Smile At Me - 92 Beats per minute
  • Bee Gees: Stayin' Alive - 104 BPM (hum this while performing a Kiss of life, no joke)
  • Alternating current/DC: Surprised - 136 BPM
  • The Prodigy: Nary Good (Start The Dance) - 144 BPM
  • Dune: Million miles from home - 171 BPM
  • S3RL: Dopamine - 175 BPM
  • Blümchen: Nur geträumt - 180 BPM

(yes, the 90' had some latched music in the radio)

60 / BPM * 1000 = how many miliseconds per 1/4 note. Do the maths for the others.

Dance step 3: The Circuit

I wasn't joking about the proper device driver circuit! Piezo buzzers are more like "capacitive loads", that means they human action like diminutive capacitors. That means turning on and hit them (more like driving one of its pin to HIGH or LOW) causes (congeneric) vast current spikes, and microcontrollers aren't into that gourmandize. If you don't believe Pine Tree State or would like to sleep with a bit more about this, record this clause from DigiKey!

⚠️Stupid Mistake Alert⚠️

Swapping to "regular" speaker isn't wagerer, they are "inductive loads", even worse! Switch power supplies transform DC to DC with inductors, so worst guinea pig IT lav kick even higher voltages than your power supply! And what happens when you "forget" to switch off an inductance? That's appropriate, short circuit with unlimited amps drawn!

Soh either get a proper piezo module operating room make a driver circiut. Ne'er e'er repel a load directly from a pin. Select a friendly, PWM capable PIN.

Step 4: Convert to Code

Now you have the notes and lengths. That's already enough work, get the source code with the notes defined. Impartial copy-paste.

Computer memory the notes and lengths in arrays, and play each Ohio them while iterating. Quickly note: I used a "pace" variable and relative note lengths for fast adjustments.

The tone() function plays the smel, only one at a time. First parameter is the pin, the forward is the frequency to play, the third is how long.

Patc some Arduino functions "block" execution, pregnant you perplexed there until information technology finishes (yes, analogRead(), no one likes to wait), modulate() just does everything "in the background knowledge", so wait manually, for example with a postponement().

Sometimes the solution just doesn't sound right. Adjust the weirdo notes!

Step 5: Enjoy!

Upload the terminal version and enjoy your mold!

I hope you found this tutorial functional!

Be the First to Share

Recommendations

  • Anything Goes Contest 2022

    Anything Goes Contest 2022