Introduction: Arduino Flute Synth

About: "A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, gi…

Before building this flute synth, I built another Arduino synthesizer using the source code from an Instructable called the Post Box Synthesizer. I put the guts inside an old music box and wired a MIDI input to control it. It sounded very good, and I had been wanting to design a flute synthesizer for a while, so I decided to see if I could pack all of the same controls into a smaller case that could be held up and played with 2 hands.

I found a small wooden pencil box for a couple dollars and began to work on laying out the controls...

Step 1: Control Layout

The Post Box Synth that I based this on used 4 pots and 6 buttons to dial in tones. It used multiple "banks" as a clever way to multitask with knobs. It also had an LCD screen so you could get visual feedback of what bank was selected. When I built the previous synthesizer, I used 6 pots so that each variable could be tweaked without selecting banks. This time I was also going to use 8 capacitive touch sensing metal buttons to trigger notes (which I had previously used on my BUGGO Arpeggio Synth), so that the entire box could be held and played like a flute/whistle using the 4 fingers on each hand, and thumbs resting on the bottom.

This setup was very constraining due to the amount of digital and analog inputs on the Arduino Pro Mini. The 6 pots were well covered by the analog inputs, but when I started mapping out the digital inputs, I needed 8 cap buttons, 4 switches, 1 output for the sound.... 13 pins total, and the Arduino Pro Mini has 14 digital inputs, though pin 13 has a resistor on it for the onboardLED, and 0/1 are the serial TX/RX, which caused me some problems that I will discuss later. I never quite got all the controls working as I wanted them. In the next design I'm sure I can work something out.

I used graph paper to lay out the controls to fit within the interior of the box, including a space for a 9v battery. To get started, I mapped my fingers on to the graph paper and went ahead with nailing the 8 cap buttons (furniture tacks purchased at Home Depot) so that I would have a good reference point for how the other controls fit around my fingers.

I decided that the only logical place I could fit the 3 knobs for cents, semis and octave control of the first oscillator was between the cap buttons. I wanted these 3 to be grouped together since they have similar functions.

That left one fairly small square in which to put the other 4 buttons and 3 controls. Small, but feasible.

Step 2: Drilling the Box

Due to the thickness of the box lid and shape of the metal on the smaller pots I was using, I had to do some fancy secondary drilling after the main holes for the pot shafts were drilled through. This was all done on the underside of the lid to allow the pots to sink in far enough so that the nut could be tightened securely on the top side. Similar drilling was needed to accommodate the 4 switch buttons as well. Due to the tight fit, I had to make some of the pots face a certain direction so that everything had room. I had to make sure the lid could close too, so everything faced inward as much as possible.

After all controls were secured in place I started adding some wires...

Step 3: Wiring

I was trying to keep the wiring super clean because there was so little space inside the box. As you can see it started off great but eventually there were just too many wires needed. I decided long after it was too late that I probably should have used a smaller gauge of braided wire for flexibility.

I had all the wires in place before I installed the Arduino Pro Mini so I could trim them to fit as much as possible.

Step 4: Connecting the Arduino

I had mapped out all of the wires to the Arduino pins they should go to and started to line them up and solder, trimming excess wire along the way. The down side to keeping all of the wires so short was that by the time all of the wires were soldered to the board, it was virtually immobile. But at least the lid closed without squishing everything. I still had the amplifier and filter to put in place, as well as the speaker, power switch, battery and a breath sensor which I didn't even know I was going to add at this point. Not to mention the struggles with using pins 13, 0 and 1 to try and make the 4th button work properly. Desoldering and moving wires around became a very frustrating process with needlenose pliers and blind trial and error. I started deciding that this project would be a prototype or Mark I version of the flute synth, so I could make all the mistakes that I wanted to.

Step 5: Amp and Filter

Time to cram a bunch more stuff in. The amp fit perfectly next to the Arduino. I used the Sparkfun Mono Audio Amp Breakout which I had used in a few other projects. It's a great little low cost digital amplifier about the size of a postage stamp. It hooks up to any 8 ohm speaker and is very easy to use.

I assembled the filter based on the Chebyshef Filter diagram shared with the Post Box Synth. I tested it outside of the case first to make sure it worked before mushing it into place near the front of the box. It is a low-pass filter (LPF) which gets rid of the high frequency noise generated by the Arduino's 33khz PWM sampling rate while letting the lower frequencies pass through. Capacitors and chokes purchased online through Mouser.

