Advertisement
2 hardware_setup

Arduino Melody Shaper

This tutorial will show you how to use the Arduino to play your most-loved tunes. The steps are pretty simple to follow and should get your project running within minutes.

The Arduino here creates tones of different frequencies, playing them through a connected piezo-speaker. The variation of the frequency of the tone (pitch) and the correct timing (rhythm) create the melody. Arduino-generated music signals outputted through one of its digital pins drive the piezo-speaker through a simple driver circuit. Note that you can change the melody easily by uploading a new sketch. Well, let’s get started.

Parts Required

1. Arduino Uno
2. Piezo-Speaker
3. 2N2222 (or S8050) Transistor, 1K Resistor, 100-μF Capacitor
4. 3-pin Female Header, Male-to-Male Jumper Wires, 9-V Battery

Hardware Setup

Remember, most commercial piezo-speakers usually have a red (+) and a black (–) wire. So connect the wires to your circuit exactly as indicated in the schematic. However, you can use an ordinary piezo-ceramic element (without any plastic shell, so it looks like a shiny metallic disc) in lieu of the recommended piezo-speaker. Here’s what the hardware setup looks like (it’s powered by an external 9-V battery):

2 hardware_setup

Arduino Sketch

Since the melody-filled sketch in this tutorial requires a “pitches.h” file, first download the “pitches.h” file to use with the “melody-sketch.ino” code (also see related download links at the bottom). Ensure that file “pitches.h” is placed in the same folder with the “melody-sketch.ino”.

3 sketch_window

In the sketch presented here, the syntax of tone function is: tone (pin, frequency, duration).

The parameters are:

• pin: the pin on which to generate the tone
• frequency: the frequency of the tone in hertz (each musical note is determined by a frequency)
• duration: the duration of the tone in milliseconds (optional)

The tone function generates a square wave of the specified frequency on a pin during the time of duration field (optional). The array melody is used to store the notes, using names corresponding to the note and the frequency.

Tiny Melody

The Digispark Attiny85 development board is undoubtedly an efficacious platform for shrinking many Arduino projects. It’s bundled with capabilities, and apart from having fewer I/O pins, it can do most of what the microcontroller in the Arduino can do. Besides, you can program it from the same Arduino IDE used with your Arduino. I do not want to spend too much time talking about Digispark as it is well documented everywhere. Take a look at the Getting Started example in the documentation (http://digistump.com/wiki/start) for all of the information. However, take a note on some significant details:
• Digispark demands Arduino IDE 1.6.5+ (1.6.9 used by me)
• Before plugging the Digispark board into a USB (or running the Arduino IDE), you need to download and install the requisite DigiSpark driver (http://sourceforge.net/projects/digistump/files/DigisparkArduino-Win32-1.0.4-May19.zip/download)
• You do not need to plug in your Digispark before invoking an upload. Just hit the upload button. The bottom status box will now ask you to plug in your Digispark — at this point, you need to plug it in — or unplug and replug it
• If you unplug the Digispark and plug it back in or attach it to another power source, there will be a delay of five seconds before the code you programmed will run

4 tiny_setup

When I used the above hardware setup and “Tiny-Melody.ino” code (see download link) on Digispark Attiny 85, Attiny85 gives the right sound as a pwm output on the defined speaker pin. Anyway, this is just an example. Better still, you can experiment with your own music notations (and modified hardware) to create short, fascinating melodies.

5 tinycode_window

Making Sounds
Here is an ultra-simple sketch playing a scale through Arduino. Let’s dissect the program for analysis.

int piezoPin = 9; // Connect + of the piezo speaker to D9 and connect its next wire to Gnd

int numTones = 10;
int tones[] = {261, 277, 294, 311, 330, 349, 370, 392, 415, 440};
// mid C C# D D# E F F# G G# A

void setup()
{
   for (int i = 0; i < numTones; i++)
   {
      tone(piezoPin, tones[i]);
      delay(500);
   }
   noTone(piezoPin);
}
void loop()
{
} // Reset to play the scale again

The Arduino command “tone” takes two parameters: The first is the pin to play the tone on, and the second is the frequency of the tone to play. To play a note of a particular pitch, we should specify its frequency. The different frequencies for each note are kept in an array. As an array is like a list, a scale can be played by playing each of the notes in the list in turn. The “for” loop will count from zero to nine using the variable “i.” To get the frequency of the note to play at each step, we use “tone[i].” This refers to the value in the “tones” array at position “i.” So, for example, “tones[0]” is 261, “tones[3]” is 311, etc. If we put the code into “loop” rather than “setup,” the same scale will be played over and over again. So here in this sketch, “loop” contains nothing!

Got it? It’s time to play your music.

No Comments

Join the conversation!

Error! Please fill all fields.
Looking for the latest from TI?