Sei sulla pagina 1di 8

technology

workshop

living

food

play

outside

Arduino Optical Theremin


by gwarbeh on September 25, 2012

Table of Contents
Arduino Optical Theremin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Intro: Arduino Optical Theremin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Step 1: Hack the headphones . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Step 2: Assemble the circuits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Step 3: Write the code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

File Downloads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Step 4: Play (see video link) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Related Instructables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Advertisements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

http://www.instructables.com/id/Arduino-Optical-Theremin/

Intro: Arduino Optical Theremin


When I first became aware of Arduino, a theremin seemed like a natural project: sensor + actuator. It turned out to be quite affordable and fun to make. It is a naive
design, using just one photocell and some hacked headphones. After some experimentation, I decided that a self-calibrating function was essential if I wanted repeatable
performance in different light levels. Also, I decided to autotune it using math instead of a lookup table. To me, the pentatonic tuning is the most pleasant, although this
can be adjusted easily to chromatic or other tuning schemes if desired.
Here's a list of the materials I used:
* Arduino Uno (running on Mac OS X 10.7.4 Intel)
* USB cable
* Breadboard
* Super-cheap earbud style headphones (from a plane flight, I think)
* standard small photocell
* 10K Ohm resistor
* Some wire
* Soldering gun with solder

Step 1: Hack the headphones


In order to send signals to the headphones, I cut the wires and soldered them to some jumper wires. There are a few tricky points here.
First, once you cut the wires and strip off some insulation, you should see 4 wires, 2 from each side. Each pair forms a circuit with a single headphone. So we can
effectively think of each pair as containing a + wire (usually colored red or blue) and a ground wire (usually copper colored). We can either just use 1 of the headphones
(and thus one of the wire pairs), or we can combine the wires to use both, thereby achieving twice the fun. To do this, we will want to solder the + wire from each pair
together to one jumper wire, and the ground wire from each pair to the other jumper wire. See detail photo of headphone hack above.
Second, beneath the obvious rubber insulation, there is often a mostly invisible resin layer coating the 4 individual wires. The easiest way to get it off is to heat it with a
lighter until the wires appear to burn briefly. Note that after this is done, the colors can be very difficult to discern, so it is a good idea to leave a section unburned that you
can use to identify them. I also use a wet cloth to wipe of some of the residue from the burnt resin to improve conduction.
For the actual soldering, I have very primitive equipment, so I just twist the headphone wires together and around the jumper cable and then drip some solder on that.

http://www.instructables.com/id/Arduino-Optical-Theremin/

Step 2: Assemble the circuits


There are two simple circuits involved.
Headphone circuit: Use any Adruino digital out pin and a ground to make the circuit with the headphone jumper cables.
Sensor circuit: It's a standard voltage divider job that lets the varying resistance of the photocell modulate a voltage read by an Arduino analog in pin. First, I make a
series circuit that starts from the Arduino 5V power pin, goes to my photocell, then to my 10K Ohm resistor, and finally back to Arduino ground. I probe the voltage at the
node between the resistor and the photocell using the A0 analog in pin.
Occasionally, I had problems with flaky connections. I think I need to use better jumper cables next time. When things just weren't working right, I could just wiggle some
cables and identify the flaky connection. Securing the cable more firmly in the breadboard or re-burning the resin always did the trick.

Step 3: Write the code


The heart of the code is basically two commands:
* An analogRead that gets the value of the voltage from pin A0. This effectively measures the light level on the photocell.
* A tone command that sends a signal to pin 9 and causes the headphones to produce a tone at a specified frequency.
Of course there are a lot of other things to be done:
* Variable definitions
* setting up pin 9 for output
* Calibration loop: The user exposes the photocell to a range of light levels, teaching the program how to scale frequencies
* Compute scale and shift parameters based on calibration data
* Autotuning: rounding frequencies to the nearest desired note using logarithms
The sketch is attached as a text file and has a ton of comments.

File Downloads

OpticalTheremin.rtf (3 KB)
[NOTE: When saving, if you see .tmp as the file ext, rename it to 'OpticalTheremin.rtf']

Step 4: Play (see video link)


Now you just open the Arduino editor, verify the sketch, plug in the USB, and upload. Here 's a video of what it should be like.
Of course there's plenty of room for modification and improvement. Some possibilities include:
* Adding a volume control like (potentiometer, or another photocell)
* Using a better speaker
* Making the code more efficient (fewer doubles)
* Taking advantage of the map function instead of mapping the calibration by hand
Feel free to suggest your own ideas, and have fun annoying people with your theremin.

Related Instructables

