loading
At the end of October last year instructables user carmitsu sent me a message after seeing my lunchbox synth. From his message:

I teach music in elementary school. We play a lot of recorder music. i.e. the kids play little flutes...... I have several special needs kids who can are using these black poster boards with circles that have the name of the note on them. These students push on the circles with notes names at the same time the rest of the students are playing a song..... Most of the special needs kids can do this fairly well and in time with the music.
What I am looking to to is build a very simple sound generator so that these kids could play the same pitch as those being played by the students on their recorders. I would only be a few pitches. I thought I could attach some kind of small button to the bottom of their round circles so that when they push on them the sound would come out of a small speaker, loud enough so they could hear.

Having a mother who is a teacher and having liked school, how could I resist? Truth be told I couldn't. This is pretty much a chronicle of the project and instructions on how to build your own.

Step 1: The Beginning or Why I Couldn't Stick to Analog

The good stuff starts on the next page. If you want to know how I ended up using the parts I did, read on.

The veritable timer:
After giving the project some thought I immediately thought of the piezo tone generator from my op-amps Forest Mims III book. Seemed like a good way to go, it's just a piezo, an 741 IC and a couple passive components. No big deal right? Well it has 2 problems, 1) when you depress the switch, it's possible to change the pitch 2) it's damn near impossible to tune. The first could likely be overcome with with some debounce technology, though i didn't know how to do that without adding another counter. It could also be an issue with using a piezo. The second issue just got unwieldy when you started trying to hit a certain pitch. What about the 555? The datasheet shows a time dlay function based on resistors and capacitors. Which is great until you start typing in actual values of real world parts, that's when you'll find hitting a 440Hz pitch starts to get a little difficult. You can use trim pots to get it tuned in, but over time they tend to move. Constantly tuning the instrument, compiled on top of rapid increase on cost and quantity of parts, and my wife making the pitch change when she pushed the button killed the 555 for this project.

Op-amp:
No problem, people have been making synths with op-amps since before I was born. How hard could it be to make a really simple one, with few parts and specific notes? Harder than I thought. Most of the designs out there are very over complicated for this project. Synth designers are out for the perfect waveform/tone. This directly conflicts with a project that is supposed to be cheap enough for school or teachers budgets. Building a keyboard is easy enough, it's just a bunch of resistors and power or a bunch of diodes and power. It's the rest of the circuit design, and the cost of custom pcbs that starts to get out of hand for someone at a beginning electronics level.

Project redefinition:
So the project got redefined before i even really got going. I needed something that could toggle a speaker pin, in time, with the press of a button. I didn't want to have to design and buy a PCB. This had to use as few components an possible, and be assembled as a beginners kit. It was staring me in the face the whole time. Duh!! Microcontroller!

Microcontroller:
So after purchasing both an Modern Devices Bare Bones Arduino kit and an Evil Mad Scientist Simple Target Board and letting them sit on my desk for months not being used, I had the perfect introductory project. I started looking at the time it takes to put both together, the learning curve for the code, cost, the additional parts needed and make it do what I want and settled on the target board.
Cost was pretty even, $15 plus a $20 FTDI cable for the Arduino, $12 plus a $22 USBtinyISP programmer. I already knew C++ from the little college I could stand and figured C for microcontrollers wouldn't be that bad, while other than blinking a light to make sure I put my kit together properly, I had no Arduino experience. Both could be mounted. It was pretty much a toss up, so I decided on the fewer parts of the two, the target board.

Step 2: What You Need Rev 1

Parts Cost
USBtinyISP AVR Programmer Kit (USB SpokePOV Dongle)
v2.0 $22.00
http://www.adafruit.com/index.php?main_page=product_info&cPath=16&products_id=46
Evil Mad Scientist labs ATmegaXX8 Mini Dev Kits $12
Headers, 6-pin DIP, 5-pk $2.75
http://evilmadscience.com/tinykitlist/74-atmegaxx8
Speaker (8-ohm Mini) $2.79
http://www.radioshack.com/product/index.jsp?productId=2062406
SPST Normally Open Momentary Switches (we used 5) $3.49(4pack)
http://www.radioshack.com/product/index.jsp?productId=2062539
"AAA" Battery Holder $1.79
http://www.radioshack.com/product/index.jsp?productId=2102735
SPST Submini Slide Switch (optional for power switch) $2.69(2-pack)
http://www.radioshack.com/product/index.jsp?productId=2062490
LED and resistor (optional, if you want a power light) $should be free if they're lying around
Wire $should also be lying around
Silver solder (if this is going anywhere near children you should probably not use lead, parents get pretty weird about that sort of thing ;) )
Rubbing alcohol (to remove flux)

