You need to know if an object is close to another one but without using a contact switch? The TCRT5000 sensor is a great and cheap option for that! It essentially consists into 2 parts bundled together: an infrared emitter and a photo-transistor.

Drawing 1: No object close to sensor
Drawing 2: An object is close to the sensor, so the infrared light is reflected to the photo-transistor.
You can’t really use that sensor to measure distance since it can only answer one question: Is an object close to the sensor or not? Well, actually, it could be used to measure distances between 2mm and 1.5cm or so.
TRCT5000 Breakout
Creating a breakout for the TRCT5000 is very simple. You simply implement this schematic:
The corresponding board looks like this:
Wiring the breakout to your Arduino
Programming your Arduino
To use this sensor, you simply call analogRead() as is:
#define LED_PIN 6 #define TCRT5000_SENSOR_PIN A2 void setup() { pinMode(LED_PIN, OUTPUT); Serial.begin(9600); } void loop() { int reading = analogRead(TCRT5000_SENSOR_PIN); // read is 0..1024, write is 0..255 int reading = reading / 4; analogWrite(LED_PIN, reading); Serial.println(reading); }
Please, can you provide a link to download the PCB?