RadioShackDIY_theramin

download RadioShackDIY_theramin

of 8

Transcript of RadioShackDIY_theramin

  • 7/28/2019 RadioShackDIY_theramin

    1/8

    Arduino Theremin Detailed

    Instructions

    This project can be divided into two parts. The first is the musical instrument itself,consisting of an Arduino, the sketch (or program) and the sonic rangefinder. Thesecond part is the audio amplifier, which will make your musical instrument nice andloud!

    Parts:

    Arduino Uno:http://www.radioshack.com/product/index.jsp?productId=12268262

    Parallax Ping sensor:http://www.radioshack.com/product/index.jsp?productId=12326359

    Multipurpose PC board:http://www.radioshack.com/product/index.jsp?productId=2102845

    Breadboard (for testing/prototyping circuit):http://www.radioshack.com/product/index.jsp?productId=2734154

    J umper wire kit (for testing/prototyping circuit):http://www.radioshack.com/product/index.jsp?productId=2103801

    Ampl if ier components:

    8-ohm speaker:http://www.radioshack.com/product/index.jsp?productId=2062406

    2 x 10uF 16V electrolytic capacitorhttp://www.radioshack.com/product/index.jsp?productId=12466736

    .1uF capacitorhttp://www.radioshack.com/product/index.jsp?productId=2102589

    .05uF capacitorClosest I found on RS site:http://www.radioshack.com/product/index.jsp?productId=12579929

    220uF 16V electrolytic capacitorhttp://www.radioshack.com/product/index.jsp?productId=12448318

  • 7/28/2019 RadioShackDIY_theramin

    2/8

    10-ohm 1/4w resistorhttp://www.radioshack.com/product/index.jsp?productId=2062338

    (or 1/4w resistor assortment)http://www.radioshack.com/product/index.jsp?productId=2062306

    LM386 amplifier:http://www.radioshack.com/product/index.jsp?productId=2062598

    100K audio taper potentiometer:http://www.radioshack.com/product/index.jsp?productId=2062358

    Silver Tone knurled knob

    http://www.radioshack.com/product/index.jsp?productId=2102832

    Getting Started with ArduinoThis is a big topic. If youve never worked with an Arduino before, take a look at theArduino Getting Started page [link: http://arduino.cc/en/Guide/HomePage]. For hands-on learning, starting with the basics, check out the Tutorials [link:http://arduino.cc/en/Tutorial/HomePage].

    To summarize, an Arduino is a device thats built around a simple, inexpensivecomputera microcontrollerthat can make your electronic projects more interactive. Itsdesigned to be much more user-friendly than other microcontrollers, which typicallyrequire a lot of knowledge and experience with programming and electronics. It also

    comes with a programming environment, which provides all the tools you need forwriting Arduino sketches and uploading them to your Arduino. It also uses its ownsimplified programming language.

    To get started, download the Arduino programming environment

    (http://arduino.cc/en/Main/Software) and install it on your computer. Try out some of theintroductory tutorials mentioned above. At the very least, upload the blink exampleprogram to your Arduino, which will make the onboard LED blink on and off.

    Building a Simple Musical Instrument

    When building complex projects, its good to break things down into smaller chunks,which can be tested individually before we hook everything up together. First, well build

    a simple musical instrument and hook it up directly to a speaker (with a resistor), without

    the amplifier. It will be very quiet, but it will let us know that the basic instrument works.

  • 7/28/2019 RadioShackDIY_theramin

    3/8

    The Tone Library

    First, youll need to install the Tone library. A library is a set of pre-programmedroutines that save you the trouble of having to build every feature of your program fromscratch. In this case, the routines take care of the low-level details of producing musicalnotes.

    First, download the Tone Library from here:

    http://code.google.com/p/rogue-code/wiki/ToneLibraryDocumentation

    When you unzip the downloaded file, it will create a Tone folder. This folder should bemoved to the Libraries folder, which is in your Arduino Sketchbook folder. If youre notsure of your Sketchbook folder location, it can be found in your Arduino preferences. If

    the Libraries folder doesnt exist, create it, then copy the Tone folder to that location.

    Restart your Arduino application. You should now see Tone when you select from themenu bar:

    Sketch > Import Library... (look under Contributed)

    For more information: Contributed Libraries on the page:http://arduino.cc/it/Reference/Libraries

    Create The Sketch

    As mentioned earlier, a sketch is what we call an Arduino program. We ve simplified

    things by writing the Theremin sketch for you.

    First, create a new sketch by selecting from the menu bar:

    File > New

    Next, import the Tone library. From the menu bar, select:

    Sketch > Import Library... > Tone (under Contributed)

    Now, copy and paste the code below into the sketch editor:

  • 7/28/2019 RadioShackDIY_theramin

    4/8

    #include

    Tone tone1;//our array of note frequenciesint note[] = {

    NOTE_C3, NOTE_D3, NOTE_E3, NOTE_F3, NOTE_G3, NOTE_A3, NOTE_B3,NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4, NOTE_B4,NOTE_C5, NOTE_D5, NOTE_E5, NOTE_F5, NOTE_G5, NOTE_A5, NOTE_B5,

    NOTE_C6, NOTE_D6, NOTE_E6, NOTE_F6, NOTE_G6, NOTE_A6, NOTE_B6, NOTE_C7 };

    int pingPin = 7;int tonePin = 10;int inches;int pingDuration;

    void setup(){

    tone1.begin(tonePin); //attach tone1 to tonePinSerial.begin(9600);

    }

    void loop(){

    pingDuration = getPing(); //read the PING)))inches = microsecondsToInches(pingDuration); //convert to inches

    Serial.print(inches);Serial.print("in, ");Serial.println();

    if (inches > 3 && inches < 34) //only play if distance is 4 to 33 inches{tone1.play(note[29 - (inches - 4)]);

    } else if(tone1.isPlaying()) {tone1.stop();

    }delay(100);

    }

    int getPing(){

    //send a 10us pulse to wake up the sonarpinMode(pingPin, OUTPUT);digitalWrite(pingPin, LOW);delayMicroseconds(10);digitalWrite(pingPin, HIGH);delayMicroseconds(10);digitalWrite(pingPin, LOW);

    //get the raw value from the sonar, corresponding to the//actual time of travel of the ultrasound wavespinMode(pingPin, INPUT);return pulseIn(pingPin, HIGH); //return this value

    }

    int microsecondsToInches(int microseconds){

    // return the duration divided by 74, the time in milliseconds// it takes sound to travel one inch out and back, then divided by two// since we only want the inches to the obstacle.return microseconds / 74 / 2;

    }

  • 7/28/2019 RadioShackDIY_theramin

    5/8

    (Make sure the first line, #include doesnt appear more than once afterpasting the code.)

    Click the check icon. This compilesyour sketchit turns it into instructions that theArduino understands. It will also help us find errors in the code. In fact, you may seesome errors right away. In the window below the sketch editor, you may see an errormessage that says something like Tone.cpp:26:20: error: wiring.h: No such file ordirectory. This is a bug with the current version of the Tone library (as of this writing)

    that can easily be fixed. In the Tone folder we installed earlier, edit the file Tone.cppreplacing the line #include with #include . This shouldfix the problem! Click the check button again. After a few seconds, you should see themessage Done compiling in the blue bar below the sketch editor, and something likeBinary sketch size: 5288 bytes (of a 32256 byte maximum) below that. This meansyour sketch is compiled and ready to be uploaded to your Arduino! But first, we need to

    build our simple test circuit...

    Connect the Sensor and Speaker

    Our simple test circuit will consist of the Ping sensor, and a speaker connected to theArduino with a 1K resistor to limit the flow of current (hooking up the speaker without theresistor could damage your Arduino). An easy way to set up this test circuit is with abreadboard. Hook up the components following this layout:

  • 7/28/2019 RadioShackDIY_theramin

    6/8

    Upload the Sketch

    To upload the sketch to the Arduino, youll first need to connect it to your computer witha USB cable (the A to B type). Make sure the right board and serial port are selected(see the appropriate Getting Started section for your operating system:http://arduino.cc/en/Guide/HomePage).

    To upload the compiled sketch, click the arrow button (next to the check button). It willtake a few moments, and you should see some lights blinking on your Arduino.

    Try It Out!

    You should now have a working (although somewhat quiet) Theremin-like musicalinstrument! Move your hand close to the Ping sensor. As you vary the distance betweenyour hand and the sensor, you should hear a scale of musical notes. By using a larger

    object, such as a hardcover book, you should get more accurate control of the scale and

    a greater range of movement. When there is no object within a few feet of the sensor, itshould go silent.

    Troubleshooting

    Debugging is a tricky process, so its impossible to cover everything here. However, ifyour Theremin isnt making any sounds, there are a couple of things to try.

    Make sure your sketch uploaded without any errors.Carefully compare your test circuit to the breadboard layout shown above. Is

    everything connected correctly? Have you connected to the correct pins on theArduino? Are you using the breadboard correctly? For a review of how the underlying

    connections work on a breadboard, see this tutorial:http://www.radioshack.com/graphics/uc/rsk/Support/ProductManuals/RadioShack_DIY_AtariPunk_Instructions.pdf

  • 7/28/2019 RadioShackDIY_theramin

    7/8

    Use the serial monitor: After uploading your sketch, and while the Arduino is running(and still connected to your computer), select Tools > Serial Monitor from the menubar. This will connect to the Arduino and read debugging output that was included inthe sketch. As you move your hand near the sensor, you should see a stream ofnumbers indicating the distance of your hand to the sensor in inches. If you don t seethis, then the sensor may not be hooked up correctly. If you do see this but don t hearany sound, then there may be a problem with the speaker circuit.

    Build the Amplifier

    Once you have the simple circuit up and running, you can make your Theremin muchlouder by building an amplifier circuit. This is a more intermediate soldering project, andwill be a good challenge if you already have some basic soldering skills.

    Here is the circuit diagram for the complete Theremin, including both the sensor and

    amplifier circuit:

  • 7/28/2019 RadioShackDIY_theramin

    8/8

    The J1 symbol represents the Ping sensor. Make sure the ground; 5V, and signal pinsare connected correctly.

    Test the circuit first on a breadboard before attempting to solder it to a PCB.

    Experiment!

    One of the best ways to learn programming is to look at existing programs and

    experiment with making modifications. For example, you could modify the scale theinstrument plays. The following line of code defines a major scale, over about 4 octaves:

    int note[] = {NOTE_C3, NOTE_D3, NOTE_E3, NOTE_F3, NOTE_G3, NOTE_A3, NOTE_B3,NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4, NOTE_B4,NOTE_C5, NOTE_D5, NOTE_E5, NOTE_F5, NOTE_G5, NOTE_A5, NOTE_B5,NOTE_C6, NOTE_D6, NOTE_E6, NOTE_F6, NOTE_G6, NOTE_A6, NOTE_B6, NOTE_C7 };

    The structure here is called an array, which, which in simple terms, is a list of things (inthis case, note values). Each note (e.g., NOTE_C3) uses something called a constant tostore the frequency of a given note (in this case C3, which is C below middle C). Thetone library defines these constants.

    With a little bit of musical knowledge, it should be pretty easy to figure out how to modifythis array so your instrument plays, for example, a chromatic scale or an arpeggio. Just

    modify the scale, save it, and re-upload it to your Arduino.

    There are limitless possibilities of how you might modify your musical instrument!