RPM Measurement with Optical Interrupter Switch

I am trying to measure the RPM of a wheel with an H21A1 Optical Interrupter Switch (Opto Coupler).

I have first tested the sensor under an oscilloscope while attached to the wheel. I got nice square waves. Than I tested if I can count the total swiching by moving a credit card up an down. I can count every movement with Mega 2650 board using an interrupt. The sensor reads TRUE when notting is blocks it.

The image above is the opto coupler.

And this one above is the motor with an encoder wheel. It has 20 holes. There is about 0,5 mm clearance between the wheel and the sensor.

When running my code, I am getting "65535" for the RPM measurement. When I slow the wheel, the flashing of the TX LED slows down too but the rpm value does not change at all.Can someone please guide me with what I am doing wrong? I think the problem is with measuring the time interval. Here is my code:

volatile byte revolutions; //this is the revolution counter
unsigned int rpm; //revolutions per minute
unsigned long timeold; //needed for the differantial time

void setup() {
  
Serial.begin(9600);
attachInterrupt(0, rev_counter, RISING);

revolutions=0; 
rpm=0;
timeold=0;

}

void loop() {
  if (revolutions >= 20) { //Calculates rpm after every 20 revs.

    rpm=(revolutions/20)/(((millis() - timeold)/60)/1000); //there are 20 holes on the encoder wheel
    timeold=millis();
    revolutions=0;
    
    Serial.print(rpm,DEC);
    Serial.print('\n');
    } 
}

void rev_counter() {
  
revolutions++;  
}

This is the oscilloscope signal when the motor is running. The opto coupler is connected to a 10K resistor. So it looks like its running at about 120 rpm.

Looks like you have your scope on AC.

Use the DC setting, and look if the sensor signal gets to less than 1volt and more than 4volt.

What is your opto LED current (resistor value).

Ambient light in the sensor, or wrong value pull-up resistor, could mean that one of the digital switch points of the Mega are not reached.

Edit your post.
Change the "quote" at the start and end of your code into "code".
Leo..

Wawa:
Looks like you have your scope on AC.

Use the DC setting, and look if the sensor signal gets to less than 1volt and more than 4volt.

What is your opto LED current (resistor value).

Ambient light in the sensor, or wrong value pull-up resistor, could mean that one of the digital switch points of the Mega are not reached.

Edit your post.
Change the "quote" at the start and end of your code into "code".
Leo..

When chaning to DC mode, I see that the max value is about 4.5V and the min is 0.3V. I also read the same values on the multimeter.

The LED resistance is 1720 ohms. I am using 220 ohm resistor in parallel with the opto LED.

I made a mistake with the code and I have changed it. Now it is reading 65535 rpm. It should be between 150-500 rpm. When I slow the wheel, the blink speed of the TX LED on Mega slows down, which indicates that the sensor is picking up the speed, but the value is still 65535 when the wheel speed is reduced.

After changing the oscilloscope to DC mode, It looks like its raising every 120 ms. It has 20 holes and that makes about 25 rpm.

mrters_34:
The LED resistance is 1720 ohms. I am using 220 ohm resistor in parallel with the opto LED.

That does not make sense.
A 220ohm resistor in series with the opto LED is about 14.5mA LED current.
You might not need that much.
A 470ohm resistor (7.5mA) might be enough, depending if the “low” level is <1volt.

Not a coder, but if you change interrupt to CHANGE, and adjust your code, you can have twice the resolution.
Leo…

Wawa:
That does not make sense.
A 220ohm resistor in series with the opto LED is about 14.5mA LED current.
You might not need that much.
A 470ohm resistor (7.5mA) might be enough, depending if the “low” level is <1volt.

Not a coder, but if you change interrupt to CHANGE, and adjust your code, you can have twice the resolution.
Leo…

Wow thanks a lot! I have changed the resistance to 440 ohms and it worked! I get 100 rpm reading and it changes if I slow down the motor.

So why didnt 220 ohm work?

Your RPM calculation looks messy.

RPM = 60000 / (millis() - timeold).

Yes, I have changed the formula.