How Does an IR Sensor Work?
Share
An infrared (IR) sensor is one of the first parts most makers reach for, and Compoden's AI build assistant is a handy companion when you are wiring one up for the first time. IR sensors are cheap, fast, and turn up everywhere: line-following robots, obstacle detectors, and touchless switches. This article explains how they actually work, what kind of signal they give you, and how to read that signal on an Arduino or ESP32.
The working principle
Infrared light sits just beyond the red end of the visible spectrum, so your eyes cannot see it but a sensor can. A typical reflective IR module has two parts: an IR emitter (an LED that shines invisible infrared light) and an IR receiver (a photodiode or phototransistor that reacts to infrared light landing on it). The emitter beams light outward. When an object is in front of the sensor, some of that light bounces back and reaches the receiver. A surface that is light-coloured or shiny reflects more; a dark, matte surface absorbs most of it and reflects very little.
The receiver's job is to convert the amount of returning infrared light into an electrical change. More reflected light means more current flows through the photodiode. The module compares this against a threshold to decide whether an object is present or absent.
How it outputs a signal
Most hobby IR modules include a small comparator chip and a trimmer potentiometer. The comparator looks at the receiver's reading and the threshold you set with the trimmer, then produces a clean digital output: one voltage level when an object is detected and the other when it is not. This is why the output pin is usually a simple HIGH or LOW. Some modules also expose an analog output, which gives you a varying voltage that rises and falls with the strength of the reflection, useful when you want more than a yes or no answer.
How you read it on a microcontroller
For a digital module, connect VCC and GND to power and ground, then wire the output pin to any digital input on your Arduino or ESP32. Read it with a digital read call and you get a HIGH or LOW that tells you object or no object. Turn the trimmer to adjust how close an object must be before the output flips. For an analog module, connect the output to an analog input pin and read the value with an analog read; a stronger reflection gives a larger number, so you can judge rough distance or surface brightness. On a line-following robot, several IR sensors point at the floor and report whether each one sees the dark line or the light surface.
Common uses
IR sensors are popular for obstacle avoidance on small robots, edge detection so a robot does not roll off a table, counting items on a conveyor, line following, and simple proximity triggers like an automatic hand-sanitiser dispenser. They respond quickly and cost very little, which is why they appear in so many starter projects. Their main limits are sensitivity to ambient light and to surface colour, so a black object can be hard to detect at the same distance as a white one.
Build it with Compoden's AI
Picking the right IR module and getting the wiring correct is where beginners often stumble. Compoden's AI build assistant helps you match an IR sensor to your project, explains which pin goes where on your Arduino or ESP32, and suggests starter code to read the output. Describe what you are building, a line follower, an obstacle detector, or a counter, and it points you to suitable parts and a clear wiring plan. You can browse the full range at our collection and ask the assistant any follow-up question as you go.
Ready to start? Browse IR sensors and starter kits at Compoden and let the AI build assistant guide your first build.
What does an IR sensor detect? An IR sensor detects infrared light. A reflective module shines invisible infrared and senses how much bounces back from a nearby object, so it can tell whether something is in front of it and roughly how reflective that surface is.
Is an IR sensor output digital or analog? Most hobby IR modules give a digital HIGH or LOW output from an onboard comparator, with a trimmer to set the threshold. Some modules also provide an analog output that varies with the strength of the reflected light.
Can an IR sensor measure exact distance? Not reliably. An IR module tells you whether an object is within range and how strongly it reflects, but it is affected by surface colour and ambient light. For accurate distance, an ultrasonic or dedicated time-of-flight sensor is a better choice.