Photoelectric Sensor Problem

Hi All,
I posted here previously regarding the problem I'm having with my project. I'm using a Arduino MEGA 2560 and a OPB704 photoelectric sensor to detect RPM of a fan blade. I have attached my code below, sometime it picks up some speed, but its random figures, after a while it then detects a different speeds, I'm wondering if my coding is correct? or could it be a faulty sensor as most the time it stays on 0.

I have attached a 220Ohm resistor and also a 100k resistor as per the guidance of the specifications.

Your help would be appreciated.


volatile float time = 0;
volatile float time_last = 0;

void setup () 
  Serial.begin (19200);
  attachInterrupt(0, rpm_interrupt, RISING);  //Pin 3 Set As An Interrupt ( if signal increase )

//Loop To Calculate RPM and Update The Serial Monitor Display
void loop () {
  int rpm = 0;
      delay(250);                 //Slow Down Display Updates
      Serial.println (" ");
      Serial.println (" ");
      Serial.print ("rpm:");
      Serial.println (rpm);
        if(time > 0) {
          rpm = 60*(10000060/(time));

//Capture The OPB704 Beam =>Interrupt
void rpm_interrupt() {
  time = (micros() - time_last);
  time_last = micros();


In the manufacturer's datasheet for the OPB704, they do not quote an operating speed.

My guess is that this reflective optical switch is not fast enough for your application.

If it was fast then the manufacture would tell you!

In the typical applications on the first page, they only mention applications where the presence or absence of slow or stationary items is detected.

I think you need to use some other sensor, whose response time is quoted.

If you really want to detect passing fan blades, reflective sensors are not appropriate for several reasons:

  • the blade surface does not align with the optical axis
  • the distance of the object varies
  • the blade may not be reflective enough

IMO you should use a fork light barrier instead, where the blades break the beam.