Wednesday, October 12, 2011

INCREMENTAL ROTARY ENCODER

An incremental rotary encoder, as used in many applications, can be considered the digital equivalent of a potentiometer. Instead of a variable resistance, an encoder provides a digital on/off output. There are two basic types, contacting (mechanical contacts) or non-contacting (optical or magnetic). The most common low cost encoders are mechanical and have 12 or 24 pulses per revolution, with or without detents. Some models also have built-in switches. Also, an encoder has no mechanical stops, which allows continuous rotation in either direction. Optical and magnetic encoders are a better choice when rotational speeds are greater than about 100 RPM.

The most basic encoder typically has two outputs called “A” and “B” and since the outputs are 90 degrees out of phase they are referred to as a quadrature output encoder. Having two outputs gives the ability to determine the direction of rotation either clockwise or counter-clockwise.

The outputs are usually connected to a microcontroller which is programmed to determine the pulse count and direction of rotation. Mechanical encoders generally require some form of contact conditioning using either an RC filter or software delay or polling to eliminate contact bounce. Optical and magnetic encoders do not have a contact bounce problem, however a schmitt trigger circuit maybe needed to square-up the output pulses.

This is what the quadrature output looks like:

FOR CLOCKWISE ROTATION

PHASE            A         B
     1                 0         0
     2                 0         1
     3                 1         1
     4                 1         0

FOR COUNTER-CLOCKWISE ROTATION

PHASE            A         B
     1                 1         0
     2                 1         1
     3                 0         1
     4                 0         0

I created a simple PWM control with an LCD display that uses an encoder to adjust the PWM duty cycle from 0 to 100%. The LCD display was helpful to ensure that the encoder pulses were being counted correctly. Since I was using a 24 pulse per turn encoder, I was able to confirm that I was getting a count of 24 for every revolution of the encoder shaft – both in a clockwise (up count) and counter-clockwise (down count) direction.

The encoder I used was a Model EN16 made by BI Technologies.
Bourns also makes some unique encoders that have LED lighted shafts.

The microcontroller was a FREESCALE MC9S08QG8. I used the keyboard interrupt (PORT PTA1/KBIP2) connected to the “B” output. The “A” output was connected to PORT PTA2. The program compares the pre-interrupt encoder value (located in main function) to the encoder value found during the interrupt routine.  There are four switch/case statements to determine if the rotation is clockwise or counter-clockwise and also increments or decrements the PWM value by 1. The program also limits the PWM range from 0 to 100%.

The output driver consists of a 4N37 optoisolator which provides complete isolation between the microcontroller circuit and the output load. This also allows the load to have its own power source separate from the microcontroller power supply. The 4N37 output provides gate drive to a pair of 2N7000 MOSFET'S in parallel. The MOSFET'S supply the base drive to a 2N3055 power transistor. The 2N3055 controls the ground side of the load.


ENCODER OUTPUT AND FILTER DIAGRAM

CLICK ON FOR LARGER IMAGE
















PWM OUTPUT DRIVER


CLICK ON FOR LARGER IMAGE





















Below is a picture of the PWM control prototype board supplying power to a small incandescent lamp at a PWM output of 15%. There is a tricolor LED that provides another visual indication of the PWM output, changing from red at small PWM values, to yellow at intermediate PWM values, to green at high PWM values.





This is a close-up of the encoder. Also, you can see the tricolor LED and the 4N37 optoisolator just to the right of the encoder.


CLICK ON FOR LARGER IMAGE


No comments:

Post a Comment