Introduction: Arduino Based Flute Player Machine
In this instructable, I am trying to present a project that combines art with engineering. A machine that plays the flute. It controls notes using Arduino. Various tune or song can be programmed on Arduino, which Arduino plays on flute. There is no limit to use the Arduino controls that plays the flute. I tried the following ways to use it to play music:
- Simply coding the song and playing it,
- Playing tones using a Random function.
A specific scale and rules can be defined on Arduino, such that it can compose (in realtime) and play a beautiful melody. - A microphone can be attached to Arduino. So you have to sing in the microphone, Arduino detects the frequency and plays the flute such that it follows whatever note you sing.
Please check the video for the demo in which I tried to play the Titanic theme.
So, there are various ways to use it.
To make this instrument, it is required to have a basic understanding of flute playing or at least some help from the person who knows flute playing.
There are broadly three sections of this instructable.
- First is to make a PVC flute. A ready-made flute can also be used but making is more fun and you can have more control over design.
- Second is to make hardware that plays the flute. this includes preparing electronics and mechanical arrangement.
- The third part is to make a program to play the song. this includes not only song but also creating a program/function that is required to write the song.
Step 1: Making a PVC Flute (Optional):
This part is optional as you can simply buy a flute and continue to next step.
Required Stuff:
- PVC pipe (diameter depends upon the scale of flute),
- Hot glue gun,
- Drill machine with a various drill bit
Knowing flute playing is very much required to complete this step.
The flute is nothing but a hollow pipe open at both of the ends. Out of both of the open end, one end is the blowing hole and another hole depends on finger positions. So by simply using the basic physics formula for the resonance frequency of open pipe length of the flute and positions of the hole can be defined.
However, these calculated value always not need to be correct for accurately tuned flute. Because resonance frequency is based on an effective length which depends on the hole size, pipe diameter and also frequency (which depends on length). so making very accurate flute in the single shot is not easy (it is not even required to have very accurate flute). So we can first try to make flute having tuning close to actual one then file the holes to make tuning accurate.
Following steps need to be followed to make a flute.
1. Decide the scale of the flute you want to make. Let's take an example of the flute that plays the lowest frequency of C4.
2. Refer the image (source: http://www.sengpielaudio.com/calculator-notenames....) and calculate the length required to produce that frequency using below formula.
Length = c / 2f
Where c is the speed of sound =~345 m/s (changes with temperature)
f is frequency = in case of C5 flute 523Hz
So length from blowing hole to open end will become around 33 cm.
4. So cut the PVC pipe slightly higher than 33 cm. seal one end of the pipe using a piece of thick plastic sheet and hot glue. Drill a blowing hole at a distance of 1.5 times pipe diameter. Then try to play it and measure frequency using any guitar tuning mobile application. (I use DaTuner for it.) than keep filing the end of the pipe till the frequency matches exactly to C.
3. Once the total length is decided, the hole position needs to be fixed. use the image (Source: http://www.oocities.org/tpe123/folkurban/flute/hom...) to decide the position of rest of the holes or you can calculate using the same equation for all notes. While drilling the hole first make a small hole, try to play it and gradually make it larger such that frequency matches as per requirement.
Once flute is ready, check all the frequency of the flute using the tuner.
Step 2: Make Air Flowing Mechanism
Making blowing mechanism is bit complex. So this is a simplified version with some compromises. While an artist play flute, artist controls the flow rate of the blow, size of flowing hole by adjusting the lips and position of blowing. But in this case, we only controlling position in two dimensions. The end of blowing tube can go up-down and also in -outside of screen plane. To control position two servo motor is attached to it to move it in the required position.
While playing the flute position of blowing tube need to be set continuously as per frequency. For higher octave and higher frequency end need to be at the lower position. The similar way to turn ON and OFF sound position of tube end is controlled in and out of plane (as air flow is continuously ON).
A shape of the cross-section of blowing tube matters. The tube I am using is having the cross-section of the rectangle of 1mm* 8mm.
The blower (or 4010 Cooling Fan) I used works on 12V supply and make the low noise, which is very much a requirement. While in operation fan is continuously ON. This fan can be bought from below link :
https://www.banggood.in/DC-12V-2-Pin-0_13A-50mm-Br...
Another thing that needs to be noticed is that the fan and servo motor are placed on the sponge base. One common reason for having sponge is to absorb the vibrations and minimize noise. Another use of it at having as the base of the blower is that flexibility of the sponge allows movement of blowing tube as it is connected to blower rigidly.
Step 3: Hole Covering Mechanism
To find a replacement of finger I test tested various ways like using solid plastic, rubber, using the sponge and also the various combination of this. Here the problem arises is that even a small leakage will ruin the sound. One possible option is to increase pressure on this cap or cover to have a better seal but than it becomes hard for servo motor to actuate it. So we have to cover the hole with the minimum force for better operation.
To cover it properly, small cuttings of the pipe is used. it better to understand using the image. at the interface of hole and pipe small piece of plastic foam is attached for a better seal.
Initially, for few of my prototypes, I have used normally open type hole. In this setup, we have to close all holes using the force of servo motor. Again as I am using ESC to provide power supply to Servo motor which can give around 2 Amp of current. So to activate all the motor this current is not sufficient. And constant current drawing also overheats servo motor. Due to this reason, I went for Normally closed type setup. Where all holes are closed using the elastic force of rubber band. So when no force is applied all holes are closed and we can produce the lowest frequency by blowing.
Step 4: Hole Cover Actuation
Here I am using 9g servo motors to actuate the position of hole cover. We have two big advantage of using the servo. First is that it gives precise control over position over hole such that flat note can also be played on it. the second advantage is that servo can give a really high amount of force.
But these advantages come with a big problem "NOISE". servo motors are noisy and 8 servos (six for the hole and 2 for blowing mechanism) are even noisier. So all these servos were isolated to another box. having sponge material inside with triangular cutes on it. These servo control wire/cable by pulling it, this pull eventually pulls hole cover. By using this cover noise can be reduced up to great extent.
No sound insulation is used for blowing mechanism as the movement of it is very small (5-10 degree) that too with less frequently at the lower speed, while hole cover motor needs to continuously actuate around 40-50 degree and speed depends on nature of music being played.
Now once all these part are properly glued to some support mechanical part is done and can now we can proceed for electronic connections.
Step 5: Electronic Connections
Here blower and servo run on a 12V power supply. As explained before, voltage is stepped down to 5V to run servo Using an ESC. while the blower is directly connected to 12 V supply. All servo motor gets control signal from Arduino (and power from ESC).
These connections are straightforward and the above image can be referred for that.
Here the Pin, on which these servos need to be connected is not specified it can be any Pin. These values need to be updated in the code.
The hardware part is now done.
Step 6: Preparing Flow of Code:
To play any song it is very much inconvenient to set positions on every servo motors every time. so better option is to specify some functions that can do the job. so all we need to do while writing the song is to enter the name of the note and duration of it.
So this will be the flow of code:
- Note, duration and attack value of the note is given as input,
- Servo position for this note value is extracted from pre-programmed data,
- Using servo position data and other input required servo position is calculated for that instance,
- The calculated value of the servo is transmitted to the respective servo for actuation.
Code i used is attached here,.
Attachments
Step 7: Setting Up the Instrument
The first thing that needs to be done is checking if every system is working properly or not. following is the checklist.
- All electrical connection is ok, the servo should work properly.
- blowing mechanism should be able to produce proper sound.
- while actuation servo motor all hole cover should open and close properly. proper closing can be verified by hearing the sound of the flute. improper closing of the hole will completely ruin the sound.
Assign servo motor to Arduino Pins:
h1.attach(3);
h2.attach(6);
h3.attach(10);
h4.attach(11);
h5.attach(9);
h6.attach(5);
b1.attach(13);
b2.attach(4);
Here h6 represent the hole closest to blowing hole and h1 is furthest. Pin number needs to be assigned as per your connection. b1 and b2 are the blowing hole positioning servo. b1 for up-down movement and b2 for in plane-out plane movement.
Next part is to set thresholds for various tone playing:
my flute is designed such that it can play lowest frequency of E (when all hole are closed.)
here one of our objective is to actuate minimum number of servo to consume minimum power and minimum noise. so to play notes only two (at max 3) servo are actuated to play any note.
Setting below value is the manual process, for every tone you have to do some trial and error to get the position value.
[x1 is position of servo that control cover of hole 1, similarly for x2,x3,x4,x5,x6;]
[same way y1 and y2 are the positions of servo that controls blower position.]
if(note==1) {x1=00; x2=00; x3=00; x4=00; x5=00; x6=00; y1=45; y2=75;} //E +++ +++
if(note==2) {x1=20; x2=00; x3=00; x4=00; x5=00; x6=00; y1=50; y2=80;} //F
if(note==3) {x1=35; x2=00; x3=00; x4=00; x5=00; x6=00; y1=50; y2=75;} //Fs -++ +++
if(note==4) {x1=40; x2=30; x3=00; x4=00; x5=00; x6=00; y1=90; y2=90;} //G
if(note==5) {x1=40; x2=50; x3=00; x4=00; x5=00; x6=00; y1=55; y2=75;} //Gs --+ +++
if(note==6) {x1=00; x2=55; x3=35; x4=00; x5=00; x6=00; y1=55; y2=75;} //A +-- +++
if(note==7) {x1=00; x2=50; x3=35; x4=29; x5=00; x6=00; y1=55; y2=75;} //As
if(note==8) {x1=00; x2=00; x3=35; x4=45; x5=00; x6=00; y1=65; y2=75;} //B ++- -++
if(note==9) {x1=00; x2=00; x3=35; x4=45; x5=22; x6=00; y1=55; y2=75;} //C
if(note==10) {x1=00; x2=00; x3=00; x4=45; x5=40; x6=00; y1=65; y2=75;} //Cs +++ --+
if(note==11) {x1=00; x2=00; x3=00; x4=45; x5=45; x6=30; y1=65; y2=75;} //D
if(note==12) {x1=00; x2=00; x3=00; x4=50; x5=40; x6=50; y1=65; y2=75;} //Ds +++ ---
if(note==13) {x1=00; x2=00; x3=00; x4=00; x5=00; x6=00; y1=70; y2=75;} //E2 +++ +++ o
if(note==14) {x1=25; x2=00; x3=00; x4=00; x5=00; x6=00; y1=75; y2=75;} //F2
if(note==15) {x1=40; x2=00; x3=00; x4=00; x5=00; x6=00; y1=69; y2=80;} //Fs2 -++ +++ o
if(note==16) {x1=40; x2=42; x3=00; x4=00; x5=00; x6=00; y1=75; y2=75;} //G2
if(note==17) {x1=40; x2=50; x3=00; x4=00; x5=00; x6=00; y1=75; y2=75;} //Gs2 --+ +++ o
General instruction for setting up above value:
- Try to set value such that servo need to travel minimum (minimum noise) and proper tone also produced.
- The position of the blower in to get lowered as higher tone approaches.
- for next octave blower need to lower even more.
- Some note need to be played by partially opening of some hole, this need to be check by various trial with a combination of blowing hole position. for example of my flute; E, Fs, Gs, A, B, Cs, Ds can be played by completely open and closed combinations of the hole. but for rest of the tone partial opening is required.
Once all initial setup is done, all tune should be reverified.
Step 8: Various Functions to Play Music:
- Playing a note:
p(note, hold, attack) -
- Notes obviously the note you want to play,
- hold is the duration (in milliseconds) for which you want to play the tone, and
- attack is how smoothly or aggressively you want to achieve the tone. 0 value is direct stepping from one note to another, while 100 is a completely linear approach to the next tone over specified hold duration
2. Turning sound ON and OFF:
mvol( typ,hold) -
- type- 0 for OFF and 1 For ON,
- Hold: the amount of time (in milliseconds) to hold the condition.
3. Tone vibrato:
pVibr(note, ref_note, duration,freq)-
- note - base note on which vibrato required,
- ref_note- oscillations between note and ref_note will occur,
- duration- in milliseconds,
- freq- frequency in Hz.
[this function still need little work to make it perfect]
4. amplitude vibrato:
pvol(freq, duration,amp)
- freq- frequency in Hz
- duration- in milliseconds,
- Amp- amplitude of oscillation vibrato required.
[this function still need little work to make it perfect]
Other function:
real-time flute player:
a microphone is connected to Arduino it captures sound measure frequency and plays same frequency on the flute. So this instrument can accompany the singer. but this function is still far for perfect.
Step 9: Fun Time: Playing the Song
Now we can write the various song and play it. The various example you can get from the below link. currently is has only note for only one song, I will keep adding after writing other songs.
Real-time melody composer:
A random function is created that select a note from a predefined scale and play with some random inputs. the various rule can be defined such that it can create music accordingly.
Just use following command to play
Step 10: Conclusion:
- It takes really good effort to make and code the complete project, but it is really satisfying to watch Arduino playing flute for you.
so, I highly recommend you to do this project and if there is an observation, query or suggestion please do comment. - There are various possibility that still need to explore like:
- Making the functions for vibrato more close to real values.
- the song follower is not very accurate and fast enough yet and required more work over it.
- This device needs to make MIDI enabled for more fun. (this is what I will target to do next)