How an IR Sensor Module Actually Works
Standard IR sensor modules (like the ubiquitous FC-03, HW-201, or generic 3-pin obstacle avoidance boards) rely on active infrared reflection. An onboard IR LED, typically emitting at a 940nm wavelength, floods a localized area with invisible light. A paired photodiode or phototransistor sits adjacent to the LED, shielded by a small plastic bezel to prevent direct optical crosstalk. When an object enters the sensor's field of view, the 940nm light bounces off the target's surface and strikes the phototransistor, generating a proportional photocurrent.
The raw photocurrent is inherently analog and highly non-linear, but these modules include an LM393 dual voltage comparator on the back PCB. The LM393 compares the phototransistor's voltage against a reference threshold set by a blue multi-turn potentiometer. This gives you two simultaneous outputs: a clean digital logic signal (DO) that snaps HIGH or LOW at the potentiometer's trip point, and a raw analog voltage (AO) that scales continuously with the intensity of the reflected IR light.
Pinout, Wiring, and Power Requirements
Wiring an IR sensor module seems trivial until you hit the LM393's open-collector output architecture. The digital output (DO) pin can sink current to ground when triggered, but it cannot source voltage when idle. If you configure your microcontroller pin as a standard INPUT, the pin will float when no object is present, resulting in ghost triggers and erratic interrupts. You must enable the internal pull-up resistor.
| Module Pin | Function | ESP32 DevKit Connection | Arduino Uno Connection | Critical Notes |
|---|---|---|---|---|
| VCC | Power Supply | 3.3V Pin | 5V Pin | Supply range is 3.3V to 5V. Use 3.3V on ESP32 to keep AO within ADC limits. |
| GND | Ground | GND | GND | Ensure a common ground with the microcontroller. |
| DO | Digital Output | GPIO 4 (or any) | Digital Pin 2 | Open-collector. MUST use INPUT_PULLUP in code. |
| AO | Analog Output | GPIO 34 (ADC1_CH6) | A0 | Outputs 0V to VCC. Avoid ESP32 ADC2 pins if using WiFi. |
Always route the AO pin to an ADC1 channel (GPIO 32-39) on the ESP32. The ADC2 channels (GPIO 0, 2, 4, 12-15, 25-27) are hijacked by the WiFi driver during active communication. If you wire AO to GPIO 25 and then turn on WiFi, your analog readings will instantly flatline to zero.
Decoding the Output: Digital Logic vs. Analog Math
The digital output requires zero math. It is a simple logic LOW (sunk to GND) when an object is detected, and logic HIGH (pulled up to VCC) when the path is clear. You adjust the physical trip distance by turning the blue potentiometer with a Phillips screwdriver while holding a target at the desired distance.
The analog output, however, requires calibration and scaling if you want to extract a physical distance. The intensity of reflected light follows an inverse-square law relative to distance. Because the phototransistor's voltage output is proportional to light intensity, the voltage ($V$) relates to distance ($d$) as $V \propto 1/d^2$. Therefore, distance is inversely proportional to the square root of the voltage.
Here is the raw-to-unit math to convert an analog reading into an estimated distance in centimeters:
// ESP32 Arduino Core Example
const int irPin = 34;
const float K_CONSTANT = 18.5; // Must be empirically calibrated for your target's color
void setup() {
Serial.begin(115200);
analogReadResolution(12); // 12-bit (0-4095)
}
void loop() {
// Use analogReadMilliVolts() to bypass the ESP32's raw ADC non-linearity
uint32_t mV = analogReadMilliVolts(irPin);
float voltage = mV / 1000.0;
float distance_cm = 0;
if (voltage > 0.15) { // Ignore noise floor below 150mV
distance_cm = K_CONSTANT / sqrt(voltage);
} else {
distance_cm = 999.0; // Out of range
}
Serial.printf("Voltage: %.2f V | Est. Distance: %.1f cm\n", voltage, distance_cm);
delay(100);
}
Calibration requirement: The $K\_CONSTANT$ is entirely dependent on the target's albedo (reflectivity). You must place a standard white piece of printer paper exactly 10 cm from the sensor, read the voltage, and solve for $K$ ($K = 10 \times \sqrt{V_{10cm}}$). If you point the sensor at black electrical tape, the voltage will plummet, and the math will falsely report the object as being further away.
Defeating Interference: Sunlight, Albedo, and Flicker
Standard 940nm IR modules are notoriously fragile in uncontrolled environments. Understanding the interference sources is mandatory before deploying these sensors outside a laboratory bench.
- Sunlight Swamping: Direct sunlight contains roughly 500 W/m² of broadband infrared radiation. A 20mA onboard IR LED cannot compete. In direct sun, the phototransistor saturates completely, the LM393 comparator locks LOW, and the sensor behaves as if an object is permanently pressed against its face. Fix: Use physical optical shrouds (heat-shrink tubing over the photodiode) or switch to modulated 38kHz IR receivers.
- Albedo Shifts: As noted in the math section, distance calculations assume a constant reflectivity. A robot navigating from white tile onto a dark rug will experience a massive voltage drop, interpreting the rug as a sudden drop-off or obstacle. Fix: Rely on the digital DO pin for simple tripwires, or implement multi-sensor sensor fusion.
- 50/60Hz AC Flicker: Incandescent and halogen bulbs emit massive IR spikes that pulse at twice the mains frequency (100Hz or 120Hz). This introduces high-frequency noise into the AO pin. Fix: Implement a software low-pass filter or use hardware oversampling (take 16 rapid reads and average them).
Decision Tree: Which IR Sensor Module Should You Buy?
Not all IR modules are created equal. Use this decision matrix to select the exact part number for your build, avoiding the trap of buying a $1 sensor for a job that requires an $8 sensor.
| Application Scenario | Environment | Required Output | Recommended Module / Part Number | Approx. Cost |
|---|---|---|---|---|
| Line tracking / edge detection | Indoor, controlled lighting | Digital (Logic Level) | TCRT5000 (4-pin reflective optical sensor) | $1.00 |
| Budget obstacle tripwire | Indoor, away from windows | Digital (Adjustable threshold) | FC-03 / HW-201 (LM393 based generic module) | $1.50 |
| Precise distance mapping | Indoor, variable surface colors | Analog (Linear voltage to cm) | Sharp GP2Y0A21YK0F (10cm to 80cm IR Distance Sensor) | $8.00 |
| Outdoor / Sunlight obstacle avoidance | Outdoor, direct sunlight exposure | Digital (Sunlight rejecting) | Pololu Digital Distance Sensor (Item #2578) | $6.50 |
If you are building an indoor hobby robot and strictly need a digital tripwire on a shoestring budget, buy the FC-03 LM393 IR Obstacle Module. However, if your project will ever operate near a window, outdoors, or requires immunity to sunlight swamping, bypass the cheap LM393 boards entirely. Your concrete pick should be the Pololu Digital Distance Sensor (Item #2578). It uses a modulated IR emitter and a specialized receiver tuned to reject ambient 940nm sunlight, solving the fundamental flaw of standard generic modules for just $6.50.
For deeper integration details regarding ESP32 ADC calibration and linearization, refer to the official Espressif ADC Oneshot Driver Documentation. Always verify your specific module's potentiometer orientation before applying power to avoid shorting the wiper to VCC.






