Lesson 8 – The Theremin
This week you will be building a Theremin, which is a musical instrument controlled by positioning of the hands in the air over the instrument. I will provide the circuit below and some hints. It is up to you to see if you can make it work.

Your hints: You learned in previous lessons how to read a distance from the sonar sensor you will need to do this again. For this you will set up two pins one for the trigger and one for the echo I chose in the circuit above 4 for echo and 5 for trigger
So you will need the following items in your code:
Outside all the functions you will declare the pin definitions for the SPEAKER, the ECHO and TRIGGER as well as vaue to store the measurements from the sensor and the output pitch.
const int PITCH_SENSOR_ECHO = 4;
const int PITCH_SENSOR_TRIG = 5;
long pitch_time;
short pitch_distance;
int pitch;
const int SPEAKER = 6;
const int LAG = 60; //Time delay between starting the loop that changes the pitch
Inside the setup loop you will need to define the pin types (INPUT and OUTPUT)
I also suggest setting up the serial output so you can read the values in case you need to “tune” your Theremin.
void setup()
{
Serial.begin(9600);
pinMode(PITCH_SENSOR_TRIG, OUTPUT);
pinMode(PITCH_SENSOR_ECHO, INPUT);
pinMode(SPEAKER, OUTPUT);
Serial.println("Starting Theremin");
}
You will define a function for reading the pulse duration from the sonar.
long senseDistance(int triggerSensor, int echoSensor) {
digitalWrite(triggerSensor, HIGH);
delayMicroseconds(10);
digitalWrite(triggerSensor, LOW);
return pulseIn(echoSensor, HIGH);
}
In your main loop you will read the distance and use it to calculate a frequency. I’m not giving you the full code for the main loop, you need to play with it and figure it out.
You will be using a new built in Arduino function called map which takes a floating point value, an input range of numbers and an output range of numbers. It scales the input range to fit the range of the output for example:
map (input, #1#, #2#, #3#, #4#) where #1# is the low input range, #2# is the high input range, #3# is the low output range and #4# is the high output range.
map(pitch_distance, 0, 10, 0, 100) would take the values between 0 and 10 and stretch them to match a range of 0 to 100 so 0 would become 0, 1 would become 10, 2 would become 20, etc.
I am leaving the values blank but for reference music notes are generally in a range of 440Hz to 2186 Hz. You distance measurement will be based on how you plan to play the Theremin so you need to use the Serial console to find your input range.
You will also be using a new command tone(pin, pitch, duration) you see we got the pitch from the map function, and the PIN is set from the SPEAKER variable. you can play with your own values for the duration #5#. There is also a defined LAG which is the time from the start of one loop to the start of the next. You probably want LAG equal to or greater then the tone duration, but you can play around and see what you find sounds best.
void loop()
{
//Calls a function to sense the distance (in time) to an object and stores it
pitch_time = senseDistance(PITCH_SENSOR_TRIG, PITCH_SENSOR_ECHO);
Serial.println(pitch_time);
//Uses the time to calculate the distance in cm
pitch_distance = pitch_time/58;
//Map the distance to the frequencies that you want to output
pitch = map(pitch_distance, #1#, #2#, #3#, #4#);
Serial.print("Sending pitch ");
Serial.println(pitch);
tone(SPEAKER, pitch, #5#);
delay(LAG);
}
The items in BOLD in the code and the things you will want to adjust to get your Theremin sounding how you like it. Bring it back next week and we will have a Theremin concert.
