Arduino Projects Online

Complete Details about the Arduino

Arduino Melody Shaper

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


) 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 theArduino Melody Shaper 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):

Arduino Melody Shaper

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”.

Arduino Melody Shaper

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 (// 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 (//
• 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

Arduino Melody Shaper

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.

Arduino Melody Shaper

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

  1. int piezoPin = 9; // Connect + of the piezo speaker to D9 and connect its next wire to Gnd
  3. int numTones = 10;
  4. int tones[] = {261, 277, 294, 311, 330, 349, 370, 392, 415, 440};
  5. // mid C C# D D# E F F# G G# A
  7. void setup()
  8. {
  9. for (int i = 0; i < numTones; i++)
  10. {
  11. tone(piezoPin, tones[i]);
  12. delay(500);
  13. }
  14. noTone(piezoPin);
  15. }
  16. void loop()
  17. {
  18. } // 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.

Also Check: Arduino : What is Arduino

Updated: 14th July 2017 — 7:58 am

Leave a Reply

Your email address will not be published. Required fields are marked *

Arduino Projects Online © 2017 Frontier Theme