Tutorial 7: Arduino Melody

Created on: 1 August 2012
Updated on: 14 January 2023

An Arduino melody project for beginners that plays a tune using an Arduino Uno. This very easy tutorial uses only two external components – a resistor and 8Ω loudspeaker. The Arduino plays a short melody on the loudspeaker. The melody sketch loaded to the Arduino is one of the sketches that is built into the Arduino IDE.

The following video shows the finished Arduino melody circuit in action. When the Arduino Uno reset button is pressed, the Arduino melody sketch starts running and plays a short tune on the loudspeaker.

Prerequisites

Complete tutorial 3 - Starting with Arduino and its prerequisites before attempting this tutorial.

Arduino Melody Circuit Components

Besides an Arduino Uno and wire link, you will need:

Qty Part Designator Notes Type
1 100 ohm (brown - black - brown) R1 1/4W, 5% or better Resistors
1 8 ohm loudspeaker SP1 Transducer

If your loudspeaker does not have a connector such as the one shown in the photo below, or the video above, then you will need to use a breadboard and some jumper wires to build the circuit.

The required components for the Arduino melody project are shown in the photo below:

Parts needed for the Arduino melody tutorial
Arduino Melody Circuit Components

Arduino Melody Circuit Diagram

The Arduino melody circuit diagram shows that the positive terminal of the speaker is connected through a 100 ohm resistor to pin 8 of the Arduino Uno. The negative terminal of the speaker is connected to one of the Arduino GND pins.

Arduino melody circuit diagram
Arduino Melody Circuit Diagram

Building the Arduino Melody Circuit

Start by connecting the negative speaker terminal (black wire) to a GND pin on the Arduino. Note that it is not necessary to use a breadboard when using a speaker that is taken from an old PC, as it has a connector that a single core wire can be plugged into. Build the circuit using a breadboard if you do not have a loudspeaker with a similar connector to the one shown in the image.

Connect the negative speaker terminal
Connect the Loudspeaker to GND

Connect a 100 ohm resistor to pin 8 of the Arduino and to the positive terminal of the speaker (red wire).

Connect the resistor
Connect the 100Ω Resistor to the Loudspeaker and Arduino

Load the Arduino Melody Sketch

The sketch to load to the Arduino Uno is one of the sketches that is built into the Arduino IDE. Open it from within the Arduino IDE by selecting File → Examples → 2.Digital → toneMelody from the top Arduino IDE menu.

Load the sketch to the Arduino and the melody plays immediately after loading. To play the melody again, press the reset button on the Arduino.

The sketch plays the melody once and then does nothing afterwards. Pressing the Arduino reset button causes the Arduino board to reset. As a result, the melody sketch starts running from the beginning again. This means that it plays the tune again and then stops.

This project can be found on the Arduino website.