Always be careful that no exposed wires or metal are touching, especially when stuff moves around while closing the lid. You will short circuit and cause yourself a lot of headaches and unexpected problems, or kill your project. OR YOURSELF. No, probably not. But maybe.

Step 6: Paint Job

I applied some Martha Stewart high gloss craft paint. It says it will stick to any surface. In hindsight, painting the box BEFORE any drilling or controls would've been the smart thing to do. Sometimes I am too anxious to get the project started for fear of it stalling out waiting for the paint to dry. I removed as many of the nuts, knobs and switches as I could on top. I had to use some smaller brushes to get into the tight parts and scrape some of the overlap off with a blade where it got on the cap buttons. I applied 2 coats for durability.

The wood of this little pencil box was fairly cheap, and there was a point where the paint glued the lid shut and had to be pried open, chipping a large edge off of the lid. After some glue, sanding and touching up, it was back on track- but I made sure to never leave it overnight with the lid closed for a while. Ideally at the beginning the box would have been completely painted, and any touching edges sanded smooth and allowed to dry for several days before assembly. There's always the balance between planning and too much planning..

Step 7: Mo' Drilling, Mo' Problems

I have a love/hate relationship with drilling things. Mostly hate. Especially if you use cheap drill bits, cheap wood, improper technique, it can be very frustrating. Always drill starter holes, take your time if you want precision work. And buy good tools and drill bits.

For the speaker (a tiny rectangular 8 ohm speaker purchased at Skycraft, the local electronics surplus, for a couple dollars) I drilled what I thought was going to be a 1" hole with a spade bit. After placing the speaker, it turned out that I needed 1 1/8" to accommodate some overlap. This is a huge pain to deal with after you already have a 1" hole. ALWAYS measure twice. Inevitably I used my trusty old bench steak knife to carve out what was needed. This thing has gotten me out of some drilling mishaps and jams. Buy a good set of files, they are lifesavers. My resulting work was ugly, but the speaker finally fit and covered up any ugliness.

To drill the speaker hole, I totally removed the lid and all of the electronics that came with it. At some point I was drilling something else with the lid and electronics in place. I am writing this Instructable 6 months after the fact, so I don't really remember what it was - maybe holes for the speaker wires to come through. As you can see from the picture, my drill bit grabbed on to the electronics and twisted them like an alligator trying to take down its prey. It was not pretty. It almost ruined everything. The filter was destroyed. It was ugly and my first try at building it. Usually the second time around goes better, as you can see. Much prettier the second time. Fortunately all of the other wires were able to be saved.

Step 8: All Together Now

Getting everything crammed in was a challenge. Then a friend of mine idiotically/brilliantly suggested "what if you had a breath sensor you could actually blow into?" I wasn't sure how I'd fit anything else in, or how they even worked, but I started searching for one and found it on Mouser. It was the Freescale Semiconductor MPVZ4006GW7U. It was 5V so it was compatible with my Arduino. After reading the data sheet it was a very simple device with 3 pins: 5V, Ground, and Analog Out. You basically provide power, and it reads the air pressure on the little nozzle and spits out data to an analog pin. The 2 extra analog pins (A6 and A7) of the Arduino Pro Mini were perfect for this input-only function.

I used a spare circle of wood, drilled a hole in the other side of the box opposite the speaker and drilled a smaller hole for the breath sensor to poke through. Once it was glued in place the box was fairly complete and it was time to figure out how this would all work in code.

Step 9: What Is a Flute Exactly?

I started looking up how an actual flute works. I'm not going to go into it since it's mostly irrelevant for this more simplified project, but there are many variations. I had 8 buttons and I knew I wanted to produce a range of more than 8 notes. I still wanted it to be simple to play and sound good. I decided that notes would be triggered by the 4 fingers of the right hand, and the left hand would change the offset of where the notes started and make them get higher pitched as you hold more buttons down.

I also looked at how an ocarina works, where it doesn't matter which holes you press to select notes, what matters is the number of holes you cover up. I decided to do the right hand ocarina style so that you didn't have to be very precise. You can hold down any 1, 2 or 3 (or all 4) buttons and the result is the same.

So 5 progressively higher pitched "banks" of notes selectable on the left fingers (which includes not pressing anything with the left hand, giving you 5 total possibilities) and 4 possible notes in each bank on the right fingers, is a range of 20 notes total. Not quite 2 octaves (an octave being 12 semitones) - not a huge range, however.. on the BUGGO Synth project I had 12 buttons for playing notes, but the buttons were mapped out along a user-selectable scale. This not only stretches the range of playable notes out significantly, but also puts you automatically "in key" no matter what you press. The goal of this flute was not to be a real musical instrument, but more something that someone could pick up and enjoy playing without being a master.

