Introduction: Arduino Random Music Generator

Picture of Arduino Random Music Generator

This was one of my very first Arduino projects and is the very definition of simplicity.

Where I live, there are no stores to buy electronic parts, and Amazon and eBay have their own demerits. So whenever I made an Arduino project, I salvaged all the parts from broken things.

This is a beginner level project, which can be tweaked by someone who has never even picked up an Arduino.

Also, this is my entry in the 'Make Noise' contest, so please do vote!

Step 1: Materials

Picture of Materials

For this project, you will need



• (x1) Arduino Uno (This is the one I personally recommend for beginners, but any Arduino would be OK)
• (x2) Male to Male/Male to Female jumper cables
• (x1) Piezo buzzer or speaker (I salvaged my own)
• Optional chassis, I didn't use one.
• Soldering Iron, Wire cutters, etc.


You will also need a computer.

Step 2: Salvaging the Speaker

Picture of Salvaging the Speaker

I got this speaker for about $8, and it's not too bad. I could have connected an audio jack input to the Arduino, and then the speaker using an aux cable, but I decided it would be simpler just to break it apart and take the component I need.

After breaking apart the speaker, I needed to solder the jumper cables on, so I did just that. I won't go into much detail over here, but all you need is the speaker itself, connected to two wires on the positive and negative end.

Now let's move on to the actual project, and make some noise.

Step 3: Arduino IDE

Picture of Arduino IDE

Start off by installing the Arduino IDE available here.

Before starting to code you have to select the option in the Tools >> Board menu that corresponds to your Arduino board. For purposes of this project, I'm gonna consider the UNO.

Next, select the serial device of the board from the Tools >> Serial Port menu. Most of the time this is COM3 or higher. You can disconnect the board and reconnect, and the option that appears would be your port. Select the port.

Open a new file, and crack your knuckles. The toughest part of this project is coming.

Step 4: The Code

Picture of The Code

One thing I hate about a lot of 'ibles is that the code is never explained. So the person reading would just copy/paste. To avoid that I am going to give a general outline of the code. Furthermore, there are more specific explanations in the attached code itself.

What I essentially do is create random values for the frequency and duration of notes, and play them out. So I setup pin 10 as the output and create values for all the notes used. I then put them in an array, and have a random function call out the various notes.

Here is the sketch (another word for code in Arduino-speak :D )

void setup() {
pinMode(10,OUTPUT); // This code runs only once. It assigns pin 10 as output, and this is where we'll connect out speaker.
randomSeed(analogRead(A0)); //This creates a random 'seed'. An initial point for the randomization to start.
//The value of this seed is from Analog input 0, which is empty. Hence it generates a random noise value.
}
int sp = 10; //Creates a variable to hold the value of the speakerPin.
int c = 523;
int d = 587;
int e = 659;
int f = 698; //I have assigned the values of one octet of notes to 8 variables
int g = 784;
int a = 880;
int b = 988;
int c2 = 1047;
void loop() { //This is the main code, and it runs repeatedly.
int notes[] = {c,d,e,g,a,c2,random(33,102),random(200,4978)}; //Creates an array of all notes used; A sort of dictionary which is indexed by numbers 0 to 7
//I added 2 random notes here to add some spice to the tonality. The notes I chose belong to a pentatonic scale.
int i = random(0,8); //Generates a random value between 0 & 8, the maximum and minimum indexes of the notes
delay(150); //Creates a delay of 150 ms between each note, so it can be heard distinctly.
tone(sp,notes[i],80); //And finally, the tone function generates a tone sent to pin 'sp',of a random frequency from index i.
//The length of each note chosen is 80.
}

Step 5: Conclusion

Picture of Conclusion

Connect the positive end of the speaker to pin 10, and the negative end to GND. Connect the USB cable, upload your sketch, and your random music maker is ready!

If you want to, change the scale (ie. The notes is the array notes[ ]), and timing, and switch it up. You could also research a music generating algorithm and implement it. I did that in another version.

I hope you enjoyed this simple introduction to the world of Arduino, and more importantly, coding.

Cheers!

Comments

LabRatMatt (author)2017-02-06

neat project! I'm starting to get into the world of Arduino and this seems like an interesting project. If I get time this week, I might take it a step further and play chords with it!

zwarag made it! (author)2017-02-03

Simple and a lot of fun. Thanks for sharing!

NajiK3 (author)2017-02-01

THANKS

feffendi (author)2017-01-31

Well, what is "random". Is that variable, method or what?
I have read the whole program but found nothing (d*mn) . Good job for your effort though (:

feffendi (author)feffendi2017-01-31

EDIT: its an array :(
Sorry

About This Instructable

2,849views

37favorites

License:

Bio: Simplicity is the ultimate sophistication.
More by adithyashok:Arduino Random Music GeneratorSpinning Earth - Beginners Unity Project Easy Designs - Turtle Graphics Python
Add instructable to: