Sie sind auf Seite 1von 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. The second part is the audio amplifier, which will make your musical instrument nice and loud!

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 Jumper wire kit (for testing/prototyping circuit): http://www.radioshack.com/product/index.jsp?productId=2103801 Amplifier components: 8-ohm speaker: http://www.radioshack.com/product/index.jsp?productId=2062406 2 x 10uF 16V electrolytic capacitor http://www.radioshack.com/product/index.jsp?productId=12466736 .1uF capacitor http://www.radioshack.com/product/index.jsp?productId=2102589 .05uF capacitor Closest I found on RS site: http://www.radioshack.com/product/index.jsp?productId=12579929 220uF 16V electrolytic capacitor http://www.radioshack.com/product/index.jsp?productId=12448318

10-ohm 1/4w resistor http://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 Arduino


This is a big topic. If youve never worked with an Arduino before, take a look at the Arduino Getting Started page [link: http://arduino.cc/en/Guide/HomePage]. For handson 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, inexpensive computera microcontrollerthat can make your electronic projects more interactive. Its designed to be much more user-friendly than other microcontrollers, which typically require a lot of knowledge and experience with programming and electronics. It also comes with a programming environment, which provides all the tools you need for writing Arduino sketches and uploading them to your Arduino. It also uses its own simplified 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 the introductory tutorials mentioned above. At the very least, upload the blink example program 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.

The Tone Library First, youll need to install the Tone library. A library is a set of pre-programmed routines that save you the trouble of having to build every feature of your program from scratch. In this case, the routines take care of the low-level details of producing musical notes. 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 be moved to the Libraries folder, which is in your Arduino Sketchbook folder. If youre not sure 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 the menu 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. Weve 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:

#include <Tone.h> Tone tone1; //our array of note frequencies int note[] = { NOTE_C3, NOTE_D3, NOTE_E3, NOTE_C4, NOTE_D4, NOTE_E4, NOTE_C5, NOTE_D5, NOTE_E5, NOTE_C6, NOTE_D6, NOTE_E6, int int int int pingPin = 7; tonePin = 10; inches; pingDuration;

NOTE_F3, NOTE_F4, NOTE_F5, NOTE_F6,

NOTE_G3, NOTE_G4, NOTE_G5, NOTE_G6,

NOTE_A3, NOTE_A4, NOTE_A5, NOTE_A6,

NOTE_B3, NOTE_B4, NOTE_B5, NOTE_B6, NOTE_C7

};

void setup() { tone1.begin(tonePin); Serial.begin(9600); }

//attach tone1 to tonePin

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) { tone1.play(note[29 - (inches - 4)]); } else if(tone1.isPlaying()) { tone1.stop(); } delay(100); } int getPing() { //send a 10us pulse to wake up the sonar pinMode(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 waves pinMode(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; } //only play if distance is 4 to 33 inches

(Make sure the first line, #include pasting the code.)

<Tone.h>

doesnt appear more than once after

Click the check icon. This compiles your sketchit turns it into instructions that the Arduino understands. It will also help us find errors in the code. In fact, you may see some errors right away. In the window below the sketch editor, you may see an error message that says something like Tone.cpp:26:20: error: wiring.h: No such file or directory. 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.cpp replacing the line #include <wiring.h> with #include <wiring_private.h>. This should fix the problem! Click the check button again. After a few seconds, you should see the message Done compiling in the blue bar below the sketch editor, and something like Binary sketch size: 5288 bytes (of a 32256 byte maximum) below that. This means your 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 the Arduino with a 1K resistor to limit the flow of current (hooking up the speaker without the resistor could damage your Arduino). An easy way to set up this test circuit is with a breadboard. Hook up the components following this layout:

Upload the Sketch To upload the sketch to the Arduino, youll first need to connect it to your computer with a 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 will take 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 musical instrument! Move your hand close to the Ping sensor. As you vary the distance between your 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, it should go silent.

Troubleshooting Debugging is a tricky process, so its impossible to cover everything here. However, if your 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 the Arduino? 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

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 menu bar. This will connect to the Arduino and read debugging output that was included in the sketch. As you move your hand near the sensor, you should see a stream of numbers indicating the distance of your hand to the sensor in inches. If you dont see this, then the sensor may not be hooked up correctly. If you do see this but dont hear any 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 much louder by building an amplifier circuit. This is a more intermediate soldering project, and will 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:

The J1 symbol represents the Ping sensor. Make sure the ground; 5V, and signal pins are 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 the instrument plays. The following line of code defines a major scale, over about 4 octaves:
int note[] = { NOTE_C3, NOTE_C4, NOTE_C5, NOTE_C6, NOTE_D3, NOTE_D4, NOTE_D5, NOTE_D6, NOTE_E3, NOTE_E4, NOTE_E5, NOTE_E6, NOTE_F3, NOTE_F4, NOTE_F5, NOTE_F6, NOTE_G3, NOTE_G4, NOTE_G5, NOTE_G6, NOTE_A3, NOTE_A4, NOTE_A5, NOTE_A6, NOTE_B3, NOTE_B4, NOTE_B5, NOTE_B6, NOTE_C7

};

The structure here is called an array, which, which in simple terms, is a list of things (in this case, note values). Each note (e.g., NOTE_C3) uses something called a constant to store the frequency of a given note (in this case C3, which is C below middle C). The tone library defines these constants. With a little bit of musical knowledge, it should be pretty easy to figure out how to modify this 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!

Das könnte Ihnen auch gefallen