This project combines the photoresistor and the piezo element to create a theremin. This fun project demonstrates one possibility when combining input and output components.
The theremin is a musical instrument that can be controlled without any physical contact! The theremin typically has one or two antennas that are set to detect the hand position of the performer. The hand movement and position then controls one or more oscillators that are usually linked to the frequency and volume output.
The theremin was invented in 1928 by Leon Theremin, a Russian inventor. It has a distinct, recognizable sound, and was a staple of early film soundtracks due to the eerie sounds that it could generate.
You’ll be using the piezo element and the photoresistor to create the theremin. There are some additional challenges and options, but these components are the core of the instrument.
The supplies are a combination of what you used for the introductory piezo element synth and the photoresistor circuit.
Here’s what you’ll be using:
This project builds on the skills you’ve used while working with the projects for the piezo and the photoresistor. The steps are split into two parts: circuit assembly and code for the sketch.
This is the diagram and steps for the basic activity. For the Experimenter Challenges, you may need to build on this basic circuit to solve the problem!
Use this breadboard diagram as your guide:
Note: The analog input connection for the photoresistor should be placed on the breadboard in between the 10k resistor and the photoresistor leg.
For example, let’s say the photoresistor leg is on 27e on the breadboard. You’d want to place the resistor on 27a and the analog input jumper on 27c.
The photoresistor input is going to be used to control the pitch of the piezo element. There will be some additional challenges where you’ll be able to modify some of the functionality, but the base sketch will be using the photoresistor to control pitch.
The basic code is a slightly modified version of the Adafruit Pseudo Theremin lesson. Here’s the sketch:
/*
* Code based on the Adafruit Arduino Lesson 'Pseudo Thermin' by Simon Monk
* Adafruit Arduino - Lesson 10. Pseudo Thermin
* https://learn.adafruit.com/adafruit-arduino-lesson-10-making-sounds/arduino-code
*/
int photoPin = 0; //this is the pin the photoresistor is attached to
int speakerPin = 9; //this is the pin the piezo is attached to
void setup(){
}
void loop(){
int reading = analogRead(photoPin);
int pitch = 200 + reading / 4; // no need to map because the value is divided by 4
tone(speakerPin, pitch);
}
Code language: JavaScript (javascript)
This basic sketch doesn’t contain any new commands! Instead, it’s a combination of several concepts you’ve used in previous projects. Let’s take a moment to look at the code:
Two global variables are created, photoPin
which is the pin that the photoresistor is attached to, and speakerPin
which is the pin that the piezo is attached to.
There isn’t any additional code inside the void setup()
. The core of the code is inside the void loop()
.
The first action in the loop is to use analogRead()
to get a reading from the photoresistor. This value is stored inside a local variable named reading
.
Next, there is another local variable named pitch
that is equal to a base of 200 + the photoresistor reading divided by 4. Having a base value of 200 ensures that the pitch being output is no lower than 200 Hz.
Note that this code doesn’t use map()
.
The example sketch provides the basic functionality for the theremin. There are several things that you can modify once you understand the concepts involved.
Here are some suggestions: