You are on page 1of 4

UG104: Praxis I

Asian Institute of Technology


Handout: Optical Encoder

September 2013
Undergraduate Program
Instructor: Waqar S. Qureshi, Matthew N. Dailey

Optical encoder for motion control through Arduino


Introduction: 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. Motion Controls are widely used in a variety of application.
Among them, position and velocity control are extensively used in applications such as robotics and drive
control. For example, to measure the velocity of a moving wheel of a ground robot or to pecisely determine
the direction and position of a robotic arm.

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
direction. With two sensors there are two bits of digital information, so you will need two input pins per
optical encoder.

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/


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


To make an encoder wheel, let us try encoder design program which can be download from http://
thedenneys.org/pub/robot/encoders/. We have already printed for you the pattern with 16 division.
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.

Component required
1. Arduino UNO R3
2. Breadboard

(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


3. Resistor (330ohm) and (10kohm)
4. capacitor
5. Reflective Optical Sensor TCRT50001
6. DC Motor
7. Digital Oscilloscope
8. DC power supply
9. printed cicuit board

Arduino pin numbers


Pin 2 as Encoder A
Pin 4 as Encoder B

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


/*
* Connect Encoder to Pins encoderPinA, encoder0inB, and +5V.
*
* Sketch by max wolf / www.meso.net
* v. 0.1 - very basic functions - mw 20061220
* edited by Waqar S. Qureshi
*
*/
int
int
int
int
int
int
int
int
int

val;
encoderPinA = 2;
encoderPinB = 4;
encoderPosA= 0;
encoderPosB= 0;
encoderPinALast = HIGH;
encoderPinBLast = HIGH;
nA = HIGH;
nB = HIGH;

void setup() {

pinMode (encoderPinA,INPUT);
digitalWrite(encoderPinA, HIGH);// for internal pullup resister
pinMode (encoderPinB,INPUT);
digitalWrite(encoderPinB, HIGH);// for internal pullup resister
Serial.begin (9600);
}
void
nA
nB
if

loop() {
= digitalRead(encoderPinA);
= digitalRead(encoderPinB);
((encoderPinALast == HIGH) && (nA == LOW)) {
encoderAPos++;

}
if ((encoderPinBLast == HIGH) && (nA == LOW)) {
encoderBPos++;
}
Serial.print (encoderPosA);
Serial.print (",");
Serial.print (encoderPosB);
Serial.println ("");
encoder0PinALast = n;
}

Description
When the code finds a low-to-high transition on the A channel, it increments the variable to account for the
rotation that the encoder captured. One disadvantage of the code above is that it is really only counting
one fourth of the possible transitions.

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.