Interrupt fires multiple times I have a Photo Interrupter which I am using to count the RPM of a motor.

I am incrementing an integer to show the amount of interrupts there have been.

The issue is that there can be several interrupts fired for each time the "beam" of the Photo Interrupter is broken.

I have a 5mm length of plastic which breaks the "beam". If I make the plastic shorter than that, the Photo Interrupter doesn't pick up the break.

I am using the following, very basic code:  

int pin = 13; 
volatile  int tcnt = 0; 
volatile int state = LOW; 

void setup() { 
  Serial.begin(9600);
  pinMode(pin, OUTPUT); 
  attachInterrupt(0, blink, CHANGE); 
} 

void loop() { 
  Serial.println(tcnt); 
  digitalWrite(pin, state); 
} 

void blink() { 
  tcnt = tcnt + 1; 
  state = !state; 
}
Two other possible problems, apart from what Jake C has adressed in his answer are:

1. Hardware bounce

If your signal from the sensor isn't a clean transition from high to low but rather "bouncy" (see image below). The microcontroller will register multiple transitions in the meantime.



Solution to this would be to add a small capacitor (wil slow it down though) or add debouncing code... Best would be to get a "clean" sensor.

2. Floating contact


  When I even move my finger close to the wire running to PIN2 the interrupt fires.


That kinda seems like it's a floating contact, where it's not really connected to anything.

This is usually solved by using a pullup/pulldown resistor. Or maybe there is something wrong with the connection.



Also:


  Maybe due to the type of sensor?


You should note the part number of the sensor or link a datasheet, so we can see if it's typical behaviour for this type of sensor.