{"id":35,"date":"2025-04-24T07:48:49","date_gmt":"2025-04-24T07:48:49","guid":{"rendered":"http:\/\/mrbenshoof.com\/JeffsPlayground\/?page_id=35"},"modified":"2025-04-25T20:58:08","modified_gmt":"2025-04-25T20:58:08","slug":"stepper-motor-music-mar-2025","status":"publish","type":"page","link":"https:\/\/mrbenshoof.com\/JeffsPlayground\/stepper-motor-music-mar-2025\/","title":{"rendered":"Stepper Motor Music &#8211; Mar 2025"},"content":{"rendered":"\n<p>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 &#8220;Stepper&#8221; library.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"800\" height=\"560\" src=\"http:\/\/mrbenshoof.com\/JeffsPlayground\/wp-content\/uploads\/2025\/04\/IMG_0054.png\" alt=\"\" class=\"wp-image-47\" style=\"width:560px;height:auto\" srcset=\"https:\/\/mrbenshoof.com\/JeffsPlayground\/wp-content\/uploads\/2025\/04\/IMG_0054.png 800w, https:\/\/mrbenshoof.com\/JeffsPlayground\/wp-content\/uploads\/2025\/04\/IMG_0054-300x210.png 300w, https:\/\/mrbenshoof.com\/JeffsPlayground\/wp-content\/uploads\/2025\/04\/IMG_0054-768x538.png 768w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/figure>\n\n\n\n<p>I noticed that the sound the motor made was amplified by the wooden desk &#8211; 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.<\/p>\n\n\n\n<p>Through trial and error I chose notes on the piano, and found what speed (rpm) it took to make the motor match that sound.<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>Using these speeds I made a simple program to &#8216;play&#8217; each note needed for &#8220;Happy Birthday&#8221; in order. <\/p>\n\n\n\n<p>Two issues arose from this method of playing the song:<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>2) The notes seemed to meld from one to the next, it was a little difficult hear distinct note changes.<\/p>\n\n\n\n<p>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# &#8211; requiring 70 rpm &#8211; 70 steps.<\/p>\n\n\n\n<p>The second issue was much simpler, and was solved by just changing the direction the motor moved each note (moving negative steps).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Program<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;Stepper.h&gt;\n\nconst int stepsPerRevolution = 200;\n\nint SongSpeed = 2;\n\n\/\/note speeds\n\/\/double letter = next octave\n\/\/note+S = sharp\nint D = 44;\nint E = 50;\nint F = 53;\nint FS = 55;\nint G = 60;\nint GS = 63;\nint A = 66;\nint AS = 70;\nint B = 74;\nint C = 79;\nint CS = 84;\nint DD = 89;\nint DS = 93;\nint EE = 99;\nint FF = 105;\nint FFS = 110;\n\nStepper myStepper(stepsPerRevolution, 10, 11, 8, 9);\n\nvoid setup() {\n  \/\/ initialize the serial port:\n  Serial.begin(9600);\n}\n\nvoid loop() {\n  \/\/HAP\n  myStepper.setSpeed(D);\n  myStepper.step(SongSpeed * (D \/ 2));\n  \/\/PY\n  myStepper.setSpeed(D);\n  myStepper.step(SongSpeed * (-(D \/ 2)));\n  \/\/BIRTH\n  myStepper.setSpeed(E);\n  myStepper.step(SongSpeed * (E));\n  \/\/DAY\n  myStepper.setSpeed(D);\n  myStepper.step(SongSpeed * (-D));\n  \/\/TO\n  myStepper.setSpeed(G);\n  myStepper.step(SongSpeed * (G));\n  \/\/YOU 1\n  myStepper.setSpeed(FS);\n  myStepper.step(SongSpeed * (-(FS * 2)));\n  \/\/HAP\n  myStepper.setSpeed(D);\n  myStepper.step(SongSpeed * (D \/ 2));\n  \/\/PY\n  myStepper.setSpeed(D);\n  myStepper.step(SongSpeed * (-(D \/ 2)));\n  \/\/BIRTH\n  myStepper.setSpeed(E);\n  myStepper.step(SongSpeed * (E));\n  \/\/DAY\n  myStepper.setSpeed(D);\n  myStepper.step(SongSpeed * (-D));\n  \/\/TO\n  myStepper.setSpeed(A);\n  myStepper.step(SongSpeed * (A));\n  \/\/YOU 2\n  myStepper.setSpeed(G);\n  myStepper.step(SongSpeed * (-(G * 2)));\n  \/\/HAP\n  myStepper.setSpeed(D);\n  myStepper.step(SongSpeed * (D \/ 2));\n  \/\/PY\n  myStepper.setSpeed(D);\n  myStepper.step(SongSpeed * (-(D \/ 2)));\n  \/\/BIRTH\n  myStepper.setSpeed(DD);\n  myStepper.step(SongSpeed * (DD));\n  \/\/DAY\n  myStepper.setSpeed(B);\n  myStepper.step(SongSpeed * (-B));\n  \/\/DEAR\n  myStepper.setSpeed(G);\n  myStepper.step(SongSpeed * (G));\n  \/\/&#91;NAME]\n  myStepper.setSpeed(FS);\n  myStepper.step(SongSpeed * (-FS));\n  \/\/&#91;NAME] 2\n  myStepper.setSpeed(E);\n  myStepper.step(SongSpeed * (E));\n  \/\/HAP\n  myStepper.setSpeed(C);\n  myStepper.step(SongSpeed * (-(C \/ 2)));\n  \/\/PY\n  myStepper.setSpeed(C);\n  myStepper.step(SongSpeed * (C \/ 2));\n  \/\/BIRTH\n  myStepper.setSpeed(B);\n  myStepper.step(SongSpeed * (-B));\n  \/\/DAY\n  myStepper.setSpeed(G);\n  myStepper.step(SongSpeed * (G));\n  \/\/TO\n  myStepper.setSpeed(A);\n  myStepper.step(SongSpeed * (-A));\n  \/\/YOU!\n  myStepper.setSpeed(G);\n  myStepper.step(SongSpeed * (G * 2));\n}<\/code><\/pre>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>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&#8217; 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!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Results<\/h2>\n\n\n\n<p>In hindsight I should&#8217;ve also put in rest notes to give better cadence to the song, however here&#8217;s the final result. The Tupperware is an addition I found increased the volume of the sound.<\/p>\n\n\n\n<figure class=\"wp-block-video\"><video height=\"1080\" style=\"aspect-ratio: 1920 \/ 1080;\" width=\"1920\" controls src=\"http:\/\/mrbenshoof.com\/JeffsPlayground\/wp-content\/uploads\/2025\/04\/IMG_0059.mp4\"><\/video><\/figure>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &#8220;Stepper&#8221; library. I noticed that the sound the motor made [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-35","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/mrbenshoof.com\/JeffsPlayground\/wp-json\/wp\/v2\/pages\/35","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mrbenshoof.com\/JeffsPlayground\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/mrbenshoof.com\/JeffsPlayground\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/mrbenshoof.com\/JeffsPlayground\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mrbenshoof.com\/JeffsPlayground\/wp-json\/wp\/v2\/comments?post=35"}],"version-history":[{"count":5,"href":"https:\/\/mrbenshoof.com\/JeffsPlayground\/wp-json\/wp\/v2\/pages\/35\/revisions"}],"predecessor-version":[{"id":63,"href":"https:\/\/mrbenshoof.com\/JeffsPlayground\/wp-json\/wp\/v2\/pages\/35\/revisions\/63"}],"wp:attachment":[{"href":"https:\/\/mrbenshoof.com\/JeffsPlayground\/wp-json\/wp\/v2\/media?parent=35"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}