I love to learn about new tools via hands on experience. I decided I wanted to learn how to control a stepper motor with an Arduino. I was pretty new to stepper motors but eventually I figured it out using the Arduino IDE built in “Stepper” library.

I noticed that the sound the motor made was amplified by the wooden desk – and that it also made a distinct note! Using a piano I found that note to be G. Making the motor play a song was my next goal.
Through trial and error I chose notes on the piano, and found what speed (rpm) it took to make the motor match that sound.
I did find that as I went lower (slower) the note became quieter and less audible, and same with going too high (faster), so I only found speeds for notes D3 through F#4.
Using these speeds I made a simple program to ‘play’ each note needed for “Happy Birthday” in order.
Two issues arose from this method of playing the song:
1) Each note was played by telling the motor to move to a fixed number of steps (200 per rotation), and when each note does this at different speeds, the notes are different lengths.
2) The notes seemed to meld from one to the next, it was a little difficult hear distinct note changes.
The first issue required setting the amount of steps the motor needed to move for a note, to be proportional to the speed for that note. For example G is played at 60 rpm, so moving the motor 60 steps has the same duration of playing A# – requiring 70 rpm – 70 steps.
The second issue was much simpler, and was solved by just changing the direction the motor moved each note (moving negative steps).
Program
#include <Stepper.h>
const int stepsPerRevolution = 200;
int SongSpeed = 2;
//note speeds
//double letter = next octave
//note+S = sharp
int D = 44;
int E = 50;
int F = 53;
int FS = 55;
int G = 60;
int GS = 63;
int A = 66;
int AS = 70;
int B = 74;
int C = 79;
int CS = 84;
int DD = 89;
int DS = 93;
int EE = 99;
int FF = 105;
int FFS = 110;
Stepper myStepper(stepsPerRevolution, 10, 11, 8, 9);
void setup() {
// initialize the serial port:
Serial.begin(9600);
}
void loop() {
//HAP
myStepper.setSpeed(D);
myStepper.step(SongSpeed * (D / 2));
//PY
myStepper.setSpeed(D);
myStepper.step(SongSpeed * (-(D / 2)));
//BIRTH
myStepper.setSpeed(E);
myStepper.step(SongSpeed * (E));
//DAY
myStepper.setSpeed(D);
myStepper.step(SongSpeed * (-D));
//TO
myStepper.setSpeed(G);
myStepper.step(SongSpeed * (G));
//YOU 1
myStepper.setSpeed(FS);
myStepper.step(SongSpeed * (-(FS * 2)));
//HAP
myStepper.setSpeed(D);
myStepper.step(SongSpeed * (D / 2));
//PY
myStepper.setSpeed(D);
myStepper.step(SongSpeed * (-(D / 2)));
//BIRTH
myStepper.setSpeed(E);
myStepper.step(SongSpeed * (E));
//DAY
myStepper.setSpeed(D);
myStepper.step(SongSpeed * (-D));
//TO
myStepper.setSpeed(A);
myStepper.step(SongSpeed * (A));
//YOU 2
myStepper.setSpeed(G);
myStepper.step(SongSpeed * (-(G * 2)));
//HAP
myStepper.setSpeed(D);
myStepper.step(SongSpeed * (D / 2));
//PY
myStepper.setSpeed(D);
myStepper.step(SongSpeed * (-(D / 2)));
//BIRTH
myStepper.setSpeed(DD);
myStepper.step(SongSpeed * (DD));
//DAY
myStepper.setSpeed(B);
myStepper.step(SongSpeed * (-B));
//DEAR
myStepper.setSpeed(G);
myStepper.step(SongSpeed * (G));
//[NAME]
myStepper.setSpeed(FS);
myStepper.step(SongSpeed * (-FS));
//[NAME] 2
myStepper.setSpeed(E);
myStepper.step(SongSpeed * (E));
//HAP
myStepper.setSpeed(C);
myStepper.step(SongSpeed * (-(C / 2)));
//PY
myStepper.setSpeed(C);
myStepper.step(SongSpeed * (C / 2));
//BIRTH
myStepper.setSpeed(B);
myStepper.step(SongSpeed * (-B));
//DAY
myStepper.setSpeed(G);
myStepper.step(SongSpeed * (G));
//TO
myStepper.setSpeed(A);
myStepper.step(SongSpeed * (-A));
//YOU!
myStepper.setSpeed(G);
myStepper.step(SongSpeed * (G * 2));
}
The SongSpeed variable, simply multiplies the duration of the notes to make the song faster or slower. Each note is a half note, and then multiplied or divided by 2 to make whole and half notes respectively.
One quick cool thing to note, is that the ratios of the speeds from one note to the next sequentially, is pretty comparable to the ratios of those notes’ known frequencies. The ratio of a 440 Hz A to a B at 493.8 Hz is ~ 0.8910. My A at 66 rpm and B at 74 rpm is 0.8918!
Results
In hindsight I should’ve also put in rest notes to give better cadence to the song, however here’s the final result. The Tupperware is an addition I found increased the volume of the sound.
The timbre of the motor definitely makes it sound out of tune, but its the best I could manage. In the future I hope to make more songs, even if I have a limited range of notes available.
I loved this project, and it was a fun and interactive way to learn how to control a stepper motor with an Arduino. In the future I hope to make more songs, I think it would be cool to do several at once and make them harmonize.