Line Sensing. QRE1113 Reflectance Sensor + Arduino
The QRE1113 is a common reflectance sensor often used in robotic line followers. The sensor works by shining an IR LED down and seeing how much of that light bounces back using a phototransistor. Because dark colors will bounce back less of the light, the sensor can be used to tell the difference between white and black areas. So an array of these can be used to help a robot determine where a dark line is on the ground so it can follow it. But they can also be used to determine proximity under an inch.
Both Pololu and Sparkfun sell digital and analog models of a QRE1113 breakout board. The analog version is very simple, it just outputs an analog voltage on the signal pin relative to how much light was reflected. And you might think the digital version, especially being called a line sensor by sparkfun, would just output HIGH when it sees a line, and LOW when it does not. But this is not the case.
The digital version is designed for times you do not have an analog input on your microcontroller but still need an analog reading of how much light was reflected. It does this by allowing you to charge a capacitor on the board, and then timing how long it takes to discharge. The more light that is reflected, the less time it takes to discharge the capacitor. In my tests, the times ranged between 10 microseconds (10/million) to 2.5 milliseconds, so this is all done very quickly and wont put much of a delay in your code.
Hooking Them Up
For more detail: Line Sensing. QRE1113 Reflectance Sensor + Arduino