Arduino Pillow
Theremin by
amf45

Augment a
Moog
Etherwave
Theremin by
jmsaavedra

The 555 Light


Theremin by
jensenr30

Solar theremin
with Arduino by
ninuxi

http://www.instructables.com/id/Arduino-Optical-Theremin/

Theremin by
udubinsky

Intel IoT Edison


Sonar Theremin
by techprolet

Advertisements

Comments
50 comments Add Comment

view all 57 comments

Brezorojo says:

Apr 10, 2015. 4:58 AM REPLY


@gwarbeh. Hi, I managed to make this cool project and even got it to work with a piezo buzzer, not with the headphones (probably joined the wrong
cables)... But as an electronics virgin I can't figure out where to insert the values in the sketch to make a chromatic or pentatonic scale. Please help!

danielg19 says:

Feb 25, 2015. 10:20 AM REPLY


@gwarbeh, if you were to use an Ultrasonic Ping sensor rather than a photoresistor... how would your calibration and note mapping code change?

gwarbeh says:

Feb 28, 2015. 4:02 PM REPLY

Hi, danielg19.
Not really sure, but my instinct is to say that you wouldn't have to change anything. Did you try it as is? The idea of the calibration loop is to adjust to
whatever range of values you get. So if your ping sensor spits out totally different voltages than my photoresistor, it should still work as long as you give it
a taste of the full range you want to use during calibration. I've never worked with the ping sensor. Maybe it will by more linear in response than the
photoresistors. If so, the sound should be a lot easier to control. Let me know how it works out.

DavidW31 says:

Feb 22, 2015. 11:35 AM REPLY

I'm getting some errors. ...sketch_feb22a.ino:13:8: error: 'double log' redeclared as different kind of symbol
/Users/mconstant/Downloads/Arduino.app/Contents/Resources/Java/hardware/tools/avr/avr/include/math.h:304:15: error: previous declaration of 'double
log(double)'
extern double log(double __x) __ATTR_CONST__;
^
sketch_feb22a.ino: In function 'void loop()':
sketch_feb22a.ino:62:8: error: assignment of function 'double log(double)'
sketch_feb22a.ino:62:8: error: cannot convert 'double' to 'double(double)' in assignment
In file included from sketch_feb22a.ino:8:0:
/Users/mconstant/Downloads/Arduino.app/Contents/Resources/Java/hardware/arduino/avr/cores/arduino/Arduino.h:88:41: error: invalid operands of types
'double(double)' and 'double' to binary 'operator+'
#define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))

DavidW31 says:

Feb 22, 2015. 10:53 AM REPLY

I'm getting this: 63:11: note: in expansion of macro 'round'


Error compiling.

j450nn014n says:

Feb 8, 2015. 9:44 AM REPLY


added a push button before the speaker. though I'm sure I can put it anywhere, just so that I don't go insane with it on. this is a good activity to play with lots
of variables.

j450nn014n says:

Feb 8, 2015. 9:05 AM REPLY


Nice little job. Worked out of the box as you suggested. Used a buzzer for the audio out though. I'm gong to check into some of the mods. Thanks.

http://www.instructables.com/id/Arduino-Optical-Theremin/

rzaragoza2 says:

Dec 18, 2014. 2:59 PM REPLY


having some trouble with this project (my first), the sensor isn't getting any variation in its serial feed ( just 0,0,0) when you view it in the serial monitor. Any tips?
the sensors work fine when tested and the circuits are correct when checked by a friend.

EmmanuelLobont says:

Nov 27, 2014. 5:29 PM REPLY

Hi! Can I ask you where I can get the sketch? I'm a beginner with Arduino and wanna practice with this project :)

nakul.tomarrajput says:

Nov 17, 2014. 6:21 AM REPLY

Hi! Can I implement the code on windows 7?


Also I have a Arduino ng kit... Will this work fine???

gwarbeh says:

Nov 17, 2014. 6:18 PM REPLY

Don't see why not. Just go to Arduino page and find the right download for you.

nakul.tomarrajput says:

Nov 26, 2014. 6:40 AM REPLY

ok. thanx... I will get back to u if need b... thanx

nakul.tomarrajput says:

Nov 17, 2014. 6:37 AM REPLY

M a beginner... this being my first arduino project.... M doing it for my college technical fest... Plz help me... M a rookie!!!!!!!!!!!

gwarbeh says:

Nov 17, 2014. 6:19 PM REPLY

No worries, nakul.tomarrajput. You can do it! Just follow the steps and let me know if you have other specific questions.

rs242 says:

Oct 29, 2014. 11:26 AM REPLY

