Introduction: Make Music With Stepper Motors!

About: Unknown engineer

Have you seen those videos where the music was played by rotating motors?

I have, so i wanted to make my own 'motor music player'.

But when I started to search for some tutorials or information of how to make something like this I realized that i can't find anything! =(
That was really sad, so I had to figure out the entire process by myself. And i succeed, so today I'll show you how YOU can make music with stepper motors!

Step 1: Motors

First, you need a stepper motor. You can find it in printers, scanners, floppy drives and some other devices.
You can recognize them by many wires going into the motor, but you need the only one with 4.

Step 2: Materials

Beside the motor you need a bunch of other materials. Here they are:

- A4988 driver

- Arduino board (i used uno)

- 9-12v power supply

- Breadboard

- A bunch of wires to connect everything together

- 47mkF capacitor

Step 3: Assembling the Driver

We are going to control stepper motor with a4988 driver, so check this video for more information:
You need to assemble everything together like in the video, but in case i use single power supply here are my wiring diagrams.

Step 4: Connect Motors

Before connecting motors you need to check the terminals with multimeter and recognize two pairs of shorted wires, each pair for each coil in the motor.

Only then connect them to the circuit.

Step 5: What About Notes?

Yeah, we are almost there.

As you can see from the video we are controlling motors by continuously sending pulses to the driver, and the speed of rotation depends on duration of the pause between them (remember it!).

Now let's move to the musical part of our trip.

As you know, every note has its own frequency which you can find in a special table shown in the picture. Knowing the frequency we can calculate the period, which is 1/f (f - frequency).

And here the main trick comes. Remember the duration of a pause between pulses? This pause is actually a period of the note you hear from rotating. Very exciting, isn't it?

Step 6: Code

Now we can simply control the period to get notes we want from our stepper motor.

And here my 'genius' code comes.

// defines pins numbers
const int stepPin = 3; 
const int dirPin = 4; 

// here comes a bunch of 'useful' vars; dont mind
int coun;
bool dir=0;
int del;
int c=1912;
int cf=1805;
int d= 1703;
int df=1607;
int e=1517;
int f=1431;
int ff=1351;
int g= 1275;
int gf=1203;
int a=1136;
int af=1072;
int b=1012;
int c1=floor(c/2);
int cf1=floor(cf/2);
int d1=floor(d/2);
int df1=floor(df/2);
int e1=floor(e/2);
int f1=floor(1431/2);
int ff1=floor(1351/2);
int g1= floor(1275/2);
int gf1=floor(1203/2);
int a1=floor(1136/2);
int af1=floor(1072/2);
int b1=floor(1012/2);
int e0=e*2;
int g0=g*2;
int b0=b*2;
int af0=af*2;
int a0=a*2;
int f0=f*2;
int use=180;
int tempo=120;
int oct=5;

void setup() {
  // Sets the two pins as Outputs
  pinMode(stepPin,OUTPUT); 
  pinMode(dirPin,OUTPUT);
}
void loop() {
  oct=5;
// Here you put your music code

}

void note(int num,long dur) {
  del=(num*oct)/10;
  dir=!dir;
 digitalWrite(dirPin,dir);
  coun=floor((dur*5*tempo)/del);
  for(int x = 0; x < coun; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(del);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(del);
  }

}

void pa(int durp){
  int ker=floor(durp/100)*tempo;
delay(ker);
  
  }
  
  

Now use these functions to create MuSiC:

- note(num,dur)

num - the note you want to play (variable that contains the value of period), the list of them is at the top of this sketch.
dur - duration of the note in milliseconds (1000 = 1 sec)

- pa(durp) (pause between notes)

durp - duration of pause in milliseconds


Also use the 'tempo' var to change the tempo.


That is all!

Step 7: Final Result

This is how the thing sounds.

And here is one of my 'music codes'

note(e1,250);
note(e1,500);
note(e1,250);
pa(250);
note(c1,250);
note(e1,500);
note(g1,1000);
note(g,1000);
note(c1,500);
pa(250);
note(g,250);
pa(500);
note(e,500);
pa(250);
note(a,250);
pa(250);
note(b,250);
pa(250);
note(af,250);
note(a,500);
note(g,330);
note(e1,330);
note(g1,330);
note(a1,500);
note(f1,250);
note(g1,250);
pa(250);
note(e1,250);
pa(250);
note(c1,250);
note(d1,250);
note(b,250);
pa(1000);

Rob scallon Rain.

for (int i = 0; i < 3; i++){
note(d,100); pa(use); note(f,100); pa(use); note(c1,100); pa(use); note(f,100); pa(use);} note(c1,100); pa(use); note(c1,100); pa(use); note(af,100); pa(use); note(a,100); pa(use); for (int i = 0; i < 3; i++){ note(c,100); pa(use); note(e,100); pa(use); note(af,100); pa(use); note(e,100); pa(use);} note(af,100); pa(use); note(af,100); pa(use); note(a,100); pa(use); note(f,100); pa(use); for (int i = 0; i < 3; i++){ note(d,100); pa(use); note(f,100); pa(use); note(af,100); pa(use); note(f,100); pa(use);} for (int i = 0; i < 3; i++){ note(af0,100); pa(use); note(d,100); pa(use); note(f,100); pa(use); note(a,100); pa(use);}

Try it!

Step 8: Conclusion

We made it through! Now you know how you can make music with stepper motors!
I just gave you the main information, now is your turn to create!

I hope you enjoyed, and good luck! Thanks for reading!


P.S. A single motor may sound quite, so try to stick it to some surface and it become loud!

=)

Instrument Contest

Participated in the
Instrument Contest