When rotary encoders are used for angle positioning or measurement, they typically have three-phase outputs: A, B, and Z. Phases A and B output square waves with a 50% duty cycle. For each revolution of the encoder, phases A and B output a fixed number of pulses. When the encoder rotates forward, phase A leads phase B by a quarter cycle; when the encoder rotates backward, phase B leads phase A by a quarter cycle. The phase difference between the square waves output by phases A and B is 90°. For each revolution of the encoder, phase Z outputs one pulse. Since phases A and B output a fixed number of pulses per revolution, each pulse from phase A or B represents a fixed angle rotation of the encoder. When phase Z outputs one pulse, it indicates that the encoder has rotated one revolution. Therefore, rotary encoders can measure angular displacement and its direction.
We typically use incremental encoders , which allow the output pulse signal of a rotary encoder to be directly input to a PLC. The PLC's high-speed counter counts these pulse signals to obtain the measurement results. Different models of rotary encoders output different numbers of pulse phases. Some rotary encoders output three-phase pulses (A, B, Z), while others only output two phases (A and B), and the simplest only outputs phase A.
The encoder has 5 leads: 3 pulse output lines, 1 COM terminal line, and 1 power supply line (OC gate output type). The encoder can be powered by an external power supply or directly from the PLC's DC 24V power supply. The power supply "-" terminal should be connected to the encoder's COM terminal, and the "+" terminal should be connected to the encoder's power supply terminal. The encoder's COM terminal is connected to the PLC's COM input terminal. The A, B, and Z phase pulse output lines are directly connected to the PLC's input terminals. A and B are pulses 90 degrees out of phase, and the Z phase signal produces only one pulse per revolution of the encoder, typically used as a zero-point indicator. When connecting, pay attention to the PLC's input response time. The rotary encoder also has a shielded wire; this shielded wire should be grounded to improve interference immunity.
Encoder-----------PLC
A, B, and Z are respectively connected to the input points of the PLC (according to the specifications of the speed counter HSC).
+24V------------+24V
COM--------------24V-----------COM
The application circuit of the rotary encoder is as follows:
The 100pF capacitor is for debouncing, but it's best to add software debouncing during software processing to prevent false positives. The most common software debouncing methods are delay and multiple consecutive sampling, which will not be detailed here.
The output waveforms of A and B are as follows
The timing of each motion cycle can be seen from the output waveform.
The static state is 11 or 00. The result of XORing A with B is 0 (Note 1).
The microcontroller can use timer interrupts to detect the status of A and B, with the interrupt time ranging from 5 to 20 ms.
When A^B=0 is detected, it indicates a static state, and the states of A and B are recorded.
When A^B=1 is detected, it indicates that there is rotation. Read the state of AB. If AB changes from 11 to 01 or...
If 00 to 10 is forward rotation, then AB is reverse rotation if it is from 11 to 10 or from 00 to 01.
This method is relatively simple and reliable, and can detect both one-step rotations and rapid rotations.
Note 1: XOR operation
The XOR operation is a binary operation, and in C, it is represented by A^B as A XOR B:
1^1=0
0^0=0
1^0=1
0^1=1
The value is 0 if the two are equal, and 1 if they are not equal.