Working with the Sharp GP2D12 (B-beam) controllers

by Stephen Hobley on May 29, 2008

I’ve been asked to write an article for Make magazine on MIDI controllers using lasers and light. It’s come at the right time, as I’ve been meaning to try out the Sharp GP2D12 sensors I bought from Trossen Robotics. Anyone that has played a Roland synthesizer with a D-Beam controller will recognise these immediately. They fire out a pulse of IR light at 25Hz and then triangulate the position of the reflected dot to get a distance measurement.

Having played around with them for a while, there are two problems that need to be addressed before they can be used for musical applications:

The first is that the output can be noisy – this is in part due to the fact that quite a lot of current is drawn every 40ms when the IR LED is fired – this can get into the output signal as a spike. First off a 300uF tantalum cap and 0.1uF regular cap can be put across the power rails close to the sensor input – this helps to smooth the power going in. Additionally this basic filter circuit can be put across the output to smooth out any remaining spikes.

[Circuit -coming soon!!]

Also only sample the output every 40ms or so, as the output is only updated this often. It’s also possible to filter in software like this:

while (!sample_available())

average = (average < < 3) - average + get_sample(); average >>= 3;
// do something with the average here


This gives each sample an exponential decay.

The second problem is that the output from the device is non-linear – so we need to apply a ‘linearization’ to the output. Thankfully someone has already done the hard part for us – the functions are as follows:


Range (cm) = (6787 / (V – 3)) – 4

GP2D120 (long range):

Range (cm) = (2914 / (V + 5)) – 1

[Taken from]

Applying this function as either integer or floating point math helps in getting a value that can be further scaled to modify MIDI pitch bend (0-16383), or continuous controller information (0-127)

One final tip – be careful where you place the sensors – putting them too close to a side wall, or in the wrong orientation can affect the reading accuracy, but when used properly these are a valuable addition to any MIDI hackers toolkit.


Leave a Comment

Comments links could be nofollow free.

Previous post:

Next post: