What is an Encoder?
What is an Encoder?
An encoder is a device that is used to measure the rotation of a motor. Encoders work by receiving data from internal light sensors that indicate changes in the motor's position, which the controller (the Arduino) then interprets by counting each time the motor changes phases (i.e. changes from one range of position to another, thereby changing the signal coming from the light sensors in the encoder). Each time the phase goes to a more "forward" position, the count increases. When it moves to a more "backwards" position, the count decreases. Different encoders read different values for a full rotation, but LEGO's encoder reads about 720 to be a full rotation forwards, and -720 to be a full rotation backwards.
The Quadrature Encoder
The Lego Encoder is a Quadrature Encoder, meaning that it uses two light sensors that are offset from each other so that the second's pulse signal is always 90 degrees behind the first's (Which is why the encoder is termed "quadrature"). Between the sensors and their light source is a slotted or spiked wheel that quickly shifts between blocking light from getting to the sensors and letting light through to them as the motor turns (sort of like an old movie projector). Every time the input value from one of the light sensors changes, the "phase" of the encoder changes.
How it works
There are four "phases" in which the combination of light sensors can be in. Each sensor stays "on" or "off" for two phases in a row, but because of the 90 degree offset, one of the sensors' value changes every phase.
|Degrees||Value 1||Value 2||Count value|
The order in which the phases are reported (in this case corresponding to counter clockwise) determines the direction in which the count moves. Each time a phase passes by, the count increases or decreases as appropriate. Because the Lego motor has 720 counts to a rotation, each phase only lasts .5 degrees of motor rotation.
Motors going too fast can cause the encoder to skip one or more phases, which could result in either the count not going up enough, or even taking a step in the wrong direction (if the next phase read is actually right behind the previous phase). Fortunately, the LEGO motor at maximum speed does not even come close to the point where this might happen.
Encoders have a maximum value, in the case of the LEGO encoder, 32,760. If the Encoder exceeds this value, it resets the count back to zero. As might be imagined, this would seriously throw off any code that depends on the encoder reading a value beyond its limit. It is always a good idea to zero your encoders before you begin an action, as the motor may have turned involuntarily in between commands or before a program starts, which then throws off all of the encoder readings after that point.