December 22, 2007

Arduino Theremin

When I got my Arduino protoshield from sparkfun, I wanted to make something cool with it. And since I had seen a few Theremin projects before, and already thought about making some, I finally made one, using the Parallax PING sonar sensor. Here are some informations about how I made it. You can see all the pictures here.

Arduino Theremin

First, the schematics:

Arduino Theremin circuit

Arduino Theremin speaker schematicArduino Theremin pushbutton schematicArduino Theremin Ping schematic

Here's the commented code for the Arduino:
int pingPin = 7;
int buzzPin = 10;
int btnPin = 1;
int val;
 
void setup()
{
  pinMode(buzzPin, OUTPUT);  //set speaker pin to output
  pinMode(btnPin, INPUT);    //set pushbutton pin to input
}
 
int getPing()
{
  //send a 10us pulse to wake up the sonar
  pinMode(pingPin, OUTPUT);
  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
}
 
void loop()
{
  if (digitalRead(btnPin) == HIGH)  //only play when the button is pressed
  {
    val = getPing() / 5;  //you can tune the pitch by dividing by a different number
    
    //generate the pulse
    digitalWrite(buzzPin, HIGH);
    delayMicroseconds(val);
    digitalWrite(buzzPin, LOW);
    delayMicroseconds(val);
  }
}

And that's about it! Now you should start to learn how to play it... Kinda hard at the beginning, but that's where the fun begins!
Here are some videos of me playing (jingle bells and tetris). It's also kinda noisy... maybe a better sonar would be better (if you have a different sonar, and manage to make a better 'thereduino', I want to see it!!!). Enjoy (or at least try to :p).


If I improve my skills, I'll post new videos... But I also want to see YOU playing YOUR Arduino Theremin! Feel free to send me an email or write a comment.

13 comments:

Mega said...

Nice project?
I like your source code viewer, what did you use for this?
I would like to use such a viewer in my own blog (http://megabotblog.blogspot.com)

Al said...

I use Windows Live Writer, which is a rich text editor for blogging, to post on this blog, with the Code Snippet plugin for viewing code.

I really like windows Live Writer, and think you will, too.

Mega said...

Thanks, works like a charm!

Justin said...

This is excellent!

Adam said...

http://www.youtube.com/watch?v=2s9UHc8sOsY

Video of one I made ; )
I didn't have a pushbutton, but, still fun. Drove the cat crazy.

I think I'm going to put this function into my robot..
http://blog.digitalhaunt.net

Chris Kilner said...

Love your theramin ... 'simple' technology is sometimes the most rewarding

katu said...

Hi! I like this project, but the sound quality is not very good. I'll like to make one, but something more like this.
http://www.oldtemecula.com/theremin/ultermen/ultermen3.htm
do you know how to implement this circuit in my arduino?
thanks

Neel said...

how do you time the pulse is there an algorithm you have for that. thank you

Al said...

Hey neel,
there's no fancy algorithm behind all this. The code is all there: all I do is make a simple square wave and I change the pulse width according to the input value from the Ping sensor.

BTW the square wave is actually the reason why it sounds so bad: a simple low-pass filter should do the job and get the signal more sinusoidal.

remotedevice said...

Hi Alberto,

Your clear and concise documentation was of great help to me as I embarked on designing my prototype arduino-based theremin-like sample-playing "Sonar Tonebank." Thanks for sharing!

If you're interested, you can check out my project on my portfolio site. I got sick of the whiny buzz of the piezo speaker and decided to send the PING data back through the serial port to Processing, which I used to trigger high quality sound samples. There's definitely a great deal of potential here for creating new instruments -- thanks again for helping to point the way.

Rory Kuma said...

Thanks so much! I'd like to point out that I got a nasty communication error with the button on pin 1, but switching it to pin 2 fixed the problem. Now to tune it and learn to play...
-Rory

Unknown said...

you can try to put the output through a low pass filter to clean up the tone.

something like a simple op amp or RC circuit:
http://www.hamuniverse.com/speakernoisecircuit.html

Brett Hepler said...

when I get to close mine gets all scratchy sounding. Also, I triead using a 10Kohm resistor and It actually doesnt work as well as a 100ohm resistor.