# What is an Encoder?

## What is an encoder?

An encoder can be defined as a sensor in which a light sensor is used in conjunction with a slotted disc in order to determine how far a rotational mechanism has turned. The slots in the disc either allow light to pass or block it, allowing the light sensor to send data to the microcontroller, which can add these pulses and determine mow many 'slots' the encoder has passed.

An encoder works by shining light through a slotted wheel

### The VEX encoder

The internals of the VEX standard shaft encoder include the slotted disc and the light sensor. The disc has 90 slots, meaning that the encoder will make one 'tick' every 4 degrees.

The internals of a VEX encoder

### Problems

The regular VEX encoder has a problem - due to its nature, it is impossible to tell which direction the slotted wheel is turning as it can only record a light reading of a 'slot' or a 'non-slot'. If the wheel was to turn the other way, the sensor will keep adding to the rotational value, giving the programmer very confusing values to work with.

## The quadrature encoder

Luckily, VEX has recently released an upgrade to the standard encoder, called the Quadrature Shaft Encoder or Quad encoder for short. The quad encoder works essentially the same as the standard encoder, with one important difference - the quad encoder has two light sensors, offset in such a way that the encoder not only can determine the direction it is turning, but also can improve its accuracy four times, meaning it will count up one degree per tick.

The new Quad Encoder has 2 wires for its 2 separate internal light sensors.

### How it works

Since the standard encoder has 90 slots in a disc, we can determine, then, that the disc has a 2° slot for every 2° of non-slot. If the wheel spins at a constant rate, the output values will look like this:

Degrees Light Value Cumulative ticks
0 1 0
1 1 0
2 0 0
3 0 0
4 1 1
5 1 1
6 0 1
7 0 1
8 1 2
... ... ...

If we use a Quadrature Encoder, where we have another light sensor offset by one degree, our values will look something like this:

Degrees Value 1 Value 2 Cumulative ticks
0 1 0 0
1 1 1 1
2 0 1 2
3 0 0 3
4 1 0 4
5 1 1 5
6 0 1 6
7 0 0 7
8 1 0 8
... ... ... ...

This way, the microcontroller can distinguish between each individual degree, as well as being able to interpret the pattern to find the direction of rotation. This is much more useful to the programmer and it becomes easier to write powerful programs using the encoder.

The robot can determine the direction of turn by interpreting the signal as a pattern. Consider the pattern of a regular encoder:

 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1

Now, if the wheel is rotating backwards:

 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1

The pattern is the same, because it is read the same backwards as forwards.

When using the quadrature encoder, however, the pattern has much more information as the robot is now getting two bits of data.

Turning forwards:

 10 11 01 00 10 11 01 00 10 11 01 00 10 11 01 00

Turning backwards:

 10 00 01 11 10 00 01 11 10 00 01 11 10 00 01 11

See how the patterns are different? The robot can distinguish this and thus tell how whether the encoder is turning backwards or forwards.