Tuesday, March 19, 2013

Using Digital Rotary Encoder Switch

Everything is going digital these days, and so they say. But there are some things which are better left done the old fashioned analog way. Like, for example, the volume control of your stereo sound system. Nobody puts a push button control for this most used feature because, well, it is not appealing to humans.

A volume control is one example. There are a lot of other controls, depending upon the equipment, that will be simply easier to use with twisting of some knob, rather than with repeated pushing of a button.

Photo 1. Shown is a section of the digital control panel of an HP8647A signal generator. While settings can be directly entered via a digital keypad, parameters that often require incremental changes (sweeping) are still easier entered with the use of rotary controls. The two rotary controls shown in this photo uses quadrature encoder switch. A quadrature encoder is a digital switch that looks and feels like an analog potentiometer.


The quadrature encoder switch, or simply encoder switch, is a pure digital device masquerading as an analog potentiometer. It looks and feels like a potentiometer from the outside, but its all digital from the inside. Being a digital device, it works and interfaces directly with any digital port, and does not require special analog to digital conversion circuitry.This blog describes the encoder switch and its usage in brief details, and introduces simple ways of using it with any microcontroller, mimicking the functions of a digital potentiometer in particular.





Photo 3. The encoder switch is ripped apart to show its guts. The encoding pattern is molded on the switch body A. The wiper contacts are mounted of the shaft B, with the spring C pushing the shaft to keep the wiper in contact with the encoding wheel. All switch components are held together by the retainer clip D.
Photo 4. Three wiper contacts are shown mounted atop the encoder shaft.

How it works

The encoder switch can be viewed as two ganged switches A and B that alternately connects to C terminal every time you rotate the shaft. Each step will produce a transition on either output. This can be better explained by looking at an actual oscilloscope trace shown in Photo 5.

The upper window shows the A and B output with a shaft rotation. The lower window shows the darkened part of the same trace magnified (horizontal) ten times. Let’s focus our discussion at the yellow trace in the upper window and call this the A trace.

A trace reveals how the shaft was rotated. Starting from the extreme left, the first falling edge shows when rotation is first started. As you start to rotate, slowly at first, pulses appear sparsely spaced, and then comes more closely packed as you pick up speed. Finally, the pulses separation widens again as you deccelerate to a stop.

This sequence of event already gives you useful information- how fast the knob was rotated (by measuring pulse spacing) and by how much (by counting the number of pulses).




Photo 5. Encoder switch output pulse pattern as it is rotated in clockwise direction.

Photo 6. Pulse pattern output from the same encoder when it is rotated in counterclockwise direction.  
You probably noticed there is still one important information that cannot be resolved from trace A: the direction of the rotation. Is the shaft turning in clockwise cw or conterclockwise ccw direction? This cannot be determined by looking at trace A alone. This is where the green trace B comes in. B trace is essentially a copy trace A, differing only on the time when the transition takes place. In fact, the two traces are said to have a phase displacement of approximately 90 degrees.

Photo 5 shows the pulse pattern when the encoder shaft is rotated in cw direction, while Photo 6 shows what happens when it is rotated in ccw direction. It is important to remind you at this point that the two rotations are entirely separate event, hence the pulse pattern would look entirely different. What is important this time is to examine what the B output is doing while A makes a transition.

During clockwise rotation:
when A output transits from high to low, output B is low
when B output transits from high to low, output A is high.
During counterclockwise rotation:
when A output transits from high to low, output B is high
when B output transits from high to low, output A is low.
 Summarizing what we have learn so far

Pulse spacing is a measure of the rate of shaft rotation. This is seldom used, but could be useful with some application.

Number of pulses determines by how much the shaft is rotated. A typical switch encoder produces 32 pulses on each channel in a full (360 degrees) rotation. Hence if we counted 16 pulses, we know that the encoder is rotated halfway. 8 pulses is ¼ of the way, and so on.

The phase relation between the two outputs tells us whether the rotation is in the cw or ccw direction.


Figure 1. Interfacing an encoder switch to a microcontroller does not require the use of any analog to digital conversion circuit. In fact, the two resistors shown in this schematic can even be omitted if mcu has internal pull up.
 


 Using the Encoder Switch


Now that we know how the encoder switch works, let us discuss how we can use it as a digital potentiometer in a microcontroller circuit. No specific microcontroller will be mentioned in this section, so that we can formulate a general algorithm that could be easily applied to any microcontroller of your choice.

First, lets pick a microcontroller port for the encoder switch. As discussed above, the encoder switch has two outputs, A and B. Two microcontroller inputs are then required to read this. Not just two general purpose inputs however. These inputs must be interrupt enabled, sensitive to the falling edge of the input. And then, connect the encoder switch with the MCU, like as shown in the schematic diagram in figure 1. The pull up resistors may be omitted if your microcontroller has pull up circuitry built-in.













In your program, assign a register as your digital potentiometer. A single 8 bit register will suffice in most cases, but you can extend this to any width as you like. Just put in mind that an 8 bit register will already take 8 full rotations to fill. Every bit you add doubles this number, so use only the number of bits your application requires.

For the purposes of this discussion, let us call the encoder register, or variable, encoder_value. The interrupt routines should be built along the following algorithm:

Interrupt routine A
If B=low then increment encoder_value,
else decrement encoder_value

Interrupt routine B
If A=high then increment encoder_value,
else decrement encoder_value
Writing the code in C, the above statement should look like as follows:

Interrupt routine A
If(B==0) encoder_value++;
Else encoder_value–;
Interrupt routine B
If(A==0) encoder_value++;
Else encoder_value–;
 Conclusion:

Photo 7. Encoder switch with push switch
As shown in our discussion, using and interfacing an encoder switch with a microcontroller is remarkably easy. With it, you can make your control system more user friendly. Encoder switches are currently available in several variants. Some may have not indents, others may include auxiliary switch. Shown in Photo 7 is an encoder switch with push switch function. This is nearly identical to the encoder we just discussed, but has extra push switch that can be activated by pushing the same knob that you use to rotate. You can use this push switch to implement additional functions; examples are as the “enter” key when the encoder switch is used to enter data, or maybe as a encoder register clear button. 

No comments:

Post a Comment