An optical encoder sensor module is an electronic device that converts the mechanical motion of a rotating or linear object into digital signals. It uses a light-emitting diode (LED) as a light source and a photodetector to sense the light interrupted by a patterned encoder wheel or disk. This sensor is commonly used in robotics, motors, and systems requiring precise motion control and feedback.
Pin Name | Description |
---|---|
VCC | Power supply (3.3V to 5V) |
GND | Ground |
A | Output channel A (digital signal) |
B | Output channel B (digital signal) |
Z | Index pulse (optional, not on all encoders) |
// Define the pin connections
const int encoderPinA = 2; // Channel A
const int encoderPinB = 3; // Channel B
// Variables to store encoder state
volatile int encoderPos = 0;
bool encoderALast = LOW;
bool encoderBLast = LOW;
void setup() {
pinMode(encoderPinA, INPUT);
pinMode(encoderPinB, INPUT);
Serial.begin(9600); // Start serial communication at 9600 baud
}
void loop() {
bool encoderA = digitalRead(encoderPinA);
bool encoderB = digitalRead(encoderPinB);
if ((encoderALast == LOW) && (encoderA == HIGH)) {
if (encoderB == LOW) {
encoderPos--; // Moving backward
} else {
encoderPos++; // Moving forward
}
}
encoderALast = encoderA;
Serial.println(encoderPos); // Print the position
}
Q: Can I use this encoder with a 3.3V system?
A: Yes, most optical encoder sensor modules can operate at 3.3V.
Q: How do I increase the resolution of the encoder?
A: Use an encoder with more pulses per revolution (PPR) or implement software interpolation techniques.
Q: What is the purpose of the Z channel?
A: The Z channel, when present, provides an index pulse once per revolution, which can be used for precise position calibration.
Q: Can I use this encoder for linear motion?
A: Yes, but you will need a linear pattern for the encoder and a way to translate linear motion to the encoder disk or strip.