Mbed Digital Theremin using Sonar

The theremin is the worlds first electronic musical instrument and it is played without physical contact. Moving hands near two antennas controls the sound. One hand controls frequency and the other volume. A few simple theremins use a volume control instead of a second antenna. It was a side effect of a research project to develop proximity sensors. In most music recordings today, more complex modern music synthesizers are used that are easier to play.

The theramin uses analog circuits and moving the hand changes the capacitance of the feedback in an analog oscillator, but here is a very simple/crude digital version using only a low cost HC-SR04 sonar sensor and a small speaker with mbed. The speaker will need an driver/amp circuit and some simple options can be found in this using a speaker with mbed wiki.

Prof Theremin plays his new instrument in the 1920s

Early classic Sci Fi movies often used a Theremin for eerie music and sound effects. Here are a couple of modern Sci Fi themes redone retro style with a Theremin instead of a modern synthesizer.

Doctor Who Theme with a Theremin

Star Trek theme played on a theremin

This example code uses distance readings from the Sonar to set time delays for a periodic interrupt. The interrupt toggles an output bit to generate a variable frequency square wave. Moving a hand in front of the Sonar sensor controls the frequency or pitch of the audio output.

//Theremin style demo using HC-SR04 Sonar and a speaker
// moving a hand away/towards sonar changes audio frequency
#include "mbed.h"
#include "ultrasonic.h"

DigitalOut audio(p26); //output to speaker amp or audio jack
DigitalOut led(LED1); 
DigitalOut led2(LED2);

Timeout cycle;

volatile int half_cycle_time = 1;

//two calls to this interrupt routine generates a square wave
void toggle_interrupt()
    if (half_cycle_time>22000) audio=0; //mute if nothing in range
    else audio = !audio; //toggle to make half a square wave
    led = !led;
    //update time for interrupt activation -change frequency of square wave
    cycle.attach_us(&toggle_interrupt, half_cycle_time);
void newdist(int distance)
    //update frequency based on new sonar data
    led2 = !led2;
    half_cycle_time = distance<<3;
//HC-SR04 Sonar module
ultrasonic mu(p6, p7, .07, 1, &newdist);    
//Set the trigger pin to p6 and the echo pin to p7
//have updates every .07 seconds and a timeout after 1
//second, and call newdist when the distance changes

int main()
    audio = 0;
    led = 0;
    cycle.attach(&toggle_interrupt, half_cycle_time);
    mu.startUpdates();//start measuring the distance with the sonar
    while(1) {
        //Do something else here
        //call checkDistance() as much as possible, as this is where
        //the class checks if dist needs to be called.

The maximum sample rate (<16Hz) of the Sonar sensor limits the device somewhat versus the original analog-based theremin. Using PWM to generate the variable frequency audio signal generates a lot of audio clicks as the PWM period is changed, so timer interrupts were used instead. Some fine tuning adjustments to the frequency scaling would make it a bit easier to play. Currently, it covers almost all of the audible frequency range so it is very hard to hit a particular note. A gain and zero pot to tune the output near the frequency of interest might be handy. A purer tone could be produced by playing back precomputed sine wave sample points using the D/A with a variable play back rate, but most instruments also have some harmonics (like a square wave). Volume/amplitude could also be changed when using the D/A and a second sensor could be added to control it with the other hand. Other proximity sensors such as a Sharp IR distance sensor or a TOF sensor might also work, but they tend to work at shorter distances.

Midsomer Murders TV Theme Song Story

A Theremini Tutorial

History of the Theremin and a Teardown

The Moog factory and engineering design center in downtown Asheville, North Carolina USA offers free factory tours and the factory store has theremin demo units that visitors can try.

Please log in to post comments.