You can use this little Arduino musical tune generator circuit for a preferred application, such as for making an interesting door bell, as a car reverse horn, or music box for gifting somebody, or simply for personal amusement.

Hardware Required

The Hardware required for the project are given as follows:

  • Arduino or Genuino board

  • piezo buzzer or a speaker

  • hook-up wires

The Arduino is powered with a 9V, 500mA power supply input which could be from any standard SMPS AC to DC adapter, or you can also try your cell phone charger for the same.

Pin#8 from the Arduino can be directly configured with the speaker which must not be rated above 8 ohm, and 1 watt.

So one wire of the speaker connects with pin#8 of the Arduino board and the other wire goes to the negative line or the ground line of the board.

Circuit for Arduino Musical Tune Generator

For Amplified Output

For louder or hugely amplified sound you can configure the pin#8 with a transistor driver stage, consisting of a TIP31 transistor, whose base may be connected with pin8 via a 1K resistor, emitter to ground and the collector to one of the wires of the speaker, the other wire of the spaker now connects with the positive supply that is the 9V supply (+).

Here make sure the speaker is rated at 8 ohms but at much higher wattage, may be at around 5 watts for an amplified music tune generation.

This sketch is coded to play and generate quite many random
melodies in sequence using a pentatonic scale


Plays a (fairly) random tune until the program is stopped.

8-ohm speaker on digital pin 8.

//Copyright (c) 2012 Jeremy Fonte

//This code is released under the MIT license



int randomNote = 131;

int randomDuration = 2;

int noteStep = 1;

int notes[15];

void setup() {

pinMode(8, OUTPUT);

notes[1] = 131;

notes[2] = 147;

notes[3] = 165;

notes[4] = 196;

notes[5] = 220;

notes[6] = 262;

notes[7] = 294;

notes[8] = 330;

notes[9] = 392;

notes[10] = 440;

notes[11] = 523;

notes[12] = 587;

notes[13] = 659;

notes[14] = 784;

notes[15] = 880;

randomNote = random(1, 15);


void loop() {

noteStep = random(-3, 3);

randomNote = randomNote + noteStep;

if(randomNote < 1) {

randomNote = random(1, 15);


else if(randomNote > 15) {

randomNote = random(1, 15);


randomDuration = random(1, 8);

// 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 = 1000/randomDuration;

tone(8, notes[randomNote],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;


// stop the tone playing:



The connection diagram for the proposed Arduino musical tune generator circuit is shown below:

connection diagram for the proposed Arduino musical tune generator circuit

For high power amplified listening, the same set up can be upgraded with a power transistor as indicated in the following figure:

Need Help? Please send your queries through Comments for quick replies!