Starting Electronics Needs Your Help!
It is that time of the year when we need to pay for web hosting and buy new components and equipment for new tutorials. You can help by making a donation. Contribute to this website by clicking the Donate button. The total will be updated once daily. (You may need to clear your browser cache to see the updates.)

Target Amount: $2000
Amount Raised: $432

Donations Received

Top Donor: D.C. $30

X

Tune Replay Arduino Project for Beginners

Created on: 5 April 2016

An Arduino Project that plays music. In this easy Arduino project for beginners, a push-button switch is connected to Arduino pin 2 and a loudspeaker or piezo disc is connected to pin 8. When the Arduino is powered up or reset, a tune is played. If the push-button switch is pressed, the tune is replayed.

The video below demonstrates the project using a speaker and a piezo disc. The speaker is much louder than the piezo disc.

Can't see the video? View on YouTube →

Building the Tune Replay Project

Hardware Requirements

The following parts are needed to build the project.

  • Arduino Uno or similar Arduino board
  • Piezo disk (not the buzzer type); OR 8 ohm loudspeaker
  • 10uF capacitor if using a loudspeaker
  • Push-button switch, or just a piece of wire will do if you don't have a switch
  • Electronic breadboard and jumper wires

A loudspeaker will be much louder that a piezo disk. If you do use a piezo disk, make sure that it is not the buzzer type that has built-in electronics to make it buzz when power is applied. Plain piezo disks are also supplied in a plastic housing which make them look more like the buzzer type, so check the details before buying.

Amazon.com

Amazon.co.uk

Breadboard Layout and Circuit Diagram

Two versions of the circuit can be built – the loudspeaker circuit and the piezo circuit. Breadboard layout and circuit diagrams are provided for both below.

Both versions of the circuit connect a push-button switch to digital pin 2 of the Arduino and the sound device to pin 8 of the Arduino. The Arduino sketch that follows is used for either circuit.

It is not necessary to connect a pull-up resistor to the push-button switch because the internal pull-up resistor is enabled by the Arduino sketch.

Circuit with Loudspeaker

A 10uF capacitor is connected in series with the loudspeaker. This capacitor is a d.c. blocking capacitor that will prevent the loudspeaker from drawing too much current from the Arduino pin should the pin be left in a high state.

Tune Replay Loudspeaker Project Breadboard Layout

Tune Replay Loudspeaker Project Breadboard Layout

Tune Replay Loudspeaker Project Circuit Diagram

Tune Replay Loudspeaker Project Circuit Diagram

Circuit with Piezo Disk

No series capacitor or resistor is needed when using a piezo disc. The piezo disk is more like a capacitor than a resistive element. It has a high resistance and won't draw too much current from the Arduino pin.

Tune Replay Piezo Project Breadboard Layout

Tune Replay Piezo Project Breadboard Layout

Tune Replay Piezo Project Circuit Diagram

Tune Replay Piezo Project Circuit Diagram

Arduino Tune Replay Sketch Code

After connecting the hardware as shown above, copy the following sketch and paste it to a new window in the Arduino IDE. Connect a USB cable from the computer to the Arduino and load the sketch to the Arduino.

When the sketch has finished loading for the first time, the tune will play. Press the button to make the tune play again.

The tune programmed into this sketch is from a guitar study by Sor.

//-----------------------------------------------------------------------------------------
//  Program:      tune_replay
//
//  Description:  Plays a tune when Arduino is first powered up or programmed. Replays the
//                tune if the push-button switch is pressed.
//
//  References:   Adapted from the Arduino examples toneMelody sketch by Tom Igoe
//                http://www.arduino.cc/en/Tutorial/Tone
//
//  Date:         5 April 2016      Author: W.A. Smith
//                http://startingelectronics.org
//
//  Project Page:
//  http://startingelectronics.org/beginners/arduino-projects-for-beginners/tune-replay/
//-----------------------------------------------------------------------------------------
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494

// notes in the melody: from guitar study by Sor
int melody[] = {
  NOTE_G3, NOTE_G3, NOTE_E3, NOTE_C4, NOTE_B3, NOTE_B3, NOTE_A3, NOTE_D4, NOTE_C4, NOTE_C4, NOTE_B3, NOTE_G4, NOTE_F4,
  NOTE_F4, NOTE_E4, NOTE_C4, NOTE_A3, NOTE_G3, NOTE_E3, NOTE_C4, NOTE_B3, NOTE_B3, NOTE_A3, NOTE_D4, NOTE_C4, NOTE_C4,
  NOTE_B3, NOTE_E4, NOTE_D4, NOTE_C4
};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 2
};

void setup() {
  pinMode(2, INPUT_PULLUP); // push-button switch on pin 2, use internal pull-up resistor
  
  PlayTune();               // play tune at start-up
}

void loop() {
  // play tune again if button on digital pin 2 is pressed
  if (!digitalRead(2)) {
    PlayTune();
  }
}

// a function that plays the tune
void PlayTune()
{
  // iterate over the notes of the melody:
  for (int thisNote = 0; thisNote < 30; thisNote++) {

    // to calculate the note duration, take one second
    // divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1500 / noteDurations[thisNote];
    tone(8, melody[thisNote], noteDuration);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(8);
  }
}

References

The code in the above sketch was adapted from the toneMelody Arduino example sketch from the Arduino IDE.

The toneMelody sketch can be found in the Arduino IDE under File → Examples → 02.Digital → toneMelody.











Payoneer