Optical Encoder For Motion Control Through Arduino
Optical Encoder For Motion Control Through Arduino
September 2013
Undergraduate Program
Instructor: Waqar S. Qureshi, Matthew N. Dailey
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.
Component required
1. Arduino UNO R3
2. Breadboard
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.