Have you ever listend to a CNC milling machine while milling or to a 3D printer while printing? If so, then you should have noticed that there ary many different tones generated. For nerds, it is obvious to manipulate the milling or 3D printing code so that no random sounds but a real song is played. How this works in principle, you will learn in this article.
Table of contents
Ludwig van Beethoven: Für Elise
In this video my CNC milling machine is playing the song "Für Elise" by Ludwig van Beethoven.
Scott Joplin: The Entertainer
Here my CNC milling machine plays the song "The Entertainer" by Scott Joplin.
Everyone who ever watched to a working CNC milling machine (and preferably with turned off spindle) should have noticed, that such a machine creates many different frequencies. In general you can say: the faster the axis moves, the higher the frequency is. So it is only logical, to generate different frequencies in a defined sequence, so that you can hear a real music.
A CNC milling machine must always know, in what position the axes are at the moment. So usually the axes are driven by stepper motors because with a step motor driver you can turn the rotor of these motors by a small, defined angle and the position then is kept. Conventional stepper motors have a resolution of 200 steps per rotation, that means you must generate 200 clocks to turn the rotor by 360°. However, to get more precise resolutions, you can use a so-called micro-step capable stepping motor controller, which uses time-shifted sine or square waves to get a better resolution. When you use such a control for example in the 16th-step-operation, you must generate 3200 clocks (16*200) to turn the rotor of the stepping motor by 360°. Of course, the stepper motor is mounted on a spindle which has a defined pitch, for example 5 mm per rotation, that means with one rotation of the spindle, the nut moves thereon by 5 millimeters. In this case, you need 3200 clocks so that the axis moves by 5 millimeters, or in other words: 640 clocks for 1 millimeter. When the axis now moves at a rate of 1 millimeter per second (= 60 mm/min), then the stepping motor is controlled with 640 clocks per second, which means 640 Hz. At the double velocity of the axis, a tone with double frequency is generated. According to this principle, (theoretically) every frequency can be generated. And depending on how many axes the CNC milling machine has, a certain number of different tones can be played simultaneously.
Converter: Music → G-code (features)
It would take a long time to write such a G-code, which creates music, by hand, so I programmed an online converter that converts music files (MIDI files) into G-code.
Here I describe the features of the online converter:
First, you must select a MIDI file (*.mid) that you want to convert. There are many websites where you can download these MIDI files for free. But those who have a keyboard can also record their own songs. Of course, the MIDI file must be somewhere on the computer.
Before the MIDI file can be converted into G-code, you must make some settings:
Number of axes
With the number of axes you automatically determine, how many tones can be played at the same time. Normally: the more axes, the better the sound of the song. At the moment, you can select up to three axes.
Selection of the channels
A MIDI file often consists of different channels for different instruments. All available channels are displayed. By enabling or disabling individual channels, you can try to generate the best possible sound from the MIDI file.
Steps per millimeter
As it depends on the type of the milling machine, you must set, with how many steps or clocks the machine moves one millimeter.
Work area settings
To prevent the milling machine from crashing the axis to the edge, you can set the maximum positioning distance for each axis, so that the direction of motion reveres when the limit is reached. If nothing is entered, then the axes move constantly in one direction.
I also integrated the option "disable breaks". When this option is enabled, then the melody is played constantly, that means the breaks are "bridged". As a result, the sound of some songs may be better.
So that you can get an impression, how the song sounds on the milling machine, in addition to the G-code, a new MIDI file is created, which can be downloaded and listened to. When the result is satisfactory, you can download the G-code and listen to the music on the CNC milling machine. If you are not satisfied, of course, the settings can be changed (for example disable some channels) to get a better result.