A light theremin

Site: ΕΛ/ΛΑΚ Moodle
Course: 3D printing with circuits and Arduino
Book: A light theremin
Printed by: Guest user
Date: Wednesday, 8 May 2024, 7:32 AM

Description

Make a pseudo-theremin instrument that transforms light to sound.

1. Theremin

The Theremin (http://en.wikipedia.org/wiki/Theremin) is a musical instrument that makes spooky synthesized sounds as you wave your hands in front of it.



We will need the speaker for output and the photoresistor and a 10k resistor that are going to control the pitch.



2. Schematic

The circuit is similar to the Sensing light circuit of the last module, with the addition of a speaker.


3. Sketch

The sketch is pretty straightforward:

const int speakerPin = 12;
const int photocellPin = 0;
 
void setup()
{
}
 
void loop()
{
  int reading = analogRead(photocellPin);
  int pitch = 200 + reading / 4;
  tone(speakerPin, pitch);
}

We simply take an analog reading from A0, to measure the light intensity.

We add 200 to this raw value, to make 200 Hz the lowest frequency and simply add the reading divided by 4 to this value, to give us a range around the 440Hz.

4. map()

In that setting, we played around with the tone value and arbitrarily chose to have a range close to 440Hz.
If we would like to widen the range to a few octaves, then we would need to somehow adjust the reading value to what a pre-defined frequency range.

One way to do this is using the map() function.

map() re-maps a number from one range to another. That is, a value of fromLow would get mapped to toLow, a value of fromHigh to toHigh, values in-between to values in-between, etc.
The map() function automatically scales one sensor value and converts it to another range of values.
map(value, fromLow, fromHigh, toLow, toHigh)

In this example we are going to convert ambient light readings to the range of 220Hz (100Hz is the minimum speaker frequency) to 880Hz.

We will change this line:
int pitch = 200 + reading / 4;
to one like this:
int pitch = map(reading, 700, 1023, 220, 880);

Try to debug the sketch using the SerialMonitor and adjust your map() parameters according to the ambient light readings you take and the aesthetic result you want to achieve.