Sounds great :-)

Orange Joe says:

Oct 4, 2014. 3:47 AM REPLY


Ha! This is the first thing I thought of making after I made aaaalll of the blinking LEDs. Thanks for a starting point and suggestions for improvements. :-D

rhonardoost says:

Feb 19, 2013. 2:05 PM REPLY

Is there anyway to get the theremin to be silent when in darkness, and only produce sound when light is introduced?

SonicDH says:

Jul 21, 2013. 1:20 AM REPLY


You could use a second photoresistor to act as the variable for an condition, putting the main function of the code inside the condition.

owendelong says:
Alternatively, loop could be modified as follows:
In place of:
tone(PHONES, f);
use:
if (val > minored) {
tone(PHONES, f);

http://www.instructables.com/id/Arduino-Optical-Theremin/

Jul 10, 2014. 1:42 PM REPLY

}
else {
notone(PHONES);
}
(Sorry about the weird linespacing, not sure how to coerce instructables into doing code snippets correctly.

Robomat says:

Sep 14, 2014. 9:35 PM REPLY


I used your alternative, but I had to change your "minored" to "minread" and capitalize the t in notone (noTone), and then it worked awesome!!

owendelong says:

Sep 16, 2014. 1:09 PM REPLY


Yep.... Sorry about the typos. Not sure how the casing got multilated on noTone. I copy/pasted it direct from the code. Probably autocorrect as
minored appears to be an autocorrect from minread as well.

trentoa says:

Jan 27, 2014. 6:15 AM REPLY

any reason as to why this won't compile on Ubuntu 12.x ? i keep getting errors dealing with double(double)'s etc..
am i missing some libraries/includes it compiles fine in Win 7 environment.

gwarbeh says:

Jan 27, 2014. 7:13 AM REPLY

No idea. Never used Ubuntu. You could try changing all doubles to floats. I didn't use any special libraries. Good luck.

owendelong says:

Jul 10, 2014. 1:39 PM REPLY


The errors people are experiencing with the dobule/float issues are because logf is apparently a reserved (or at least used) word in a conflicting
namespace in at least some versions of the Arduino IDE.
Renaming this variable to logfreq resolved all errors for me.

mmokhtar2 says:

Feb 12, 2014. 1:05 PM REPLY

good circuit ,,, thanks :)

MoonDocker says:

Nov 26, 2013. 12:09 PM REPLY


Fun Stuff! I finally got around to trying this one out. Just like you I had a few pair of the airline headphones and they worked fine. I just used one pair it was
hard to distinguish which was + or - but I lucked out and it worked first time around. Thanks for an entertaining afternoon. I am a big fan of Big Bang Theory. I
felt like Sheldon while playing with my Theremin!

http://www.instructables.com/id/Arduino-Optical-Theremin/

gwarbeh says:

Nov 26, 2013. 1:29 PM REPLY

Glad you enjoyed it, "Sheldon."

dmartin46 says:

Jul 9, 2013. 2:43 PM REPLY

Does it matter where in the breadboard I plug the wires as long as they are aligned and in the correct holes in the Arduino?

gwarbeh says:

Jul 10, 2013. 7:00 AM REPLY


Just be sure that you understand the way a breadboard is wired, which holes are connected, etc. If you understand that, then you can hook up the wires
however you want, as long as it's equivalent to my circuit. Good luck.

dhendricks1 says:

Apr 1, 2013. 6:24 PM REPLY


I noticed a couple of people used piezo's with success, I just tried it with a small 8 ohm speaker out of a kid's radio. It works well too.

leeseibert says:

Mar 11, 2013. 4:31 AM REPLY

I made a project similar to this. Then I shrunk it down to an ATtiny85 micro controller.
Check it out here:
https://www.youtube.com/watch?v=BsKRtUCCle4

faziefazie says:

Feb 16, 2013. 6:15 AM REPLY

I'm done with this project, and this is my first arduino project. Thanks a lot to you :)
if I want to put potentiometer, where should i put? And what kind of potentiometer I should use? thanks before.

gwarbeh says:

Feb 16, 2013. 9:16 AM REPLY

Cool. Does it work?


As for the potentiometer (pot), it depends on what you want it to do.
The pot can go in place of the resistor if you want it to affect the volume. A lower resistance will just let more cuurent through to the speaker. Not sure
what all types of pots are available, but i'd try one that goes up to 100k; linear is probably fine.
If you put the pot in series with the photocell but before the A0 read, it could be used to affect pitch. You'd wanna use one with a max resistance similar
to that of your photocell.
Good luck!

faziefazie says:

Feb 10, 2013. 5:26 AM REPLY


I want to try this cool project, but I'm a beginner in Arduino, for Step 3: Write the code, we're just have to write the code in sketch and that's it, or we have to
do the step "there are a lot of other things to be done:", because I really don't understand how to do the Variable definitions, Calibration loop, etc. Please
help me..

gwarbeh says:

Feb 10, 2013. 10:28 AM REPLY


Hi. The code should work as it is. All that about "other things to be done" is just me summarizing what the code is all about. You can just copy the code
that I have there as an .rtf if you want. Then you can try fooling around with it to change it and/or learn how it works. Best of luck!

Zetainex says:

Jan 8, 2013. 10:41 AM

(removed by author or community request)

gwarbeh says:

Jan 8, 2013. 11:22 AM REPLY


Why not? Give it a try. I don't see how it could break anything. I think the piezo buzzer is probably pretty robust. The frequency response may be an
issue, though. It might just make it sound strange. Maybe that's what you want? Good luck, and let me know how it turns out.

Zetainex says:

Jan 8, 2013. 3:07 PM REPLY


I wanted to save me having to use headphones and use a soldering iron and, as I have a piezo buzzer, I wondered if it would work well. The answer
is a resounding yes, it sounds almost like your Theremin in the vid. Thanks for the code and the instructions!

Zetainex says:

Jan 8, 2013. 3:06 PM REPLY


I wanted to save me having to use headphones and use a soldering iron and, as I have a piezo buzzer, I wondered if it would work well. The answer is a
resounding yes, it sounds almost like your Theremin in the vid. Thanks for the code and the instructions!

martzsam says:

Jan 6, 2013. 6:28 PM REPLY


I made a video here of mine. I plugged in a regular speaker from a set of desktop speakers. You can't really tell from the video but it does get fuller sound.
http://www.youtube.com/watch?v=Opvzt_hryBE

http://www.instructables.com/id/Arduino-Optical-Theremin/

gwarbeh says:

Jan 7, 2013. 6:37 AM REPLY

Nice job, martzsam! Sounds good. Thanks for the comment.

Trecool771 says:

Oct 2, 2012. 3:48 PM REPLY


pretty cool bro. im a master at circuitry and programming but new to microprosessors and the arduino programming language. u look good at all 3.

gwarbeh says:

Oct 3, 2012. 12:10 AM REPLY

Thanks Trecool771
I am not a master of any of those, but I do enjoy learning.

AJMansfield says:

Oct 1, 2012. 4:18 AM REPLY


I'v actually done stuff with headphones before, and its actually possible to weld the headphone wires with just a regular lighter. I had to do this once when I
was in Italy on vacation: I needed a sound cable, and I didn't have access to hardly any of my tools, but fortunately I was able to weld two headphone wires
together with that trick and use that.

judas79 says:

Sep 30, 2012. 1:33 PM REPLY


Very cool, have you thought of reversing the way the sensor works, to no light being high frequency sound and low frequency when illuminated by light. Also
maybe have the sound cut off at a certain brightness, so its not alway on.

gwarbeh says:

Sep 30, 2012. 11:36 PM REPLY

Hi, judas79.
That's a really good idea. I think it will be part of the next version. Thanks.

uncle frogy says:

Sep 30, 2012. 12:32 PM REPLY


having built a couple of theremins before I was interested in what this would be like.
I breadboarded a light controlled one as an experiment once, using the basic type of circuit of the classic theremin and oscillators using 555 chips. I never
tried using a digital processor.
I personally like the "slide" of the theremin as it allows vibrato
If you adjust the output to line level you could play it through any amp including a home stereo.
also might make an interesting proximity sensor effect like a "party door bell "
great work always push the edges!
uncle frogy

gwarbeh says:

Sep 30, 2012. 11:35 PM REPLY

Thanks, uncle frogy. I like the doorbell idea! It started out with the slide, but I ended up like the step tone thing better.

professorred says:

Sep 30, 2012. 10:04 AM REPLY


I can hardly wait to have the time to do this. Just a note though, if you wire up a headphone jack, you can save the ear buds for regular use afterwards.

redrok says:
Hi gwarbeh;
This is only half a Theremin.
A real Theremin has 2 controls, Volume in addition to Frequency.
Basically they worked in a similar way. The volume signal is used to modulate the amplitude of the frequency signal.
I suggest you add a second sensor that controls the pulse width of the frequency output.
redrok

view all 56 comments

http://www.instructables.com/id/Arduino-Optical-Theremin/

Sep 30, 2012. 8:07 AM REPLY

Potrebbero piacerti anche