I am trying to get an optical encoder working. It is out of an inkjet printer. Here is my test code.
// Encoder pin configuration
const int encoderPinA = 6; // Encoder pin A
const int encoderPinB = 7; // Encoder pin B
volatile int encoderPosition = 0; // Current encoder position
void handleEncoder() {
// Read the state of the encoder pins
int pinAState = digitalRead(encoderPinA);
int pinBState = digitalRead(encoderPinB);
// Update the encoder position based on the change in pins
if (pinAState != pinBState) {
encoderPosition++;
} else {
encoderPosition--;
}
// Print the current encoder position to Serial Monitor
Serial.print("Encoder Position: ");
Serial.println(encoderPosition);
}
void setup() {
// Initialize Serial Monitor
Serial.begin(9600);
// Set encoder pins as inputs
pinMode(encoderPinA, INPUT);
pinMode(encoderPinB, INPUT);
// Attach interrupt to encoder pin A
attachInterrupt(digitalPinToInterrupt(encoderPinA), handleEncoder, CHANGE);
}
void loop() {
// Do other tasks here if needed
}
If I attach a meter (I don't have an oscilloscope) and spin the motor the voltage of the sensors (with reference to ground) lowers so this suggests there is ouput. The colored lines on the back of the sensor shown are what I assume is the pinouts. Could be wrong.