user11025: 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; 
}


user2944: I would recommend adding a comparator with hysteresis so as to get a clean digital pulse, pictured below:

simulate this circuit – Schematic created using CircuitLab
This has the correct values.
I recommend the comparator with hysteresis as the phototransistors are prone to cause issues with interrupts even when used with pul-up/pull-down resistors, thus is as they very the voltage more than give a clean 0/1 digitally.
Here is your code, modified with the addition of RISING and of a 10mS delay.
int pin = 13; 
volatile  int tcnt = 0; 
volatile int state = LOW; 

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

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

void blink() { 
  tcnt = tcnt + 1; 
  state = !state; 
}

This method produces very reliable results. It is also accurate with objects moving rapidly because of the interrupt.

I would also say that the problem you have when you use a piece of plastic < 5mm is that the length of the slit in the sensor is 6.0±0.1(seen below). If the plastic is less you probably find that the phototransistor still has enough IR falling on it to allow voltage to flow, but enough to trigger and interrupt. With the comparator you could probably use a smaller piece of plastic and find the voltage swing with a DMM and modify the voltage divider (R1 and R2) to suite the needs.


user8033: 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.

11025: <p>I have a Photo Interrupter which I am using to count the RPM of a motor.</p>

<p>I am incrementing an integer to show the amount of interrupts there have been.</p>

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

<p>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.</p>

<p>I am using the following, very basic code:  </p>

<pre><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; 
}
</code></pre>
 2944: <p>I would recommend adding a comparator with hysteresis so as to get a clean digital pulse, pictured below:</p>



<p><img src="https://i.stack.imgur.com/EqgJp.png" alt="schematic"></p>



<p><sup><a href="/plugins/schematics?image=http%3a%2f%2fi.stack.imgur.com%2fEqgJp.png">simulate this circuit</a> &ndash; Schematic created using <a href="https://www.circuitlab.com/" rel="nofollow">CircuitLab</a></sup></p>

<p>This has the correct values.</p>

<p>I recommend the comparator with hysteresis as the phototransistors are prone to cause issues with interrupts even when used with pul-up/pull-down resistors, thus is as they very the voltage more than give a clean <code>0/1</code> digitally.</p>

<p>Here is your code, modified with the addition of <code>RISING</code> and of a 10mS delay.</p>

<pre><code>int pin = 13; 
volatile  int tcnt = 0; 
volatile int state = LOW; 

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

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

void blink() { 
  tcnt = tcnt + 1; 
  state = !state; 
}
</code></pre>

<p>This method produces very reliable results. It is also accurate with objects moving rapidly because of the interrupt.</p>

<hr>

<p>I would also say that the problem you have when you use a piece of plastic &lt; 5mm is that the length of the slit in the sensor is <em>6.0±0.1</em>(seen below). If the plastic is less you probably find that the phototransistor still has enough IR falling on it to allow voltage to flow, but enough to trigger and interrupt. With the comparator you could probably use a smaller piece of plastic and find the voltage swing with a DMM and modify the voltage divider (<strong>R1</strong> and <strong>R2</strong>) to suite the needs.</p>

<p><img src="https://i.stack.imgur.com/4iHaF.jpg" alt="enter image description here"></p>
 8033: <p>Two other possible problems, apart from what Jake C has adressed in his answer are:</p>

<p><strong>1. Hardware bounce</strong></p>

<p>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.</p>

<p><img src="https://i.stack.imgur.com/6f1w9.gif" alt="Hardware bounce"></p>

<p>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.</p>

<p><strong>2. Floating contact</strong></p>

<blockquote>
  <p>When I even move my finger close to the wire running to PIN2 the interrupt fires.</p>
</blockquote>

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

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

<hr>

<p>Also:</p>

<blockquote>
  <p>Maybe due to the type of sensor?</p>
</blockquote>

<p>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.</p>
