Introduction: Making Rhythms With Arduino

Hello everyone!

Im here with the most "you're-doing-it-wrong" project today. But it works and pleases my ears. And who knows, you might learn something while you do this.

The list of things is small. You need one speaker, one arduino and two cables. Thats all folks. You'll be making music in no time and have a great evening. The speaker could be pretty much any speaker but I suggest you'll take an old one you don't care about. I'm using chinese crap speakers for < $1.

Step 1: Solder Everything to Everything!

You only need one speaker. I use 50 because, well, because Mega. Normally you would care about + and - on your speakers, we do not. We don't care about phase here. Hell, i encourage some phase problems, it sounds nice.

One cable goes to + and one to -. One of those goes to a digitalPin output of your choice. Lets say digi pin 2. The other one goes to GND.

If you have a lot of speakers you connect all the GND in a serie like in my picture.

Thats all the technical stuff.

Step 2: Make Sound!

What is sound? Sound is movement of air that hits your ear. How do you create sound? You make something move to push air, like the speaker membrane. How do we make the speaker membrane move? Shoot some power into it and it will push straight out(or back). Anyway you see it we know that the membrane WILL move if we send it som power. But to create sound we need fluctuations. That means we need to push the air together in intervals. Just pushing the membrane out once just pushes one wave forward, we never hear that, sorry.

This brings us to Hz and frequency. A frequency of 1 Hz means we have a fluctuation/interval of one membrane push and one membrane "inhale"(when the membrane is in the normal position(not pushed(triple parentheses))). The hum from a telephone is 440 Hz and a perfect A4. 440 Hz means one up and one down 440 times a second. Translated into Arduino language it means we need to have one HIGH and one LOW output 440 times in a row each second.

I hope you start to understand the code now.

Because its ultra simple. No kidding.

To create a 440 Hz tone on an arduino you write this in void loop:

int delayYeah

void setup(){
digitalPin(2, OUTPUT);

//Get length of sound wave in microseconds
delayYeah = 1000000 / 440;

//Since one wave equals to one up and one down we divide delayYeah with 2.
delayYeah = delayYeah / 2;
}

void loop() {
//Up!
digitalWrite(2, HIGH);
delayMicroseconds(delayYeah);
//Down!
digitalWrite(2, LOW);
delayMicroseconds(delayYeah);
}

We use microseconds to be as accurate as possible. But it can't be perfect. Sorry again.

Step 3: Make Noise!

But today we don't want any pure and nice notes. We want noise. And what is noise? Noise is a lot if different frequencies played at the same time, roughly said. Because everything you hear is actually a lot of different frequencies played at the same time. But what defines noise? There are many different kinds of noise. We will focus on random noise where the frequencies of the sound played is totally random.

If we don't have regular intervals between our speakers "push" and "inhale"(HIGH and LOW output) the output sound will be irregular and noisy. To accomplish this we do a minor change in our void loop().

Change the previous void loop() too:

void loop(){
digitalWrite(2, HIGH);
delayMicroseconds(random() % 1000);
digitalWrite(2, LOW);
delayMicroseconds(random() % 1000);
}

Step 4: Rythm and Note Length

So we want control. First of we need to decide our tempo. I like 120 beats per minute since it's mainstream cool and danceable. At 120 bpm 1/16 note equals to 125 ms, witch is a nice disco tempo nowadays.

Staying alive is 109 bpm. Use this link to find 1/16 in ms at 109 bpm

http://www.sengpielaudio.com/calculator-bpmtempoti...

Code time! We need to make a loop within our void loop():

void loop(){

unsigned int p = millis();

	while(millis() - p < 125){		
		digitalWrite(2, HIGH);
		delayMicroseconds(random() % 1000);
		digitalWrite(2, LOW);
		delayMicroseconds(random() % 1000);

	}
delay(125);
}


Look at that! What's going on here? P stores the current time in milliseconds before we enter our loop. We then subtract the current time with the start time(P) each loop and tells our loop to go for it if we are below 125 ms. When we looped for ~125 ms we exit the loop and add a delay for 125 ms so we get that silence that is needed to create a nice rhythm.

Thats all folks. Noting more to it.