Connecting an Incremental Encoder to a Microcontroller.

An incremental encoder looks like a potentiometer, but unlike a potentiometer it has no end positions: it can rotate in both directions an unlimited number of turns. It should also be noted that an incremental encoder doesn't rotate smoothly like a potentiometer, but in steps. You can see one on car radios, oscilloscopes, stereo systems, washing machines and other equipment where some parameter is adjusted over a wide range. Of course, parameters can also be changed with buttons; for example, to turn the music up by 20 steps with a button you'd have to press it 20 times, while with an encoder you just turn it by a certain angle, depending on the processing algorithm.
An incremental encoder consists of two contacts whose closing order depends on the direction of rotation.

Essentially, an incremental encoder converts shaft rotation into electrical pulses that carry information about the direction of rotation.
Let's build the test circuit shown in the picture above and connect an oscilloscope to pins A and B; the pull-up resistors are 4.7K.
Let's turn the encoder clockwise.

Now counterclockwise.

The oscillograms show that the order in which the contacts close changes depending on the direction of rotation. But the edges don't always come out this clean.

Since the contacts are mechanical, they are subject to bounce, i.e. when they close, the elasticity of the materials causes multiple uncontrolled closings and openings, which can be seen in the oscillogram above.
Bounce can be dealt with in two ways; the first is to add capacitors and resistors, as shown in the picture below.

Since bounce is a short-lived phenomenon, it is easily suppressed by a capacitor.

The oscillogram shows that after installing the capacitors the edges became less steep and the bounce disappeared.
The second method is in software, and here everything depends on how the encoder pins are polled. If the encoder state is tracked using external interrupts, then after the interrupt fires you need to wait 20 - 30 milliseconds, during which the MCU won't respond to changes of the pin state, i.e. won't feel the bounce. If polling of the encoder pins is implemented with a timer, the interval between polls must be longer than the bounce duration, the same 20 - 30 milliseconds.
Let's look at methods of processing the data coming from the encoder.
The first method is to connect one of the encoder pins to an external interrupt input and configure it to interrupt on the falling edge. In the interrupt we check the state of the other pin: if it's zero, the rotation is in one direction, otherwise in the other. Below is code implementing this method for AVR.
#define F_CPU 8000000UL
#include <avr/io.h>
#include <avr/interrupt.h>
ISR(INT2_vect)
{
if (PINB & 0X02)
{
PORTB |= (1<<0);
}
else
{
PORTB &= ~(1<<0);
}
//debounce
_delay_ms(20);
//clear the interrupt flag caused by bounce
GIFR = (1<<INTF2);
}
int main(void)
{
//configure the external interrupt pin as an input
DDRB &= ~(1<<2);
//enable the pull-up
PORTB |= (1<<2);
//configure the input for the second encoder pin
DDRB &= ~(1<<1);
//configure the LED pin as an output
DDRB |= (1<<0);
//set the interrupt edge - falling
MCUCR &= ~(1<<ISC2);
//enable external interrupt int2
GICR |= (1<<INT2);
sei();
while(1)
{
}
}
When the encoder is turned in one direction, the LED lights up; when turned in the other, it goes out.
The second method consists in comparing the current state with the previous one. Let's express the logic levels of the pulse sequence as zeros and ones.

Then we get a finite number of encoder states. The first digit is the logic level of the first encoder pin, the second the logic level of the second pin.
00 = 0
10 = 2
11 = 3
01 = 1
Suppose the last state of the encoder was three; if the next state is one, it's rotating in one direction, if two, in the other. So you can detect the transition from one state to another and determine the direction of rotation, but the simplest implementation is for the transition from 11 to 01 and 10. Below is code implementing the described algorithm for AVR,
#define F_CPU 8000000UL
#include <avr/io.h>
#include <avr/interrupt.h>
uint8_t last_state = 0;
ISR(TIMER0_COMP_vect)
{
//both encoder pins are connected to pins 2 and 3 of port B
//read their state
uint8_t current_state = (PINB & 0x06)>>1;
//take the transition into account only if the previous state is 11
//and if it differs from the new one
if ((last_state == 3) && (last_state != current_state))
{
//if the new state is 01 - turn on the LED
if(current_state == 1)
{
PORTB |= 0x01;
}
//if the new state is 10 - turn off the LED
if(current_state == 2)
{
PORTB &= ~0x01;
}
}
//on exit from the interrupt, the current state becomes the previous one
last_state = current_state;
}
int main(void)
{
//two inputs for connecting the encoder
DDRB &= ~0x06;
//pull the inputs up to the supply
PORTB |= 0x06;
//output for connecting the LED
DDRB |= 0x01;
//configure the timer in clear-on-compare-match mode
TCCR0=(1<<WGM01);
//enable the counter compare match interrupt
TIMSK |= (1<<OCIE0);
//set the compare value so that the polling period is 20ms
OCR0 = 150;
//set the prescaler to 1024
TCCR0|=(1<<CS02 | 1<<CS00);
//enable interrupts globally
sei();
while(1)
{
}
}
That's all.
I bought the encoder here.