Stepper Motor Music

Hello! I am using an Arduino Mega 2560 and a 28byj-48 stepper motor and was wondering, "How do I make the stepper motor buzzing noise from my 28byj-48 stepper motor match the frequency of the notes on the piano?" For instance, if I were to set the stepper at 1500 steps and a speed of 23 RPM what is the frequency (noise occurrence) of the stepper motor? I then want to compare this frequency to the frequency of the piano notes. But, how do I do that? Or maybe does the measurement have to be on a decibel (db) scale for the stepper motor buzzing noise? Is there a type of software that I could use for the stepper motor buzz to get a frequency in return? Here is the software side of the test program (hardware & datasheet are below as well):

 Stepper Motor Control - one revolution

 This program drives a unipolar or bipolar stepper motor.
 The motor is attached to digital pins 8 - 11 of the Arduino.

 The motor should revolve one revolution in one direction, then
 one revolution in the other direction.


#include <Stepper.h>

const int stepsPerRevolution = 1500;  // change this to fit the number of steps per revolution

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11);

void setup() {
  // set the speed at 23 rpm:
  // initialize the serial port:

void loop() {
  // step one revolution  in one direction:

  // step one revolution in the other direction:

Also, the specifications for this stepper motor say that the stepper motor operates at 100Hz, is that the buzzing noise? Does the buzzing pitch change with different steps? Oh, by the way, I am going to use the pitches.h library to make music with the stepper motor AND possibly play in on a speaker, making run at the same time, I just don't know how to make the code yet. Is playing music on a speaker AND have the stepper motor go with the song even possible? I am also using the ULN2003 stepper motor controller, which is also linked in the datasheet/hardware. By the way, to get to the specific less I am on go to the link, tap on the link, and then, once you get on the website, click "Lesson 31 Stepper Motor." Yes, you can literally click on the lesson and it will automatically take you there. Pretty handy feature. Anyway, make sure to read through that, please. Oh, and the song I want to play is in a MIDI file listed below and the link to the sheet music will be listed below as well.

Datasheet and hardware link

Mario sheet music

As far as I know the step rate is the frequency that you hear. I have used the tone() function to do music on steppers. I used NEMA 17 bipolar steppers and DRV8825 drivers. I don't know what you have cause I do not want to dig through that PDF to find out.

How would I use the Tone Library in order to control the stepper motor to the music or notes? Or how do I use the tone(); function to do music on my stepper motor?
Here I made some code to try out the tone library and the stepper motor, the stepper buzzes like it is supposed to, but doesn't make a sound, in fact, when I lay the stepper motor on my desk, it makes no sound. Like, when I pick up the stepper motor I feel the shaft buzzing, but it makes no sound:

//By UptownKitten453 and bhagman

#include <Tone.h>

Tone SMPin[4];     //state how many pins there are

int notes[7] = { NOTE_A3,     //initialize the notes
                NOTE_G4 };

void setup() {
  SMPin[0].begin(8);   //initialize the pins

void loop() {

SMPin[0].play(notes[NOTE_A3]); //play a note


It is traditional to use old 5 inch floppy drives which conveniently include the stepper driver and a sounding board.

I have a supply in my garage awaiting this very application. :grinning:

It's not like I can go to your garage to get a floppy drive AND I don't want to buy one. I am just simply trying to use a 28byj-48 stepper motor along with a ULN2003 stepper motor controller to make music, I am trying to use the parts I already have and not parts I don't have. And, who has floppy drives anymore, I mean, seriously. This is for educational purposes, I need to learn how to do this, so, please someone tell me how to use MY PARTS and NOT CHEAT by using the code above in #2 and how to fix the code, because I have no idea at this point on how to fix the code.

Mmmm. Looks like your avatar is apt! :grinning:

The motor you have may not be suitable for making music.

It was designed to control air vents in cheap equipment.

Ok, then let's suppose I have a NEMA 17 bipolar stepper motor and a L298N stepper controller, then, how would I use the tone library to make music with the stepper motor? How can I fix my code above (in #2) to configure to this stepper motor? (Hardware is in the link along with the datasheet).

NEMA 17 Bipolar Stepper Datasheet

L298N tutorial

The motor you have may not be suitable for making music.

It was designed to control air vents in cheap equipment.

You mean it is too wimpy and not noisy enough? :roll_eyes:

Mmmm. You may be right!

I haven't played with it for years!

How would I use the Tone Library in order to control the stepper motor to the music or notes? Or how do I use the tone(); function to do music on my stepper motor?

Most stepper motors are bipolar, and operated through a stepper controller board, which makes one step for every pulse on the STEP pin. That's where the tone() library comes in: it produces that pulse, making the stepper step at the frequency you set tone() to produce.

If you operate the 28byj-48 stepper motor through a ULN2003 it doesn't work that easily, as you make steps in a different way. This is going to take a bit more programming. You may actually be able to modify the tone() library (copy the relevant parts of the code into your own sketch) to make that stepper step instead of just setting a pin high or low.

So you need a bipolar stepper to make this work, or wire the 28byj-48 to a stepper board, without connecting the common wire.

Indeed it is not exactly a particularly noisy stepper, mostly as it's so tiny and light weight. Most NEMA17 size steppers will have a lot more bulk, and will be much noisier. Especially when stepping in full steps, which is what you should be doing here.

Star Wars March on eight floppy drives.

@wvmarle may you, please, explain more on what you mean about making the stepper step at the frequency that I set tone() to produce. Like, I don't understand how to actually make the stepper step at that frequency, to match the piano frequency. Like, how do I code the stepper to step at the frequency of a piano note, is what I am trying to ask. I don't understand how to code that, that's what I was asking help for. I think instead I'll use a NEMA 17 bipolar stepper motor and a L298N stepper driver, along with a 12V 5A power supply/adapter. Like I said in #7.

The ancient, inefficient and weak L289N is totally suitable for modern, low impedance stepper motors.

Pololu has an excellent selection of modern, efficient stepper motor drivers.

Please, explain how the L289N is "ancient, inefficient and weak."

The L298 was designed well over 20 years ago, using old, bipolar transistor technology. It can handle only 1 Ampere/channel continuously, and internally, drops about 4 V per channel when saturated, causing it to overheat and shut down.

It cannot perform current limiting as required by modern, low impedance stepper motors.

drops about 4 V per channel when saturated,

So, how come my stepper motors still work if they drop so much voltage?

If your motor does 23 rpm and it does 1500 steps per revolution, that's 34500 steps per minute or 575 steps per second. You should hear a 575 Hz tone. If you don't, you have a very smooth stepper motor. Put a piece of stiff plastic sheet on the axis. It might amplify the sound.
If you manage to produce the 575 Hz sound, you have the ratio. 23 rpm makes 575 Hz. That's a linear ratio.
And to calculate what tone 575 Hz is, do:
575 Hz / 442 Hz = 1.30
2x/12 = 1.30
x = 12 * ln(1.3) / ln(2)
x = 4.542
Your tone is 4.542 semitones above A4, which is between C#5 and D5. Or 45.8 cent below D5.

More sophisticated stepper motor based music.

So, how come my stepper motors still work if they drop so much voltage?

12V-4V=8V. That's more than enough for a stepper.

Fair chance that the limits of what that H-bridge physically can conduct is keeping the overall current in check.

May someone, please, explain #16? I got like half of what that person was talking about, plus I thought that stepper motors (NEMA 17 bipolar) needed EXACTLY 12V. My point is, how do I code any of this?