Make Music With Stepper Motors!

by Whiteshadow11 in Circuits > Arduino

20079 Views, 29 Favorites, 0 Comments

Make Music With Stepper Motors!

пп.jpg

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!

Motors

IMG_4327.JPG
IMG_4329.JPG
IMG_4328.JPG
HTB1KT6SilHH8KJjy0Fbq6AqlpXac.jpg
IMG_4330.JPG

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.

Materials

IMG_4320.JPG
IMG_4331.JPG
IMG_4319.JPG
IMG_4324.JPG
IMG_4321.JPG
IMG_4323.JPG

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

Assembling the Driver

ugjghc.png
Untitled Sketch_bb.png
IMG_4333.JPG
IMG_4326.JPG
IMG_4334.JPG
IMG_4335.JPG
IMG_4336.JPG
IMG_4339.JPG

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.

Connect Motors

IMG_4332.JPG
IMG_4337.JPG
IMG_4338.JPG

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.

What About Notes?

note-frequencies.png

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?

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!

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!

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!

=)