Mini IR Theremin

Jump To:
The Theory
           The theory for how this musical instrument actually works will be split into two sections. This first seection will explain how the IR Proximity sensor takes and outputs data and how the microcontroller translates that data into usable distances units (centimeters or inches).
           As discussed before the IR sensor has three connections, power, ground and output. The output will give us a specific analog voltage that correlates to the distance the sensor is away from an object. The voltage curve for Output Voltage to Detected Distance (for the 2Y0A21) can be see in the picture below.

Since we know the expected voltage output for certain distances we could brute-force the system and hard code the expected voltages but that wouldn't be the scientific approach. Both the 2Y0A21 and the GP2D120 have the same output curve.

Finding A Linear Model

           To make it easier, some math people out there decided to use their brains and linearize the curve seen above and give us a nice equation that, for the valid portions of sensor data, has the same x,y data graph. Many different equations exist out in the wild, so I'll just pick one of them. The picture below shows you the graph of the formula function, it's the same as the output curve already seen above. Please note, if you choose to use the GP2D120 sensor for this project, the formula for that sensor is different.

           The formula seen above uses a 10-bit A-to-D value, that number should always be between 600 and 50, representing the 3v to 0.4v range that the IR sensor can output. Also, notice that in the graph above voltage is used and not the raw A-to-D value like in the formula. The PIC will see the output voltage from the sensor as a 10-bit A-to-D value, never as a single 3v or 2v number.
           What this tells us is that we have a formula that we can apply all input voltages to and get reliable results, instead of hardcoding different voltages into the system, which would work too, but would require more effort.