Built-in project ยท Arduino Uno R3

Photo interrupter on pin 3 lights the LED on 13, on an Arduino Uno

A photo interrupter on pin 3: put a card in the slot and its output changes, and the LED on pin 13 follows it. The whole build, an Arduino Uno and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $31. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • How a slot sensor detects objects
  • Reading a digital input with digitalRead
  • Mirroring an input on an LED
  • Printing only when a state changes
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: Arduino Uno R3
  • Parts: 3

How it works

A photo interrupter is a small slot with an infrared beam across it. When something sits in the slot, the beam is blocked and the output on SIG changes. SIG goes to pin 3, with power from 5V and GND.

The sketch reads pin 3 every 10 ms. HIGH means the beam is blocked, and the LED on pin 13 lights. It also prints "beam blocked" or "beam clear" whenever the state changes. Click the module in the simulator to put the card in the slot.

Parts list

3 parts, plus the jumper wires. They retail for about $31; on Mokxi they're free.

Kits that can build it

This starter kit has every part in the circuit. Each kit's page lists what else it can build.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Photo interrupter, slot pin GNDArduino Uno R3 pin GND
Photo interrupter, slot pin SIGArduino Uno R3 pin 3
Photo interrupter, slot pin VCCArduino Uno R3 pin 5V

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino ยท Arduino Uno R3
// Photo interrupter on pin 3 lights the LED on 13.
//
//   interrupter SIG -> pin 3   VCC -> 5V   GND -> GND
//
// An infrared beam crosses the slot; anything in the slot blocks it and the output changes.
//
// Click the module to put the card in the slot.

const int SENSOR = 3;
const int LED_PIN = 13;
int last = -1;

void setup() {
  pinMode(SENSOR, INPUT);
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: photo interrupter on pin 3, the LED on pin 13");
}

void loop() {
  int now = digitalRead(SENSOR) == HIGH ? 1 : 0;
  digitalWrite(LED_PIN, now ? HIGH : LOW);
  if (now != last) {
    last = now;
    Serial.println(now ? "beam blocked" : "beam clear");
  }
  delay(10);
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

Sets SENSOR (pin 3) as an input and pin 13 as an output for the LED. Then it starts serial at 9600 baud and prints a hello line.

void setup() {
  pinMode(SENSOR, INPUT);
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: photo interrupter on pin 3, the LED on pin 13");
}

loop sketch.ino

Reads pin 3: HIGH sets now to 1. It writes now to the LED. If now is different from last, it saves it and prints "beam blocked" or "beam clear". A 10 ms delay ends each pass.

void loop() {
  int now = digitalRead(SENSOR) == HIGH ? 1 : 0;
  digitalWrite(LED_PIN, now ? HIGH : LOW);
  if (now != last) {
    last = now;
    Serial.println(now ? "beam blocked" : "beam clear");
  }
  delay(10);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The LED does the opposite of what you expect This project
    The sketch treats HIGH as blocked. If your module works the other way, flip the test in loop.
  • Nothing changes This project
    Check SIG goes to pin 3, and the module has 5V and GND.
  • The output never changes, or is always the same. The LED side needs a series resistor, and the phototransistor side needs a pull-up. Photo interrupter
    Put about 220 ohms in series with the LED from 5 V. Pull the output up with about 10k, or use INPUT_PULLUP. Output goes low when the beam is clear and high when blocked, or the reverse on some modules.
  • Pulses are missed or counted twice at higher speeds because the loop is too slow or the edge is noisy. Photo interrupter
    Count with an interrupt on the pin, on one edge only. Make sure the wheel slots are wide enough to pass the beam and the object fully blocks it.
  • Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
    Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected.
  • Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
    Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.