Photo Interrupter Sensor

The Photo Interrupter Sensor project demonstrates how to use an Arduino Nano to interface with a photo interrupter sensor. This sensor consists of an infrared LED and a photodetector, which are positioned facing each other. When an object obstructs the path between the LED and the photodetector, the sensor detects the interruption and provides a digital output signal.

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 Nano
  • Photo Interrupter Sensor module
  • Arduino Nano
  • USB cable for programming and power
  • Arduino Nano

Wiring to the Arduino Nano

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 pinArduino Nano pinFunction
S / OUTD2HIGH when the slot is blocked (carrier dependent)
Middle / +5VSupply
− / GNDGNDCommon ground

Build and Upload

Open the Arduino IDE and paste the provided code.

Upload the code to the Arduino Nano.

Once the code is uploaded, open the serial monitor.

The serial monitor will display whether the sensor is blocked or unblocked based on the presence or absence of an obstruction in the sensor's path.

Example Code

Counting slot interruptions, the basis of an encoder or tachometer. Upload it with the board set to Arduino Nano and open the Serial Monitor at 9600 baud.

Counting slot interruptions, the basis of an encoder or tachometer
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 Nano

The Arduino Nano 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 Nano shares the Uno’s ATmega328P but adds A6 and A7, which are analog-input only and cannot be used as digital pins.

Its DIP footprint drops straight into a breadboard, which suits permanent sensor builds.

Older clones use the CH340 USB bridge and may need that driver plus the "ATmega328P (Old Bootloader)" processor option.

Arduino Nano characteristicValueWhy it matters here
Logic voltage5 VMatches most hobby modules directly
ADC resolution10-bit (0–1023)Sets how finely an analog reading can be resolved
Analog inputsA0–A7 (eight channels, two more than the Uno)Determines how many analog sensors can share the board
PWM outputsD3, D5, D6, D9, D10 and D11Needed for brightness, speed and tone control
I²C pinsA4 (SDA) and A5 (SCL)Fixed by hardware — wiring copied from another board may not match
Interrupt pinsD2 and D3 onlyRequired for counting fast or asynchronous events
Seriala single hardware UART shared with USBMonitor 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 FALLING rather than CHANGE.
  • 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 Nano 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 Nano I²C is on A4 (SDA) and A5 (SCL).

Taking It Further on the Arduino Nano

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 Nano specifically:

The Nano’s DIP footprint makes it the natural choice once a breadboard prototype becomes a soldered build. Mount it on female headers rather than soldering it down, so the board can be recovered if the project is retired.

With A6 and A7 available in addition to A0–A5, the Nano can read two more analog sensors than an Uno. Remember that those two pins are analog-input only — they cannot be used with digitalWrite or as digital inputs.

For battery-powered builds, the Nano’s regulator and USB bridge dominate idle current. Powering the 5V pin directly from a regulated supply and removing the power LED substantially extends runtime.

Notes and Practical Limits

On the Arduino Nano, 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.