Arduino Light Theremin (Starter Kit Project #6)

We will use a phototransistor as a light sensor to control a piezo and produce sound – resembling the functionality of an actual theremin.

Setup

Here’s what we need:

  • Arduino UNO + Breadboard
  • Jumper wires/cables
  • 1 10kΩ resistor
  • Piezo
  • 1 phototransistor

Layout

Here’s what the instrument should look like, as per the Arduino projects book.

And here is what I made:

Code

The calibration of the sensor is the most important part in this mini-project.

int sensLow = 1023;
int sensHigh = 0;

void setup() {
    while (millis() < 5000) {
        sensVal = analogRead(A0);
        if (sensVal > sensHigh) {
            sensHigh = sensVal;
        }
        if (sensVal < sensLow) {
            sensLow = sensVal;
        }
    }
}

The millis function returns the amount of time for which our board has been working. Thus, we calibrate for the first five seconds by taking in the minimum and maximum possible sensor input values. These will be used to scale our input values later on.

void loop() {
  sensVal = analogRead(sensPin);
  int pitch = map(sensVal, sensLow, sensHigh, 50, 4000);
  tone(8, pitch, 20);
  delay(10);
}

Here, we take in our input value and map it to the appropriate frequency. Then, using the tone function, we output sound using our piezo (which vibrates as per the frequency provided by tone()). You can play around the frequency and mapping values just for fun.

Arduino Color Mixing Lamp (Starter Kit Project #4)

This entire thing is fairly simple. We will use phototransistors to input the RGB components of any light source to our arduino. The arduino will then convert these to digital values, and combine the RGB components to operate a four pin, RGB LED. All we have to do is input to our uno, process the data (fancy word for dividing by 4), and output.

Setup

Here’s what we’ll need:

  • Arduino UNO
  • Breadboard
  • 3 220 Ω resistors, 3 10 kΩ resistors.
  • Jumper wires/cables
  • 4 pin RGB LED
  • RGB color filters
  • 3 phototransistors

Layout

Here’s what the circuit should look like (from the arduino starter kit projects book).

And here is my build:

You can see that I didn’t use the wooden components already in the starter kit to appropriately connect the filters to the phototransistors. This is because those components were too small (probably a defect in the kit), fitting the screens caused them to fly across the room at random intervals.

Code

You can find the full code here. I’m not discussing the setup part here.

void loop() {
    redSensVal = analogRead(redSensPin);
    delay(5);
    greenSensVal = analogRead(greenSensPin);
    delay(5);
    blueSensVal = analogRead(blueSensPin);
}

Here we’re reading in the rgb components of the light source under scrutiny. A delay of five milliseconds has been taken as the arduino takes a while to process analogue inputs and convert them into digital values.

void loop() {
    // ...
    
    redVal = redSensVal/4;
    greenVal = greenSensVal/4;
    blueVal = blueSensVal/4;

    analogWrite(redLEDPin, redVal);
    analogWrite(blueLEDPin, blueVal);
    analogWrite(greenLEDPin, greenVal);
}

Now we divide by four because our analogue input corresponds to digital values between 0 and 1023. However, our output LED requires digital values between 0 and 255. Given that our output LED requires analogue inputs, we use the UNO’s built in DAC to display our combined RGB components.

Note

Many people (including me) initially thought that this entire thing wasn’t working. This isn’t the case. The fact is that the phototransistors that come with the starter kit are incredibly insensitive. They registers values of o,0,0 for room lights (and most lights), which is why there is no discernible output unless one uses a strong flashlight (built-in in smartphones) or a decent light source.

Here’s a small demo. You can visibly see that iphone flashlights have strong blue and green components.

Mini-Demo