Circuit 156 - Reading an incremental encoder with single display and PIC16F877

encoder single

An encoder is a device that converts the angular position or motion of a shaft or axis into an analogue or digital code. They are commonly used in the industrial, on automatic machines to control motors and movements, positioning and adjustments.
We have numerous types of encoders based on their operating modes, coding systems and output signals. Mainly we can divide them into absolute encoders and incremental encoders. 

Absolute Encoders, provide the angular position immediately and retain it when the power supply fails; this requires a fairly complex construction and a certain number of output pins to be able to read the encoding. These are high-precision, but expensive devices.

Incremental encoders, on the other hand, provide a pair of pulse outputs, normally on square wave between each other (two input) and from the analysis, the logic which they are connected must find the position, direction and speed of the shaft.
There are models that use optical or magnetic sensors; these are usually devices of a certain cost (several hundred euros) and with high characteristics (even 10k pulses per revolution and rotation speed of 30krpm), suitable for integration into automatic machinery and robotics.

However, there are other models, like those made with mechanical contacts that are not intended to be rotated by mechanisms, but rather, to be operated manually, by acting on a knob, similar to a potentiometer. This destination means that they have a low number of pulses per revolution (from 4 to 24) and a low maximum rotation speed (60-120rpm), which, together with the construction technology, make them very low cost objects (<€5) .

For more details see Figure 1 below 

encoder single2

Figure 1 - channels one and channel two

They are used intensively as input devices for digital systems, from instrumentation to audio equipment, where, for example, they replace analogue potentiometers. The angular movement is interpreted by the microcontroller and transformed into the PWM or voltage variations necessary for regulation.
Constructionally, the encoder contains two sliding contacts on suitably shaped tracks that open and close between a common terminal and two outputs A and B.
These outputs, equipped with pull-ups (resistors), are connected to the I/O of the microcontroller. The sequence of openings and closings that are determined by the rotation allows the software to process the information.

 It should be noted that, despite appearing in many examples on the WEB, using weak pull-ups integrated into the chip may not be a good solution. In fact, if you use these, you save two external resistors, but we are dealing with mechanical contacts, which require a minimum current to function correctly. The WPUs have quite high values (70-100k), while the datasheets generally indicate the need for a pull-up of 10k on average.
Use with currents that are too low causes uncertainties in switching, especially after a certain amount of use.

 From a logical point of view, the relationship between signal phases to determine the direction of rotation. As seen in the diagram in Figure 2 below.

encoder single3

 Figure 2- Direction sequence

  • If the rising transition of B precedes that of A, the rotation is counterclockwise (CCW - CounterClockWise).
  • if A precedes B the rotation will be clockwise (CW - ClockWise)

The interpretation of the signals is developed by considering the pulse transitions. There are several ways: the one we see now is based on one consideration. For more details see Figure 3 below

encoder single4

 Figure 3 - Square Waves

Let's observe from Figure 3 above how the superposition of the two square waves determines 4 phases in a cycle
It is, in practice, a two-bit Gray code, in which only one-bit changes at each transition.
We can analyze the situation by synchronizing with one or more switching edges through an I/O interrupt (INT) and checking with a lookup table. However, Baselines do not have this function, so you can resort to polling the I/O. Furthermore, we can also process variations of the Gray code from time to time. 
 First of all, we can easily determine whether state changes have occurred in the signals: if we take an image of the signal logic levels and then compare this with the current situation through an XOR, a change is indicated with a result other than 0.
If there has been a change, we perform an XOR between the previous situation, shifted to the left by one position, and the current one; each pair of values for CCW movement will be 0, while CW will be 1
For each variation, the program will derive a pulse in one of the two rotation directions, allowing both an up/down count and the detection of the angular position, evaluating the relationship between pulses per revolution and rotation angle. Furthermore, it is possible to detect the speed by calculating the period of the pulses and, consequently, also acceleration or deceleration.
It should be kept in mind, however, that encoders with mechanical contacts are subject to bounces during switching; given that bounces take up much longer than an instruction cycle, it is necessary to prevent the program from interpreting these events as valid signals.
Furthermore, since they are rotated by hand, some irregularities (chattering or slippage) are possible during the rotation, which, if not considered, would distort the count. Figure 4 below shows just this
 
encoder single5
Figure 4 - Bounces during switching
The datasheet states 3ms maximum bounce and 2ms for any noise during rotation. These are maximum values that are difficult to find on a new component, but possible after a long working time, with the wear of the contact surfaces.

favicon bacd

+(39) 347 051 5328

Italy - Kazakhstan

09.00am to 18.00pm

About

We offer the best and economical solutions, backed by 27+ years of experience and international standards knowledge, echnological changes, and industrial systems.

Marketing Materials

Spring Renovation
Industry
US Gas Company
Construct
Plus Project
Vam Drilling Service
X Project
X Project
Cabrrus Training

Marketing Materials1

Spring Renovation
Industry
US Gas Company
Construct
Plus Project
Vam Drilling Service
ultrasonic sensor
ultrasonic sensor
Cabrrus Training