Have you ever wondered how automatic doors open as you approach, or how your smartphone screen turns off when held to your ear? The magic behind these conveniences is often a proximity sensor. While commercial sensors are highly refined, building your own basic proximity sensor is an excellent electronics project that demystifies the technology and enhances your DIY skills. This tutorial will guide you through creating a simple infrared (IR) proximity sensor, a common and accessible type.
Before starting, it's crucial to understand the basic principle. An IR proximity sensor works by emitting an invisible infrared light beam. When an object comes near, the light reflects back to a receiver component. The sensor then detects this reflected light, signaling the presence of the object. Our project will use this principle with discrete components.

Let's gather the necessary components. You will need an IR LED (Light Emitting Diode), a photodiode or phototransistor (as the IR receiver), a 100-ohm resistor, a 10k-ohm resistor, an LM358 operational amplifier (op-amp) IC, a breadboard for prototyping, jumper wires, and a 5V power source like a USB cable or battery pack. Having a multimeter on hand is also helpful for testing.
Begin by placing the LM358 op-amp on the breadboard, straddling the center gap. Identify its pinout: Pin 8 is for Vcc (positive supply), Pin 4 is for GND (ground). We will use only one of its two internal amplifiers. Connect Pin 8 to the positive rail of your breadboard and Pin 4 to the negative (ground) rail. Then, connect your 5V power source's positive wire to the positive rail and its negative wire to the ground rail.
Now, set up the IR transmitter. Take the IR LED. Its longer lead is the anode (positive). Connect this anode to the positive rail through the 100-ohm resistor, which limits the current to protect the LED. Connect the shorter cathode lead (negative) directly to the ground rail. This circuit will cause the IR LED to constantly emit infrared light.
Next, build the receiver circuit. Place the phototransistor on the breadboard. A phototransistor typically has a longer collector lead and a shorter emitter lead. Connect the emitter lead to the ground rail. Connect the collector lead to one end of the 10k-ohm resistor. Connect the other end of this 10k-ohm resistor to the positive rail. This resistor acts as a pull-up resistor. The junction point between the phototransistor's collector and the 10k-ohm resistor is our signal point. Connect this point to Pin 3 (the non-inverting input) of the LM358 op-amp.
We now need to configure the op-amp as a comparator, which will compare our signal voltage against a reference. We create a reference voltage using a voltage divider. Use two 10k-ohm resistors in series between the positive rail and ground rail. The midpoint between these two resistors will provide approximately 2.5V. Connect this midpoint to Pin 2 (the inverting input) of the LM358.
Finally, we need the output. Connect a standard LED (with a suitable current-limiting resistor, e.g., 220 ohms) to the output for visual indication. Connect the resistor to Pin 1 (the output pin) of the LM358. Connect the other side of the resistor to the anode of the LED, and connect the LED's cathode to ground. This LED will light up when the sensor detects an object.
It's time to test and calibrate your circuit. Power it up. The IR LED should be on, though you cannot see its light. Point the IR LED and phototransistor in the same general direction, side-by-side. Initially, the output LED should be off because no reflected IR light is hitting the phototransistor. Now, bring a white object (which reflects IR well) close to the front of the sensors. The output LED should light up. If it doesn't, adjust the distance of the object or slightly tweak the angle of the components to ensure the reflected light hits the phototransistor. The sensitivity can be adjusted by changing the value of the reference voltage resistor divider or the 10k-ohm resistor in the receiver circuit.
This basic sensor has limitations. It can be affected by ambient light, especially sunlight which contains IR. For a more robust sensor, you can modulate the IR signal (turning it on and off at a high frequency) and have the receiver circuit tuned to that frequency, but that involves a more complex circuit. This project, however, perfectly illustrates the core concept.
Once your prototype works on the breadboard, you can solder the components onto a perfboard or a custom PCB for