You are on page 1of 4

UG108: Praxis II

Asian Institute of Technology


Handout: Optical Encoder

January 2013
Undergraduate Program
Instructor: Chaiyaporn Silawatchananai, Matthew N. Dailey

Optical Encoder for Motor Control Tutorial


Introduction: Motion Controls are widely used in a variety of application. Among them, Position and
velocity control are extensively implemented in applications such as robotics and drive control, in order to
improve the performance. For example, robot arm can move its link to the desired position precisely. To
achieve that, the information of position or velocity need to feedback to computer or processor. In this
tutorial we will learn the principle of optical encoder for motor control, build your own encoder and using
Arduino to count the pulse.

Optical Encoders
An optical encoder is a sensor that is used to monitor the direction of rotation, position or velocity of a
rotary or linear operating mechanism. This device typically consists of four elements: a light source, a light
sensor, an optical disc and signal conditioning circuitry.
The light source is usually a light-emitted diode (LED) that transmits infrared light. The light sensor is
a phototransistor that is more sensitive to infrared energy than to visible light. The optical disc is connected
to the shaft being measured so that they rotate together. Usually, the disc is made of plastic, glass or metal.
It has opaque and translucent regions. The disc is placed between the light source and sensor as shown
in Figure 1. Optical encoders are classified as either incremental or absolute. This tutorial will focus on
incremental encoder.

Figure 1: A structure of optical encoder


As the disc is rotated, light passes through the translucent segment and is block by the opaque area.
Thus, the encoder sends the pulse-train signals as Figure 2.
If you always know which direction the disc is moving, then one sensor or one channel signal is enough.
But if you are changing the direction frequently, you may want to measure the direction as well.
By adding another optical sensor with carefully placed in relation to the first, you can detect the direction
of motion in addition to the speed. According to Figure 3, you see that the distance between the two sensors
is half of one division. Whenever one sensor is on the boundary between light and dark, the other is in the
middle of a solid light or dark area.
If the wheel is moving counterclockwise, sensor 1 sees a light or dark area before it reaches sensor 2. If
the wheel direction is reversed, then sensor 2 sees the light or dark first. (In the waveforms, high represents
light and low represents dark. Depending on your circuit it might be the opposite.) These sensor readings
are 90 degrees out of phase. A simple program in a microcontroller can detect these transitions and turn it
into a count that increases when the wheel goes one direction and decreases when the wheel goes the other

Figure 2: Optical Encoder with 1 sensor, reproduced from http://thedenneys.org/pub/robot/encoders/

Figure 3: Optical Encoder with 2 sensor, reproduced from http://thedenneys.org/pub/robot/encoders/


direction. With two sensors there are two bits of digital information, so you will need two input pins per
optical encoder.
The quality of an encoder is measured by the number of slot per round, called resolution. The higher
the number of slot, the more precise the angular position can be measured, and then the better the encoder.
For example, if the disk has 360 slots, then each slot represents 1 degree of rotation. By contrast, for the
disk with 760 slots, each slot refers to 0.5 degree of rotation.

Building your own encoder


If you are looking for optical encoders in the market, you will probably find that they are quite expensive.
Alternatively you can take apart a computer mouse which consists of two tiny encoder wheels, one for X
and Y motion. If you have these stuffs, you may not need to construct your own wheel. To make an encoder
wheel, let us try encoder design program which can be download from http://www.societyofrobots.com/
downloads/Encoder_Design12.exe written by Scoot Boskovich. You can specify the size of disc, the number
of slot or divisions, then save as .bmp file and print out.
For todays lab we will make a encoder and attach o the wheel or the rotating shaft. The reflective
optical sensor, TCRT5000L which include an infrared emitter and phototransistor in a package, is used to
detect the black area on the disc, and generate the pulse-trained signal when the disc is rotated. Figure
4a and Figure 4b shows a package of TCRT5000L and schematic for detection, respectively. The output
from sensors can connect to Arduino directly in order to count the pulse and check the direction. We can
interpret the number of counted pulse to the angular position in degree unit. For example, the encoder has
16 divisions and Arduino can count 20 pulses. It means that the disc is rotated for 20*360/16 = 450 deg.

(a) Reflective optical sensor, reproduced


from
http://www.robotshop.com/ca/PDF/
vishay-TCRT5000L-infrared-reflector-specs.
pdf

(b) Schematic for detection

Figure 4: Reflective optical sensor, TCRT5000L

Component required
1. Arduino UNO R3
2. Breadboard
3. Resistor (330ohm) and (10kohm)
4. Reflective Optical Sensor TCRT50001
5. DC Motor
6. Digital Oscilloscope
7. DC power supply

Arduino pin numbers


Pin 2 as Encoder A
Pin 4 as Encoder B

Example : Read quadrature Encoder (checks the Encoder in loop())


/* Read Quadrature Encoder
* Connect Encoder to Pins encoder0PinA, encoder0PinB, and +5V.
*
* Sketch by max wolf / www.meso.net
* v. 0.1 - very basic functions - mw 20061220
*
*/
int
int
int
int
int
int

val;
encoder0PinA = 2;
encoder0PinB = 4;
encoder0Pos = 0;
encoder0PinALast = LOW;
n = LOW;

void setup() {
pinMode (encoder0PinA,INPUT);
pinMode (encoder0PinB,INPUT);
Serial.begin (9600);
}
void loop() {
n = digitalRead(encoder0PinA);
if ((encoder0PinALast == LOW) && (n == HIGH)) {
if (digitalRead(encoder0PinB) == LOW) {
encoder0Pos--;
} else {
encoder0Pos++;
}
Serial.print (encoder0Pos);
Serial.print ("/");
}
encoder0PinALast = n;
}

Description

Figure 5: waveform of the A&B channel of an encoder, reproduced from http://playground.arduino.cc


When the code finds a low-to-high transition on the A channel, it checks to see if the B channel is high
or low and then increments/decrements the variable to account for the direction that the encoder must be
turning in order to generate the waveform found. One disadvantage of the code above is that it is really
only counting one fourth of the possible transitions. In the case of the illustration, either the red or the lime
green transitions, depending on which way the encoder is moving.

References
http://thedenneys.org/pub/robot/encoders/.
http://www.societyofrobots.com/sensors_encoder.shtml
http://playground.arduino.cc/Main/RotaryEncoders
http://www.robotshop.com/ca/PDF/vishay-TCRT5000L-infrared-reflector-specs.pdf
Industrial Control Electronics: Devices, Systems & Application by - Terry Bartelt.

You might also like