circuitcellar.com
Magazine Support   Digital Library   Products & Services   Suppliers Directory 
 
 





 

Issue 152 March 2003
Using Rotary Encoders as Input Devices


QUADRATURE ENCODERS

Signals that are 90° out of phase with each other are said to be in quadrature—this is where the name "quadrature encoders" comes from. This type of encoder’s two outputs—labeled channels A and B—are illustrated in Figure 1. The x-axis can be considered the passage of time during which the encoder is being turned in a CW direction. Note that the A switch always closes first with respect to B. During CCW rotation, the B channel switch always closes first. Normally, these encoders would be connected with their common terminal at ground potential and both the A and B channel outputs pulled up to VCC through resistors.

(Click here to enlarge)

Figure 1—These are the conditions of the two switches in a quadrature rotary encoder. The dashed lines represent detents, but the detent setup will vary among different types of devices.

You can monitor this type of encoder by watching for the falling edge of the A signal, and then quickly checking to see if the B signal is high or low. The state of the B signal then indicates the direction in which the encoder is turning and dictates whether you should increment or decrement the parameter that you’re adjusting. The simplest way to accomplish this is to connect the A signal to a free interrupt pin on the microcontroller and connect the B signal to another available input port pin. Configure the microcontroller to accept falling-edge interrupts, and then enable interrupts.

The interrupt service routine (ISR) for that particular interrupt would merely read the state of the port pin connected to signal B, and then increment or decrement a variable accordingly. Depending on how fast your microcontroller can respond to an interrupt, you might have to add a bit of a delay into the body of the ISR to eliminate the effects of contact bounce, which occurs in the rotary encoder’s switches.

The aforementioned solution works best in situations where it’s important that any movement of the encoder is recognized, even when the microcontroller program is busy doing something else (i.e., not actively monitoring the port lines connected to the rotary encoder). If you needed to monitor numerous rotary encoders, this scheme might not be ideal because most microcontrollers have only one or two interrupt inputs, limiting you to that number of encoders. Later, I’ll describe a simple solution to this problem.

Now, I’ll cover detenting. Quadrature encoders can be obtained with no detents. If you simply spin them, they can come to rest anywhere. A rotary encoder is detented if it can only come to rest at the number of positions per turn it was designed to encode. The detent is often associated with a click at each valid position. For the simple software solution that I described earlier, it’s important that you stick with a detented encoder. Non-detented encoders can come to rest in a position where the switches are just at the verge of opening or closing. This "teasing" would cause problems for the software.