If you want some places to save money, you don't have to get parts from RadioShack. I did because they're close and predictable.

Step 3: Assembly Rev 1

Put your parts together as shown. Be sure to use rubbing alcohol and a flux brush when you are done to get rid of any flux from soldering.

Schematic is below for those who prefer it. D1 and R1 can be whatever you want, it's just a power light. C1 is just to help make the power a little more consistent. I used a 10uF. This schematic is the same for the later versions, just swap out push buttons for fabric switches. in the last revision I added switches to PC4 and PC5 or pins 27 and 28.

Step 4: What You Need Rev 2

So basically you are swapping out the Radio Shack buttons for hand made fabric ones.

You need:
Multiple colors of fabric, or one color if you want every note the same color. The quilting section of your closest fabric store is the best place to get this cheaply. Quilting quarters are $1.50 and you can get a ton of switches out of one.
Conductive fabric, I suggest the Flectron fabric from LessEMF it's $20 for a 12"x54" piece
Eyelets for attaching the wire to the fabric. You can get them at most fabric stores cheap. JoAnn has the tool and 25 for $2 You will need a hammer for this.
Wire, I used 22AWG, I suggest smaller if you have it.
Fusing, to get the colored and conductive fabric to stick together. Ask at the store and you can probably buy it by the yard. This would probably work fine, though I do suggest you ask someone
Batting, I used a really loose stuff at first, then switched to something tighter knit. I suggest you just try a couple different ones till you find what works best.
Heat shrink tubing, this made the whole thing a lot more manageable. heat shrink wires together as they pass each other and they'll stay a lot better.


Step 5: Assembly Rev 2

Lay the multi-colored fabric over each other and cut a out a square, it doesn't have to be perfect. Iron the fabric to get any folds out, layer them on top of each other again and make sure the edges all match. Cut any edges that don't.
Cut the top and bottom round, leaving the left and right sides straight. You're going to invert the switch after sewing so don't make the rounded sides too round and leave enough height on the flat sides to push the switch through easily when you're done. Round out the flat side of the first and last switch.
Head on over to your sewing machine and put two of the rounds together so that the good side faces each other and sew a seam on the flat parts.
Once the tops of the buttons are sewn, cut squares out of the conductive fabric and fuse one side. Cut wire to length and strip one end. solder the wire around one of the eyelets. (the solder will not likely stick to the eyelet) Cut a smallslit in the fabric at one corner and hammer the eyelet to attach the fabric and wire. remove the backing from the fusing and fuse the conductive fabric to the back of the colored fabric. Repeat for all the switches. To make things easier, you can heat shrink tube the wires together whenever one wire passes the the wire for the next switch.
Get or make a piece of fabric the length of the switch set and cut a strip of conductive fabric the length of it. Unlike the colored switches, this is one long piece. This will be what the buttons touch to make the connection. Cut a small slit and attach a wire to it with an eyelet, like the colored switch tops. Fuse it to the back of the bottom fabric.
Sew the top and bottom together with the conductive fabric facing out, use a yard stick to invert the set after it's sewn.
Cut a few small holes in the batting, I used a chopstick to get a circle then just remover the chopstick and make the cut. Cut them to shape and insert them into the switches. This is what holds the conductive fabric apart.
It may be easier to insert the batting while you are inverting the switches instead of after. The stuff I used tore up pretty bad while I was inserting it and I had to get different batting.
Finally hook the wires up to the target board and you're all set. The long bottom strip will be ground.

Step 6: What You Need Rev 3

In Rev 3 I made the switches independent of the wiring. This saved a ton of time. I also like the look of the finished result a lot more.

So, here's the new parts you need:
Snaps, Just got them from JoAnn's. They came with an install tool not to bad for $7 I would suggest one of the pliers tools instead as I had to take my tool apart to install the snaps in the middle. Sew ons would be a good alternative, though they take longer to adhere.
Conductive thread, Spark Fun has stuff that can go in your sewing machine. I had to go really slow in my machine or the thread would break. Your mileage may vary


