Arduino Uno Photo Interrupter Sensor
This project demonstrates how to use an Arduino with a photo interrupter sensor and an LED. The photo interrupter sensor detects changes in light intensity and triggers the LED accordingly. When the sensor is blocked, indicating an interruption in the light path, the LED turns on. Conversely, when the sensor is unblocked, the LED turns off.
How It Works
A photo interrupter — also called a slotted optical switch or optocoupler interrupter — places an infrared LED on one side of a narrow gap and a phototransistor directly opposite. When the slot is clear, light reaches the phototransistor and it conducts. When something opaque enters the slot, the light path is cut and conduction stops.
Unlike a reflective sensor, the geometry is fixed and the detection point is mechanically precise — typically within a fraction of a millimetre at the slot edge. That repeatability is the reason photo interrupters dominate position sensing in printers, scanners, disk drives and 3D printers.
Because the LED and detector face each other across a small, shaded gap, ambient light rarely interferes. There is also nothing to wear out, unlike a mechanical limit switch whose contacts degrade over tens of thousands of operations.
The phototransistor output is usually open-collector, so it needs a pull-up resistor. Many carrier boards such as the KY-010 already include one along with an indicator LED.
Components Needed
- Arduino Uno
- Photo Interrupter Sensor module
- Arduino Uno
- USB cable for programming and power
- Arduino Uno
Wiring to the Arduino Uno
Connect S to D2, the supply pin to the 5 V rail and GND to ground. If you are using a bare slotted sensor rather than a carrier board, you must add both the LED's series resistor (around 220 Ω) and a 10 kΩ pull-up on the phototransistor collector.
Output polarity varies between carriers — some read HIGH when blocked, others LOW. Rather than trusting a datasheet, run the sketch below once with the slot clear and once blocked, and note which way round yours behaves.
Mount the sensor so the interrupting vane passes cleanly through the slot without touching either side. A vane that rubs will eventually shift the alignment and cause missed counts.
| Module pin | Arduino Uno pin | Function |
|---|---|---|
| S / OUT | D2 | HIGH when the slot is blocked (carrier dependent) |
| Middle / + | 5V | Supply |
| − / GND | GND | Common ground |
Build and Upload
Open the Arduino IDE and create a new sketch.
Copy and paste the provided Arduino code into the sketch.
Upload the code to the Arduino Uno.
Open the serial monitor with a baud rate of 9600.
Observe the messages on the Serial Monitor indicating whether an object is detected or not as you interrupt the sensor's beam.
Example Code
Counting slot interruptions, the basis of an encoder or tachometer. Upload it with the board set to Arduino Uno and open the Serial Monitor at 9600 baud.
const int SENSOR_PIN = 2;
const int SLOTS_PER_REV = 20; // matches a typical encoder wheel
volatile unsigned long pulses = 0;
unsigned long lastReport = 0;
void countPulse() { pulses++; } // keep ISRs short
void setup() {
Serial.begin(9600);
pinMode(SENSOR_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(SENSOR_PIN), countPulse, FALLING);
}
void loop() {
if (millis() - lastReport >= 1000) {
noInterrupts();
unsigned long count = pulses;
pulses = 0;
interrupts();
float rpm = (count * 60.0) / SLOTS_PER_REV;
Serial.print("pulses/s: ");
Serial.print(count);
Serial.print(" speed: ");
Serial.print(rpm, 1);
Serial.println(" RPM");
lastReport = millis();
}
}
Applications
A photo interrupter sensor turns up in a wide range of projects. These are the uses where it is the right choice rather than a compromise:
- Rotary encoders measuring shaft speed and direction
- End stops and homing switches on 3D printers and CNC machines
- Paper presence and jam detection in printers and feeders
- Counting items dropping through a chute
- Tamper detection, where removing a cover clears the slot
Working with the Arduino Uno
The Arduino Uno is built around the ATmega328P and runs on 5 V logic with 2 KB of SRAM and 32 KB of program flash. These details change how this circuit is wired and what the sketch can do, so they are worth stating plainly before you build.
The Uno runs at 5 V, so most hobby sensor modules connect directly with no level shifting.
With only 2 KB of SRAM, avoid large buffers and prefer the F() macro for constant strings.
The single hardware UART is shared with the USB connection, so heavy Serial printing competes with uploads.
| Arduino Uno characteristic | Value | Why it matters here |
|---|---|---|
| Logic voltage | 5 V | Matches most hobby modules directly |
| ADC resolution | 10-bit (0–1023) | Sets how finely an analog reading can be resolved |
| Analog inputs | A0–A5 (six channels) | Determines how many analog sensors can share the board |
| PWM outputs | D3, D5, D6, D9, D10 and D11 | Needed for brightness, speed and tone control |
| I²C pins | A4 (SDA) and A5 (SCL) | Fixed by hardware — wiring copied from another board may not match |
| Interrupt pins | D2 and D3 only | Required for counting fast or asynchronous events |
| Serial | a single hardware UART shared with USB | Monitor runs at 9600 baud by default |
Troubleshooting
Most problems with this module fall into a handful of categories. Work through these before suspecting the part itself:
- The output never changes — the vane is missing the slot, or the LED side has no current-limiting resistor and has failed.
- Counts are double what they should be — the sketch is triggering on both edges. Use
FALLINGrather thanCHANGE. - Counts are missed at speed — polling cannot keep up. Use the interrupt-driven approach shown here.
- The reading is inverted compared with the tutorial — carrier boards differ; swap the comparison and continue.
- Code written for an ESP board gives odd analog values — the Arduino Uno uses a 10-bit ADC returning 0–1023; rescale any constant taken from a 12-bit example.
- An I²C sensor is not found after copying wiring from another Arduino — on the Arduino Uno I²C is on A4 (SDA) and A5 (SCL).
Taking It Further on the Arduino Uno
Once the basic reading works, where you go next depends very much on which board you are using. These are the directions that suit the Arduino Uno specifically:
The Uno’s shield ecosystem is its real advantage. Once the circuit works on a breadboard, a prototyping shield turns it into something permanent that still stacks with a data-logging or Ethernet shield without rewiring.
Logging to an SD card via a shield is the natural next step, since the Uno has no onboard storage and no network. Timestamp each reading with a DS3231 real-time clock so the log survives power cuts with correct times.
Because SRAM is limited to 2 KB, keep logged strings short and write them out immediately rather than buffering. Building a long String in memory is the most common cause of an Uno sketch that runs for hours and then freezes.
Notes and Practical Limits
On the Arduino Uno, interrupt-capable pins are D2 and D3 only. Attaching an encoder to a non-interrupt pin is the most common reason counts go missing once a motor spins up.
Keep the ISR to a single increment, as above. Printing to Serial inside an interrupt handler will corrupt timing and can hang the board.