Speed using encoders

I want to measure rotation rate of a wheel with an opto-interruptor type encoder. Also I want to avoid using interrupt pins. I know I can use port interrupts but I am just curious if a passive circuit can do the job. Please suggest something. Also even with interrupts, how can I get accurate results?

There are analog tachometer chips that convert frequency to voltage. You could use the Arduino to read the output voltage, but I really don't see why you would do that. If interrupts are out of the question for some reason, and your frequency is low enough, you can probably get away just polling the pin in the loop.

But really, the optimal solution is definitely to use interrupts. Here are some great examples using timer interrupts: Gammon Forum : Electronics : Microprocessors : Timers and counters.

If you have a quadrature encoder and you need the exact location or direction of movement, you can use external interrupts, for example using the PJRC Encoder library.


If the processor has nothing else to do, you can just poll a digital pin and look for light/dark transitions from the encoder.

This example is for a code wheel that is 1/2 black and 1/2 white. It uses direct port access to pin A5 to read the input bit quickly.

   unsigned long period;
   unsigned long ticks=millis();	//time one full revolution

   while( (PINC&(1<<PC5)) ==0);	//seeing white, wait till black

   while( (PINC&(1<<PC5)) !=0);	//seeing black, wait till white

   period = millis()-ticks;

   rpm=600000UL/period;		//convert to rpm*10 (1 rev/second = 60 rpm = 1000 ticks)
   t=rpm%10; 				//one digit after decimal point (avoid loading floating routines)
   rpm=rpm/10;			       //integer value