The Physics: How a Sensor Fotoelektrik Detects Objects
A sensor fotoelektrik (photoelectric sensor) operates on a straightforward optoelectronic principle: an emitter projects a beam of light (typically red visible LED or infrared), and a receiver (a phototransistor or photodiode) measures the light that returns or passes through. When an object interrupts or reflects this beam, the receiver's internal resistance changes, triggering the sensor's internal comparator to switch its output transistor. This allows for non-contact detection at ranges anywhere from a few millimeters to over 50 meters, far exceeding the capabilities of inductive or capacitive proximity switches.
Industrial models generally fall into three sensing modes. Through-beam separates the emitter and receiver into two housings; an object breaks the beam, offering the longest range and highest reliability. Retro-reflective houses both in one unit and bounces the beam off a specialized reflector tape; it is easier to wire but can be fooled by highly reflective objects. Diffuse relies on the object itself to scatter light back to the receiver, making it the most versatile for general object detection but highly dependent on the target's color and surface finish.
Output Signals and the 24V-to-3.3V Interfacing Problem
The most critical mistake makers make when integrating industrial automation hardware into microcontroller projects is ignoring the voltage domain mismatch. A standard industrial sensor fotoelektrik operates on a 10-30V DC supply and outputs either a 24V digital signal (PNP/NPN) or a 0-10V / 4-20mA analog signal. Feeding a 24V PNP output directly into an ESP32 GPIO will instantly destroy the silicon.
For digital object detection (simply knowing if an object is present), the output is a PNP open-collector transistor. When triggered, it sources the supply voltage (24V) to the black output wire. To interface this safely with a 3.3V ESP32, you must use an optocoupler like the PC817 to galvanically isolate the 24V industrial loop from your 3.3V logic.
Always verify against the specific datasheet, but IEC 60947-5-2 standardizes these colors for DC sensors:
- Brown (+V): 24V DC Power Supply
- Blue (0V): Ground / Common
- Black (Out): Control Output (PNP sources 24V, NPN sinks to 0V)
- White (Mode): Normally Light-ON / Dark-ON select (tie to Blue or Brown depending on spec)
Optocoupler Interfacing Steps for PNP Digital Output
- Connect the sensor Brown wire to the positive terminal of a 24V DC power supply.
- Connect the sensor Blue wire to the power supply ground.
- Connect a 2.2kΩ current-limiting resistor from the sensor's Black (Output) wire to the Anode (Pin 1) of a PC817 optocoupler.
- Connect the PC817 Cathode (Pin 2) to the 24V power supply ground.
- On the ESP32 side, connect the PC817 Emitter (Pin 4) to ESP32 GND.
- Connect the PC817 Collector (Pin 3) to your chosen ESP32 GPIO (e.g., GPIO 4).
- Enable the internal pull-up resistor in your ESP32 code, or add an external 10kΩ pull-up to 3.3V. When the sensor triggers, the optocoupler conducts, pulling the GPIO LOW.
Signal Math: Converting Raw ADC to Physical Distance
If your application requires measuring how far away an object is rather than just detecting its presence, you need an analog diffuse or laser-triangulation sensor fotoelektrik with a 0-10V output. Let's assume a sensor configured for a 100mm to 1000mm range, where 0V equals 100mm and 10V equals 1000mm.
Because the ESP32 ADC maxes out at ~3.1V safely, we must use a voltage divider. Using R1 = 22kΩ and R2 = 10kΩ, the division ratio is 10 / (22 + 10) = 0.3125. At the sensor's maximum 10V output, the ESP32 sees 3.125V, which is perfectly safe.
The Raw-to-Unit Calculation
The ESP32 12-bit ADC yields raw values from 0 to 4095. However, the ESP32 ADC is notoriously non-linear at the extremes. For precision, use Espressif's esp_adc_cal library (or analogReadMilliVolts() in the Arduino core) to get a true millivolt reading, but here is the raw mathematical derivation if you are reading the raw 12-bit integer:
- Calculate measured voltage at the pin:
V_pin = ADC_raw * (3.3 / 4095) - Scale back to actual sensor voltage:
V_sensor = V_pin / 0.3125(which isV_pin * 3.2) - Map voltage to physical distance:
The span is 900mm (1000mm - 100mm) across a 10V range.
Distance_mm = (V_sensor / 10) * 900 + 100
Combined Formula for your C++ code:
float v_pin = analogRead(34) * 0.0008058; // 3.3 / 4095
float v_sensor = v_pin * 3.2;
float distance_mm = (v_sensor * 90.0) + 100.0;
Defeating Common Interference Sources
Photoelectric sensors are highly susceptible to environmental noise. If your ESP32 is logging phantom triggers or erratic distance jumps, check these three culprits:
- Ambient Sunlight (IR Saturation): Direct sunlight contains massive amounts of infrared energy that can blind the receiver's phototransistor, causing a diffuse sensor to read 'empty' even when an object is present. Fix: Use a sensor with a built-in optical bandpass filter (most modern red-light models have this) or mount a physical hood over the receiver lens.
- Specular Reflections: If you are using a retro-reflective sensor to detect shiny objects (like aluminum cans or mylar), the object might bounce the light back to the receiver just like the reflector tape does, causing the sensor to fail to trigger. Fix: Switch to a polarized retro-reflective sensor, which uses a quarter-wave retarder filter to ignore light that hasn't bounced off the specialized reflector tape.
- Dust and Oil Accumulation: In workshop environments, aerosolized oil and sawdust coat the lens, attenuating the beam and shrinking your effective range by up to 50%. Fix: Select a model with an IP67 or IP69K rating and wipe the lens with isopropyl alcohol during routine maintenance. Never use abrasive solvents that will cloud the acrylic or glass lens.
Decision Tree: Selecting Your Exact Part Number
Do not waste time guessing which mode you need. Follow this decision path to select the correct sensor fotoelektrik for your embedded project.
| Application Requirement | Required Sensing Mode | Recommended Part Number |
|---|---|---|
| Counting high-speed objects on a conveyor (up to 1000 Hz) with absolute reliability. | Through-Beam (Separate Tx/Rx) | Omron E3Z-T81 (PNP, 15m range) |
| Detecting clear glass bottles or transparent packaging film. | Retro-reflective with Polarizing Filter | Sick PR18-2 (Transparent object detection) |
| Measuring exact physical distance (e.g., bin level monitoring) via ESP32 ADC. | Analog Laser Triangulation (0-10V) | Baumer OADM 20I (0-10V output) |
| General object detection, presence/absence, end-of-arm tooling on a budget. | Diffuse Reflective (Digital PNP) | Omron E3Z-D82 |
The Default Pick
If your project simply requires knowing if an object is within a short distance (up to 1 meter) without needing complex alignment or analog math, stop evaluating and buy the Omron E3Z-D82. It is a diffuse-reflective, PNP output sensor that operates on 12-24VDC. It features an easily adjustable potentiometer on the side for setting the trigger threshold, an IP67 housing that survives harsh environments, and a response time of 1ms. Wire it through the PC817 optocoupler circuit detailed above, set your ESP32 GPIO to INPUT_PULLUP, and you will have a bulletproof, industrial-grade digital input for under $45.
For deeper technical specifications on the E3Z family wiring diagrams and response curves, refer to the Omron Automation Photoelectric Sensors documentation. For handling the ESP32's ADC non-linearity in your code, consult the official Espressif ADC Calibration API guide.






