Reading position form a mouse wheel

Hi all,

I have stripped out a mouse well and the 3 pronged encoder, which just looks like a potentiometer (POT).

the mouse wheel moves in single click incraments which i would like to capture to a counter.

Does anyone know how i would do this ? Firstly would it need to be defined as an analog or a ditial sensor ?

I've wondered about this myself, but haven't experimented with it. What kind of signal does it send out? I'm guessing you get some kind of pulse. It's fairly simple to setup an external interrupt handler (using the attachInterrupt() command) that increments or decrements a counter so you can keep track of pulses. Obviously there has to be a second signal of some sort that helps you figure out which way the mouse wheel is rotating.

Hi Emdee,

I've already figured it out, here's my code if you want it. to wire it, you have 1 pin to GND and the other 2 to your digital input pins.

You need to use 2x10k resistors in line with the digital input pins to pull the signal down to get it to read cleanly;

Rich :slight_smile:

#define encoderPinA 2
#define encoderPinB 4

volatile unsigned int encoderPos = 0;

void setup() {
pinMode(encoderPinA, INPUT);
digitalWrite(encoderPinA, HIGH);
pinMode(encoderPinB, INPUT);
digitalWrite(encoderPinB, HIGH);

attachInterrupt(0, doEncoder, CHANGE);
Serial.begin(9600);
}

void loop () {

}

void doEncoder(){
if (digitalRead(encoderPinA) == HIGH) {
if (digitalRead(encoderPinB) == LOW){
encoderPos = encoderPos -1;
}
else {
encoderPos = encoderPos + 1;
}
}
else
if (digitalRead(encoderPinB) == LOW) {
encoderPos = encoderPos + 1;
}
else {
encoderPos = encoderPos -1;
}

Serial.println (encoderPos, DEC);

}

Nice job Ribuck!