Arduino Bascis - Playing Sounds and Tones

About: Watch this space for projects on science, engineering, and tech. I explain how and why things work, by breaking apart everyday objects and DIY-ing the heck out of them.

Intro: Arduino Bascis - Playing Sounds and Tones

I wanted to play some sound effects, and realized that this is one of the neglected areas when it comes to tutorials. Even on Youtube, there is a lack of good tutorials on Arduinos and sounds, so, I being the nice guy, decided to share my knowledge of Arduinos.

If you are not into reading, here's the full video with the actual sounds being played:

Step 1: First, the Basics

If you ever bought an Arduino Starter kit, they usually contain one Active Buzzer/ Speaker and one Passive Buzzer/ Speaker.

So which one is which?

The visual differences are the following:

- The Active Speaker is usually taller than the Passive one

- The Active Speaker is sealed at the bottom, but the Passive Speaker has the bottom open.

The technical differences are that the Active speaker has built in Active components that allow it to operate with DC Voltage.

So, if we apply a 5V or 3V supply to an Active speaker, it beeps automatically, whereas the Passive speaker works with an AC voltage, or a PWM DC Signal.

Now that we know the differences, let's go ahead and connect them.

Step 2: Connecting Speakers to the Arudino

The connections are simple.

One of the PWM pins (PIns 2 through 13) is connected to the positive of the speaker, and the negative is connected to 100 ohm resistor, which is connected to ground.

Now, we can go to our sketch/ code.

Step 3: The Code

The Code is actually just one line!

If you want to just play a single tone, the following is all that is needed:

tone(pin, frequency, duration)

Where pin, is the Pin the speaker is connected to, the frequency is the frequency (in Hertz), and duration in milliseconds, is optional.

Simple, right? Let's do something more interesting.

Step 4: Playing the Supermario Tone

The following can be used to play the Supermario tone!


Just paste, and enjoy:

Arduino Mario Bros Tunes
With Piezo Buzzer and PWM by: Dipto Pratyaksa last updated: 31/3/13 */ #include

#define melodyPin 3 //Mario main theme melody int melody[] = { NOTE_E7, NOTE_E7, 0, NOTE_E7, 0, NOTE_C7, NOTE_E7, 0, NOTE_G7, 0, 0, 0, NOTE_G6, 0, 0, 0,

NOTE_C7, 0, 0, NOTE_G6, 0, 0, NOTE_E6, 0, 0, NOTE_A6, 0, NOTE_B6, 0, NOTE_AS6, NOTE_A6, 0,

NOTE_G6, NOTE_E7, NOTE_G7, NOTE_A7, 0, NOTE_F7, NOTE_G7, 0, NOTE_E7, 0,NOTE_C7, NOTE_D7, NOTE_B6, 0, 0,

NOTE_C7, 0, 0, NOTE_G6, 0, 0, NOTE_E6, 0, 0, NOTE_A6, 0, NOTE_B6, 0, NOTE_AS6, NOTE_A6, 0,

NOTE_G6, NOTE_E7, NOTE_G7, NOTE_A7, 0, NOTE_F7, NOTE_G7, 0, NOTE_E7, 0,NOTE_C7, NOTE_D7, NOTE_B6, 0, 0 }; //Mario main them tempo int tempo[] = { 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,

12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,

9, 9, 9, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,

12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,

9, 9, 9, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, };

//

Step 5: What's Next?

What is you want to play something more complex? Like Music, or sound effects with your Arduino? Well, the Arduino memory is limited, so it is not possible to sample tones at a higher rate. In case you need to do something fancy, you would need an SD card connected to the Arduino and play the sound from the card.

Hope you guys learnt something from this!

Consider subscribing to Fungineers on Youtube; there's tutorials, DIY stuff, 3D printing geek-ery every week!

Share

    Recommendations

    • Fix It! Contest

      Fix It! Contest
    • Furniture Contest 2018

      Furniture Contest 2018
    • Tiny Home Contest

      Tiny Home Contest

    Discussions