If I were to make the instrument more flute-like, I could greatly increase the range by saying different combinations of buttons held down with the left fingers selected different "banks" or ranges of notes. If I could do the math of how many possible combinations that would be.. I'd say it would be.. at least.... 5 to the... 5th power or some such high number. It is certainly something to consider when looking for more headroom. You could also apply the ocarina concept to the left side. I kept it linear since it seems more natural that the notes go higher as you hold more buttons toward the speaker end.

Now that I think about it, this project is probably more accurately called a recorder, or a digital ocarina. Maybe when Mark 2 comes around I will consider another name. Hopefully no flautists or purists will have been offended by the misnomer.

Step 10: The Source Code

The Postbox Synth code written by Sam Stratter (aka humanharddrive) sounds awesome. It has 6 waveforms, 2 oscillators, an LFO. The code is available under his Instructable. I used his code almost as is, except I removed the arpeggio stuff and modified it so that I could use all 6 knobs.

The capacitive sensing code I used is a small snip from I'm not sure where, I've been copying and pasting it from project to project. There is a whole capacitive sensing library on the Arduino Playground which has many more options. The code I have is simple, doesn't require a resistor and just reads a pin and returns a number, which is the number of cycles that it takes for the pin to discharge. The return value is usually 1 to 20 or so.

A note about capacitive touch sensing - it won't work right unless you have a good ground. I found this out the hard way after working on this project. When powered by the USB cable, all of the cap buttons read fine. When I would unplug and power it by my 9V battery, the readings all got erratic. I was able to get around the problem by putting 2 more metal tacks on the bottom of the box where your thumbs would naturally go and wiring them both to ground. This was actually a good thing since it gives your thumbs something to grip. After doing that the readings are more normal but there is still some constant "jitter".

The only code worth sharing at this point is for calculating what note to play. Here it is:

int nonote=1;
int curnote=0;
noteMultiplier=0;

for (int x=0;x<4;x++) {
if (readCapacitivePin(multPins[x])>1) { noteMultiplier=x+1; }
if (readCapacitivePin(notePins[x])>1) { curnote++; nonote=0; }
}

// multPins is an array of what Arduino pins the left side "multiplier" buttons are attached to.
// notePins is an array of what Arduino pins the right side "note" buttons are attached to.

if (nonote==1) {
notePlaying=false;
} else {
noteSelect=43+((noteMultiplier*4)+curnote);
notePlaying=true;
}

The first for loop reads all 4 pins on each side. If the value of the pin is greater than 1, it is being touched. On the left side as it reads them it makes whatever the highest button being touched the multiplier. So if you're touching button #4, it multiplies the number of buttons you are holding down on the right by 4. The left side is sequential. If you're holding down #1 and #4 at the same time, noteMultiplier is going to end up being 4, since it reads the values sequentially and sets noteMultiplier.

The nonote/notePlaying variables determine that if no buttons are pressed on the right (nonote still equals 1 after running through the loop) then notePlaying=false, don't play anything. notePlaying is a variable used by the synthesizer code that says to generate sound or not.

noteSelect is the synthesizer engine's code for what note number to play. We start at a base note (43 in this case, which is a C), and add the multiplier value, plus whatever the value of curnote is to arrive at what note is currently playing. This allows you to step from note #47 (nothing pressed on left, 1 button pressed on right) up to a maximum of note #67. (4 buttons pressed on the left and 4 on the right)

Step 11: Breath Sensor

The breath sensor was a cool addition and added a lot of versatility and expression. It becomes another knob, or replaces one of the existing knobs so it's almost like you're able to turn a knob from zero to full while you're playing. I tried all the different options to see which sounded best, and decided that so many worked well that I would make them selectable. I had 8 buttons, so I programmed in 8 different breath sensor settings to select from.

To do this you hold down the 2 red oscillator selection buttons and press one of the 8 cap buttons. In the source code it loops while you hold both buttons, reading which metal button is pressed and setting a variable (breathMode) to the number selected. Then it chooses the settings with a switch/case statement. It is done a bit brute force, but I had plenty of memory left for code so there was no point in getting too fancy.

Step 12: Conclusion

I will definitely be doing another one of those flutes at some point. I learned quite a bit. I built an Arduino drum machine side-by-side with this flute project as a companion piece. That is why they are the same indigo color, they are bros. I will be doing an Instructable on the drum machine at some point as well.