Step 7: Assembly Rev 3

This revision was a lot better than the than the last in terms of time till built. Plus, having removable switches saved my sanity more than once.

Start off by making the base. It's just one quilting quarter folded in half. Put a layer of batting on the top and sew, leaving a hole to invert it. Push the thing through the hole and you basically have a pillow type thing. I quilted it so it was easier to work with. Use plain thread and go nuts. I did diamonds on this one, but maybe next time I'll put a dragon in back, or something cool.
Now, on to the switches. I like this design because you can basically make a ton of these ahead of time and just use what you want. start by using a scrap piece of cardboard, paper, or fabric and cut a pear shape out of it. Use that as a guide to cut a top and bottom for each switch. I cut one color for each bottom and a different color for the tops, but you can be creative and do what you want.
Cut down your guide to a lollipop shape that will easily fit inside the switch. Lengthen the "stick" so it can wrap over the edge of the "pear." Cut the shape out of the fusing and conductive fabric and fuse one side of the conductive fabric.
Remove the backing from the fusing material and fuse it to the top of the fabric that will be on the bottom and the bottom of the fabric that will be on the top. Wrap the extra bit around the top.
Head on over to your sewing machine and put batting with holes between the top and bottom fabric. Sew outside the conductive fabric and skip over the "stick" part of the switch. I found that it's possible to sew the top conductive fabric to the bottom creating a short. It's better not to sew through the conductive fabric. Attach snaps and the switch is done. I found it easier to use the male snap for all the bottom/GND connections and the female snap for all the tops. This makes all the switches interchangeable.
The circuit:
The thing about cutting rounds is that you have a lot of extra bits. I took my scraps, fused big pieces of fusing to big pieces of conductive fabric and used those to cut out the smaller rectangles I used as pads. Roughly lay out your switchesand fuse the pads to the base with enough room to sew a line to them and have a snap. The foot of the sewing machine I used didn't take kindly to being real close to the snap, so keep that in mind and give yourself some space. Since I got conductive thread that could go in my sewing machine I just sewed a line from pad to pad and back. I had to go slow or the thread would break, but it was a ton faster than hand sewing. Also with conductive thread on the bobbin and needle, I got a really good solid connection. The stuff frays like crazy, but a little craft glue or Elmers clears that right up. Try to keep the lines well away from each other and you shouldn't have any issues.
Final assembly:
Snap on all your switches, connect the board, load code and you're done. I used wire to get from the board to the pad and then just sewed the wire to the base by hand. For the next version, I will mount the board in a plastic box with snaps to attach it to the base so that idle fingers do not pull it apart.

Step 8: Code

If you have never programmed a chip before, this is a somewhat daunting task. It doesn't help that the tools are flaky and most of the time you have to perform the same operation multiple times. The two best resources I've found for getting an understanding of what's going on is the page for the USBtinyISP, http://www.ladyada.net/make/usbtinyisp/ and the crash course in programming the noise toy, http://blog.makezine.com/archive/2008/05/noise_toy_crashcourse_in.html These should be able to get you started.

A lot of people like Arduino for this coding and there's nothing wrong with using it, except I feel it adds a lot of bloat to a normally simple program. Also, I knew C and don't know Arduino. Maybe one day, if there's time. :)

