How to connect the devices?

The power supply of the?E6B2-C OMRON encoder is between 5V to 24V, we used the 7V from the M-Duino to power supply the encoder. The two resistors are connected, as a Pull-up configuration, between the two phases to the 7V of the M-Duino. And the two phases are connected to I0.4 (phase B) and I0.5 (phase A). And?all the GND are connected to the M-Duino GND.

Once all is connected we can proceed to programming the M-Duino. Remember that the encoder convert revolutions to pulses.

How to read these pulses? How to know the rotate direction?

On this example we use an external interrupt to know when the encoder is rotating. The phase A is connected to the interrupt 0 (I0.5). The interrupt will be configured to active when it appears a rising edge on I0.5. Every rising edge on the phase A will indicate us that the encoder is rotating (one pulse). But to know rotate direction we have to read if value of the phase B is low or high. If the rotate direction is forward (CW; phase B = LOW) and if the rotate direction is going backward (CCW; phase B = HIGH). Next it is showed a signal to get a better understanding of that.

Counting pulses through an external interrupt is the best option, because you get sure that you do not lose any pulse while the microcontroller is executing other code.

With every interrupt we add pulses (pulses++) if the encoder is rotating forward and we subtract pulses if the encoder is rotating backward. Once we have the pulses counting on the right way we just need to know the time to be able to convert these pulses to a speed value. To know the time while we are?getting ?pulses we will use two variables, one for initial time (time0) and other to final time (timeF). Using the function micros() (also you can use millis() function) we will set the two values to implement our equation.

The?E6B2-C OMRON encoder that is used on this example has a resolution of 500 pulses for revolution. So, our equation will be:

Speed = (pulses/500) ? / ? ( (timeF-timeO)/60000000 ) ? rev/min

In this example we display the speed of the encoder on serial monitor. To display it you have to introduce a ?p? of print through?the serial monitor.

IMPORTANT: remember, the interrupt has to be as fast as possible, if not you can get some Aliasing!

To add more information of the functions that are used on this code take a look on the Arduino Reference:?millis(),?micros(),?attachInterrupt(),?detachInterrupt()