Introduction: Automatic Guitar From the Guts of a 3D Printer
For this Instructable, I wanted to see if I could "misuse" some 3D printer electronics that I had laying around to make a musical instrument.
I had a good power supply, a few half-height NEMA 17 steppers and a 3D printer board that had bad heater control circuitry but good stepper drivers. I also had a large plywood board and some fishing line.
The idea was simple. My "instrument" would use the stepper motors to tension or loosen the strings to different pitches. By using a microphone I could pick up the sound of a plucked string and the computer could analyse that to adjust each strings to the correct pitch.
Once it had the tuning for each string, it could varying the tension on the fly to change notes. It would be a curious case of using the GCODE not for making objects in 3D, but instead to make music. Could it possibly work?
In this Instructable I will show you how I built my prototype, explain how to customize Marlin for it and give a run down of the software I developed for it (a work-in-progress at this point, but already usable).
Step 1: Gather the Materials
For this project, I used:
- A piece of plywood
- A power supply from a 3D printer
- A controller board from a 3D printer
- A power cord
- Several NEMA 17 steppers (one for each string)
- Some monofilament fishing line
- Wires for the NEMA 17 stepper motors
- Some mounting screws and nuts.
- Some 3D printed parts
The tools necessary for this project are fairly minimal, just a drill and a screwdriver. You will also need a (working) 3D printer if you want to print out plastic parts in the design (but you could also improvise).
Step 2: Mount the Electronics to a Board
The assembly is straightforward. You need to decide where you want everything to go, drill holes for the mounting screws, and then mount everything into place.
There were a few choices I made in my build that I would have done differently, so here are some suggestions. At first, I had planned to mount five motors in line, so I used a very tall board. It turns out that staggering the motors keeps the strings closer together. By the time I realized this I had put all my motors at the top and made the board top heavy. I suggest using a shorter board and mounting the motors as low as possible on it.
The second mistake I made was that I mounted my motors touching each other. Without any separation, they get really hot. I recommend separating them out a bit so there is some space for air to flow.
If you are using an open-case power AC to DC supply, you will need to attach a power cord to the appropriate terminals using crimp-on terminal lugs. This part involves wiring up 120V AC, so if you don't feel comfortable with this, a better choice would be a controller board that uses a DC power jack and an external power brick, or perhaps a bench top power DC supply.
Whatever you use for your power supply, factor in the fact that each stepper motor will draw approximately 1-2 amps at either 12V or 24V (depending on the specifications for your controller board). However, the energy use will be far less than an actual 3D printer, since there will be no heaters or fans to run.
Step 3: Print the Plastic Parts
A few parts I designed to be 3D printed, they are:
- The bridge that holds up the strings on either side (you need two of these).
- The tuning pegs that hug the stepper motor axis and allow you to tie a string (one for each string).
- The L bracket which holds the board up (one or two).
This parts aren't very sophisticated, so if you don't have a 3D printer, you can probably make them out of wood.
One of the bridges will go on the far end of the board, while the other will go on the end nearest the motors. Secure them with two M3 bolts and nuts.
The L brackets will need to be attached to the bottom edge of the board to act as a stand.
Step 4: Stringing It All Up
For the string, I used 25 lb strength monofilament fishing line from Walmart. Although you could use "real" guitar or ukulele strings, suspect it won't make much difference and you'll end up spending more. On one end of the string, tie the string to a tuning peg and then push the peg on the shaft of the motor axle. On the other end, make a loop and attach it to the bridge with M3 screws. Don't worry if the strings are loose, once the motors are energized, they will take up the slack.
One thing I didn't understand at first is why a real guitar or ukelele is strung up using different diameters of string. I also did not understand why each string was tuned to a different note. If you want to know why, read on, otherwise you may skip the following explanation.
Some Harping on the Strings (for those who care)...
In order to get a string to vibrate at a given frequency (what musicians call the pitch), you can vary one of three quantities:
- The length of the vibrating portion of string -- a shorter length will vibrate at a higher frequency than a longer length.
- The tension on the string -- the more taut it is, the higher the frequency of vibration.
- The diameter of the string -- the thicker the string, the lower the frequency of vibration.
Most musical instruments allow the player to vary one of these quantities while keeping the others constant. Of these, practically all stringed instruments allow the player to vary the length of the vibrating portion of string. In a guitar, for example, you press against the frets of the fingerboard to change the length of the vibrating portion of string and hence the pitch.
In a guitar, each different string set to a different "default "note. You can still vary that note by changing the length of the vibrating portion of the strings using the frets, but the positions of the notes are "shifted" along each string so that the notes of a chord are kept as close as possible together, right across the fingerboard, for easier one handed play.
The different tuning of the strings also explain why different diameters are used for each string. For comfort, it is better to have all the strings "feel" about the same. If all strings were the same diameter and length, the ones tuned to higher note would need to be at significantly higher tension than those for the low notes -- the high strings would feel tight while the low strings would feel loose and flaccid. To avoid having such a different feel, different diameters are used to bring the required tensions closer together.
So, in a guitar, you use a specific tuning for each string and different diameters for comfort and by convention.
In my case, I will be throwing these conventions aside. Rather than using a fingerboard to vary the length of the strings, I will change pitch by having a stepper motor vary the tension of each string. Rather than using different diameter strings, I will use the same diameter for all the strings.
I ended up using 25 lb strength monofilament fishing line -- the cheap stuff from Walmart. Quality or cost is not necessarily an advantage here. I began by using a more expensive braided line, but it didn't work nearly as well as the cheaper stuff.
Step 5: How to Fool Marlin Into Driving a Not-A-Printer
Like in a 3D printer, I used Marlin to control the electronics. The advantage of using Marlin is that it is pretty much guaranteed to work with whatever 3D printer controller you might have. Marlin also does all the hard work of driving the stepper drivers, which is all I really need for this project.
The thing is that Marlin expects to be in a 3D printer, not in some souped up Franken-guitar. To keep Marlin happy, we will need to do the following things:
- Turn off some safeguards to keep Marlin from freaking out about temperatures.
- Make sure Marlin interprets the lack of endstops as untriggered endstops.
- Set stepping so we can get the finest control over stepper motion.
- Disable all inactivity timers.
There is no need for code changes, but we will have to edit some configuration files and recompile. The good news is that the configuration is far easier than it would be for a real printer, since we get to ignore a lot. Just download the code from here and then edit the parameters as follows. For help in this step, you can also use the official Marlin documentation here.
- Set "MOTHERBOARD" to whatever is appropriate for your board.
- If your board has five motor drivers, and you want to use five strings, then:
- Set "EXTRUDERS" to 2
- Set "TEMP_SENSOR_1" to 1
- Set "DISABLE_INACTIVE_X" to "false"
- Set "DISABLE_INACTIVE_Y" to "false"
- Set "DISABLE_INACTIVE_Z" to "false"
- Set "DISABLE_INACTIVE_E" to "false"
- Set "MIN_STEPS_PER_SEGMENT" to 1
Then, use the Arduino IDE or Platform.IO to compile the firmware and load it up on your 3D printer controller board. Easy peasy!
Step 6: Installing Python on a Windows PC
The controller software runs on a desktop PC. It performs the following functions:
- Sends commands to the electronics controller board via the USB cable.
- Processes audio data from the microphone.
- Displays a user interface.
The software was written in Python, an open-source programming language. To run it on Windows, I suggest you download and install the Python Anaconda Distribution.
Once this is installed, the next step is to update it with a few extra libraries. One fast way to do this is to open the "Anaconda Prompt" from the Anaconda group in your Start Menu and type the following command:
conda install -c anaconda pyaudio cbetters peakutils tk pyserial
Now, launch "Spyder" from the Anaconda group in your Start Menu. Spyder is a Python IDE.
Step 7: View the Spectrum of a Plucked String
Next, click this link and save it to your PC as "tuner_gui.py". Open the "tuner_gui.py" file in Spyder and click the Play button to run the GUI.
At this point, you want to make sure the controller board is plugged in to your PC and that you have a microphone hooked up to your PC.
Click the "Connect" button on the Main window. This will give you a selection of serial ports to connect to. Click the one corresponding to your board and click "OK"
Now, tap the "+" sign. The motor on the first string should energize and you can click it repeatedly until the string feels tight.
Next, click the "Spec" button. This brings up the "Audio Spectrum" window. On it, you will see a peak each time you pluck a string. Aim to pluck hard enough so that the peak crosses the red line. A successful detection will be indicated by a red dot at the top of the peak. If the red line is to high to cross, there will be no detections. Click anywhere on the graph to lower or raise the detection threshold.
Experiment with the "+" and "-" buttons to tighten or loosen a string and move the frequency peak. The closer you get to a green line, the more perfect the pitch will be. Notice that if you run off the edge of the graph on either side, the peak will wrap back around to the other side -- this is because the graph can only show about an octave.
For the next step, you will need to tune the top-most ("X") string so the the peak is on the right side of the spectrum, near the green line closest to 450.
Step 8: Exploring the Relationship Between Tightness and Pitch
There is a direct relationship between how tight a string is and its pitch. In this step, we will have the computer learn the relationship and show it on a graph.
Click the "Learn" button on the Main window. Now, pluck the string. Each time you pluck it, the motor will adjust to loosen the string a bit (this is why I asked you to start with the string tuned high). Pluck the string a few times and notice as the pitch gets lower and lower.
Now, click the "Learn" button again to exit "Learn" mode. You can now click the "Pitches" button. This will bring up a new graph, showing pitches across the bottom of the screen and the corresponding motor position along the left. Notice how the dots form a diagonal line.
Click on any of the green dots and the tighten the string to that pitch. If you pluck the string, you will see a colored vertical bar. The color of that bar will indicate how precise the pitch was: red means it was low, blue means it was too high, and green means just right. Each time you pluck, the computer will try to automatically adjust the motor to getting a closer pitch.
Repeat this process for all the notes, trying to get them as well tuned as you can. Notice the effect of jumping from note to note. Do the pitches stay in tune?
Step 9: Is This Accurate Enough to Play a Song?
If you've completed the previous steps and have tuned the individual pitches the best you can, then there is one additional thing to experiment with.
Click on the "Song" button and pluck the string. Each time you pluck, the computer will automatically attempt to re-tune the string to the next note in a song. In theory, if the pitches were spot on, you would get a recognizable song.
In practice, well, try it for yourself and how it sounds. What might have gone wrong? What could be done to improve things?
If you have some ideas, let me know in the comments!
My software uses some code that was originally written by Matt Zucker and released under the Creative Commons Attribution-ShareAlike 3.0 license.
Participated in the
Creative Misuse Contest