Code:
{{{
#include <avr/io.h>
//Use a pin to turn on the SPK

/// Typedefs //////////
typedef unsigned char u8;

int main(void)
{
    u8 btnState0;
    u8 btnState1;
    u8 btnState2;
    u8 btnState3;
    u8 btnState4;
    u8 btnState5;
    u8 btnState6;

    DDRB = (1 << DDB6); //Set SPK for output
    PORTD =(1 << PD0) | (1 << PD1) | (1 << PD2) | (1 << PD3) | (1 << PD4); //Set Button High
    PORTC = (1 << PC4) | (1 << PC6);
    TCCR2B = (1 << CS21); //Set Up Timer
   
    while (1)
    {
        btnState0 = ~PINC & (1 << PC5);
        btnState1 = ~PINC & (1 << PC4);
        btnState2 = ~PIND & (1 << PD0);
        btnState3 = ~PIND & (1 << PD1);
        btnState4 = ~PIND & (1 << PD2);
        btnState5 = ~PIND & (1 << PD3);
        btnState6 = ~PIND & (1 << PD4);
       
        if (btnState0)
        {
            if (TCNT2 >= 190)
            {
                PORTB ^= (1 << PD6); //Flip SPK Pin
                TCNT2 = 0;
            }
        }
        if (btnState1)
        {
            if (TCNT2 >= 179)
            {
                PORTB ^= (1 << PD6); //Flip SPK Pin
                TCNT2 = 0;
            }
        }
        if (btnState2)
        {
            if (TCNT2 >= 159)
            {
                PORTB ^= (1 << PD6); //Flip SPK Pin
                TCNT2 = 0;
            }
        }
        if (btnState3)
        {
            if (TCNT2 >= 142)
            {
                PORTB ^= (1 << PD6); //Flip SPK Pin
                TCNT2 = 0;
            }
        }
        if (btnState4)
        {
            if (TCNT2 >= 126)
            {
                PORTB ^= (1 << PD6); //Flip SPK Pin
                TCNT2 = 0;
            }
        }
        if (btnState5)
        {
            if (TCNT2 >= 119)
            {
                PORTB ^= (1 << PD6); //Flip SPK Pin
                TCNT2 = 0;
            }
        }
        if (btnState6)
        {
            if (TCNT2 >= 106)
            {
                PORTB ^= (1 << PD6); //Flip SPK Pin
                TCNT2 = 0;
            }
        }
    }
}

}}}
Where do the pitches come from? A little math was needed. The timer clock on the atmega 168 runs at 1MHz. That's much too fast for audio so we have to use the prescaler /8. Then since we need to flip the output pin high then low in order to make 1 cycle, we need to divide the answer by 2 in order to come up with the right pitch. The formula looks like this,
Pitch to put in code= (1000000/8)/(Target frequency*2)
For A(440) this would be 125000/880=142.045 or 142 for our purposes, since the value must be an integer.
The target frequencies of notes can be found just about anywhere online and are generally all the same.

