...

Optical Interrupters


Here are some optical interrupters that were harvested from various pieces of junked electronics (printers, copiers and scanners are a good source, since they have quite a few moving parts). The body forms a "C" or an "U" shape, and the passing of a solid object between the arms triggers the interrupt. These interrupters are a good alternative to mechanical switches, more reliable because of the lack of moving parts, and in that they provide a nice crisp bounce-free switching action.

This is what the internal schematics usually look like. The housing should contain an IR (infra-red) emitter-detector pair, with 3 leads (we're calling these leads "E" for the connection to the Emitter +, "S" for the detector collector, and "G" for ground).

The emiiter is an IR LED, and we should put in a current limiting resistor. We're not sure what the specs of the LED are, but anything from 180 to 620 ohms should probably work. We picked a value of 470 ohms to be safe, for the 5V Vcc we are using, and we are getting good results with that. The 10K ohm resistor allows this to be used in a pullup switch configuration. This scehmatic shows how to add the resistors to the interrupter, and connect to a microcontroller pin for input.

Identifying the "E", "S" and "G" pins can be usually done by inspecting the traces on the PCB. Ground should be a trace connecting one leg from the emitter and another from the detector. In the image above, it is the trace that runs up from the bottom left, through the right leg in the middle, and on to the center pin of the connector at the top of the image. The emitter pin "E" is usually (not always, though) the one connected to the component farthest away from the PCB, and the remaining connection should then be "S".

Some interrupters come with built-in current limiting resistors already provided (see the 430 ohm SMD LED in the picture above).

We can use the folowing Arduino sketch, together with the schematic above, to see how to use these interrupters. "S" is connected to pin 4. The code prints "0" normally, and "1" when the beam is broken.

const int signalPin = 4;

void setup() {
  Serial.begin(9600);
  Serial.println("started...");
}

void loop() {
  Serial.println(digitalRead(signalPin));
  delay(500);
}
Here are the pinout values for some of the interrupters we had:
Component markingSignalGroundEmitterValue on interruptNote
K949whiteblackgray1 
ALEPHblueblackred1 
73whitecentergray1 
TP1217red (center)yellowbrown1 
OM1317red/whiteblackred0has a SMD resistor (marked "431", so value is 430ohm)
Omron 1230 KPC45 1 
2112A 1 
S58 1 

Interface code:

Here's some sample arduino code showing how an optical switch can be used to trigger a hardware interrupt. No need for hardware or software debouncing, in this case.
void setup() {
  Serial.begin(9600);
  // encoder pin on interrupt 0 (pin 2)
  // FALLING to trigger on transition from dark to light
  // RISING  to trigger on transition from light to dark
  // CHANGE to trigger on either transition
  attachInterrupt(0, handleInterrupt, RISING); 
  Serial.println("started...");
}

void loop() {
}

void handleInterrupt() {
  Serial.println("beam broken...");
}

And to use analog read...

int sensorPin = 0; //analog pin 0
void setup(){
  Serial.begin(9600);
}
void loop(){
  int val = analogRead(sensorPin);
  Serial.println(val);
  //just to slow down the output - remove if trying to catch an object passing by
  delay(100);
}