Light to Sound

Making music by varying lights is a very popular project. The effect is often called a Light Theremin. The original Theremin instrument by Léon Theremin used tuned resonant radio frequency circuits converting changes in resonance to sound. Using a light changes to make different sounds is quite a bit easier.

Circuit Playground has a perfect sensor for detecting light in the upper left of the board (the part with the eye next to it). Light falling on the sensor changes the sensors resistance, changing the voltage the microcontroller reads. The value read by the sensor may be from 0 to 1023.

To make music, we will map the number received from the light sensor to the frequency ranges we looked at in The Sound of Music page (notes C3 to A6). The program will map to values that are not true musical notes though.

One more change is to use the Circuit Playground slide switch. There are times when the people around you do not want to hear the sounds (believe me). The light to sound code is only executed if the slide switch is moved to the "+" side.

// Adafruit Circuit Playground - Light Theremin    Support Open Source, buy at Adafruit
//   2016-08-07 Version 1  by Mike Barela for Adafruit Industries
// Uses the CircuitPlayground library to easily use the full functionality of the board

#include <Adafruit_CircuitPlayground.h>

void setup() {
  CircuitPlayground.begin();   // initialize the Circuit Playground library
}
 
void loop() {
   uint16_t value, sound;
   if(CircuitPlayground.slideSwitch()) {      // if the slide switch is on
     value = CircuitPlayground.lightSensor(); //   read the light sensor
     sound = map(value, 5, 1000, 131, 1760);  //   map light values to music values
     CircuitPlayground.playTone(sound, 100);  //   play sound for 100 milliseconds
   }
}

To ensure each output frequency is a musical note, one could create an array of musical notes and have the light values mapped to one of the musical notes in the array and output the musical note.

I chose the musical note range so sounds did not get too low (low sounds may create more of a click on the speaker) or too high (making a sound that is rather high or a whine, harsh on the ear). Feel free to adjust the last two values in the map function to change the sound values. Use the notes in pitches.h on the previous page to help you look at frequency ranges to play.

Applications

Ways in which light to sound may be used:

  • Night Alarm - if you go to sleep but want to be awakened if someone enters a room.
  • Box Alarm - if you want to be alerted if someone goes into your lunchbox, toolbox, etc. place Circuit playground inside and it will make a high pitched sound when someone opens the box.
  • Musical Instrument - be the Leo Theremin of the 21st century
Last updated on 2016-08-14 at 03.40.12 AM Published on 2016-08-17 at 02.09.02 PM