I still want to add a case statement instead of using a bunch of Ifs and use PWM to better control the volume and pitch of the speaker, but for now, this works.
Weren't sure you were aware of this (from your comment that you knew C but didn't know Arduino), but the Arduino is actually programmed using C, just plain ole C. So I guess I don't understand the comment that&nbsp;you think&nbsp;the Arduino&nbsp;adds a lot of bloat.<br /> <br /> Also, just to clear up confusion for anyone about Bekathwia's comment... Early reviews of this thread stated that it tended to fray quite a bit, which could cause shorts. For this reason it wasn't recommended for top thread in a sewing machine. Because the bobbin thread in a sewing machine lies straight (it's the top thread that forms the loops around the bobbin thread), there is less fray.<br />
There is no such thing as pinmode() in C or analogwrite(), digitalread() or any other Arduino function.&nbsp; That is a construct of the Arduino platform in a header file hidden from the user.&nbsp; A sketch needs to be compiled to hex code with the Arduino definitions in order to run on a microcontroller.&nbsp; Those definitions that are needed to make Arduino actually work are the reason for my bloat comment.&nbsp; Sketch programmers for Arduino are taught to use functions that are hidden from them.&nbsp; That, to me, does not qualify as C programming.&nbsp; It is closer to programming in Basic or Java, not &quot;plain ole C.&quot;&nbsp; If Arduino was more open about the header file their software adds on compile and the users actually understood that that's what is happening, then I'd agree that they were programming in C.<br /> <br /> Also, technically, the bobbin thread should not lay flat. It should be pulled into the fabric as evenly as that of the needle. See this article, <a href="http://www.threadsmagazine.com/item/4302/understanding-thread-tension" rel="nofollow">www.threadsmagazine.com/item/4302/understanding-thread-tension</a>.&nbsp; If you use regular thread on the top and conductive thread on the bottom, there will be no electrical connection if your tension is set correctly since the copper pads are on the top.<br /> <br /> Go troll somewhere else.<br />
Well! I guess I should respond that you should go trash Arduino users somewhere else! There are a lot of us Arduino users, especially when it comes to fabric. We use LilyPad Arduino. Just realize that when you go getting all uppity about your work, and slam other platforms, people do have the right to respond and defend their platforms. The &quot;go troll somewhere else&quot; comment is totally inappropriate to this community of users! <br /> <br /> I will admit that I was incorrect about the bobbin thread lying flat. The recommendation not to use this thread as a top thread stemmed from the fact that the top thread has to go through many more twists and turns through the machine and then through the eye of the needle. In tests by the experts in fabric electronics, this tended to cause more fraying.<br />
&nbsp;I'm not trashing Arduino, that's just your interpretation. &nbsp;I use it on all my MIDI and complex projects where importing an Arduino library is faster than writing one of my own. &nbsp;One of my Arduino projects is even on Instructables. &nbsp;For a project as easy as this, I didn't deem it necessary to do it in Arduino. &nbsp;I also believe it's important to fully understand what is going on behind the scenes in order to do the best job I can. &nbsp;The majority of people just want to get it done quickly. &nbsp;Arduino allows for very quick/easy development. &nbsp;I just can't believe it's the end all of DIY firmware code design. Sorry.<br /> <br /> While perhaps my troll comment was slightly out of line, your comment seemed like an attack on me for not just doing it in Arduino like everyone else and my ignorance of what Arduino is/how it's programmed. &nbsp;Your comment neither suggested improvement nor offered praise for spending the time to put this Instructable out there. &nbsp;If I read more into it than you intended, I&nbsp;apologize.<br />
Apology accepted. :) I'm sorry that I gave the impression that I was trashing you.<br /> <br /> I went and looked at the Arduino site for information about the pinMode statement (which is a function). Arduino is said to be totally open-sourced (I thought), so I figured that anybody who is interested in how the wheel they're using was built should be able to go there and find the source code. Except that I couldn't find it...<br /> <br /> You are right that many (and in my estimation, a fairly sizable number) of people who use the Arduino platform don't understand the code they're using. However, that's not all bad, in that it has opened up microcontrollers to many more people (artists, for example).<br />
No big deal. &nbsp;You'd think I'd have mastered that whole count to ten before getting upset thing by now. &nbsp;;) &nbsp;<br /> <br /> If you look under the FAQ on the Arduino site, there's a build process link. &nbsp;It takes you to&nbsp;<a href="http://arduino.cc/en/Hacking/BuildProcess" rel="nofollow">arduino.cc/en/Hacking/BuildProcess</a>&nbsp;which explains the whole thing is gory detail. Look at the Wprogram.h stuff. &nbsp;Wprogram.h includes wiring.h and that's how they get serial.print() and digitalWrite() to actually mean something. &nbsp;you can see the source at <a href="http://code.google.com/p/arduino/source/browse/trunk/hardware/arduino/cores/arduino/" rel="nofollow">code.google.com/p/arduino/source/browse/trunk/hardware/arduino/cores/arduino</a>. &nbsp;I still can't figure out why they keep talking about a bootloader if they are compiling and loading the chip through USB, but I figure telling people there's a bootloader is faster than sitting then down and explaining the detail of that page. &nbsp;:D &nbsp;
Thanks for the tip on Arduino. Sorry about the slow response to this. I am not at all well, and have to take my good days when I can get them. :)
there is no demonstration video yet?
See steps 4 and 6. Both those versions have their own videos. I can make a video of the first one if you'd like, though it'll sound the same. Should I put one on the first page?
If you put the conductive thread in the bobbin side of your sewing machine and use a regular thread on top, it won't break quite as much. I also find the kind from Lame Livesaver is a lot stronger:<br/><br/><a rel="nofollow" href="http://members.shaw.ca/ubik/thread/order.html">http://members.shaw.ca/ubik/thread/order.html</a><br/>
It was only the top that ever broke. Since I ran a line from pad to pad and then back it would have probably worked out OK only using it on the bobbin. Did you have to increase the tension on the needle to bring the conductive thread up or did you sew it upside down to get the conductive thread to make contact? btw, love the bike patch. :)
What a great project! I love that the buttons are color coded. Very functional and really cute too! :D
thanks. Rev 3 is actually based on the colored construction paper pad they were using before. Here's a pic.

About This Instructable

10,433views

22favorites

License:

More by pstretz:First Modded SK1 Microcontroller Fabric Tone Generator in C-code Controlling Cubase with Arduino based MIDI 
Add instructable to: