Mouse Scroll Wheel Sensor and Arduino

Hello guys,
I'm trying to add this scroll wheel sensor to my Arduino project.
Does anybody have any ideas?
I would upload a photo but by now is imposible, there are 3 pins and
there appear to be two different steps, in one all the pins are connected, and in the other they're all disconnected.
This is a problem by the time of knowing in which way the wheel is going.

1 should come on before the other (slightly before you have to really watch it) that is what direction the wheel is spinning

The answer is "Quadrature". Here have a look at this: Rotary encoder - Wikipedia

The three contacts serve as two bits, with one contact being common. With two bits, you have 4 possibilities... 00, 01, 10, 11. The order they appear in tells you the direction.

Thanks to all, your help is very useful,
I'll show you when I get something stable,
all I got for now are some unsuccessful experiments.

You might find it easier to interface to the whole mouse...

yea but its less fun just pluggin it in and running someone else's lib, or that could be just me


yea but its less fun just pluggin it in and running someone else's lib, or that could be just me

Agreed :). Half of the fun is mucking through it yourself ;D!

Yeah that is right, we'll do it from 0.
I'm still trying the same thing no news, now i'll show you the code i've done (it's not supposed to work but has to evolutionate ) see if you know what to change or add:

int inPin = 7;   // pushbutton connected to digital pin 7
int inPin2 = 8;

unsigned long duration;
unsigned long duration2;

void setup()
  pinMode(inPin, INPUT); 
  pinMode(inPin2, INPUT);

void loop()
  duration = pulseIn(inPin,HIGH);   // read the input pin
   duration2 = pulseIn(inPin2,HIGH); // read the input2 pin


The final result is a mess of left and rights appearing rapidly when the pins are in connection mode.
As i've seen in the quadrature article, this component works by flanks, and not by the duration of the pulses.
Thanks to all

How about this one:

Quadrature Example
int quadAPin = 1;
int quadBPin = 2;

int lastQuadValue;

int CLOCKWISE = 1;
int CCLOCKWISE = -1;
int MISSEDPULSE = 999;

void setup() 
  lastQuadValue = readQuadValue();

void loop() 
  int newQuadValue = readQuadValue();
  int quadDir = getQuadDir(lastQuadValue, newQuadValue);
  if (quadDir == CLOCKWISE) Serial.print("Clockwise");
  if (quadDir == CCLOCKWISE) Serial.print("Counter Clockwise");
  if (quadDir == MISSEDPULSE) Serial.print("Missed Pulse Detected");
  lastQuadValue = newQuadValue;

int readQuadValue()
  int val = digitalRead(quadAPin);
  val = val * 2 + digitalRead(quadBPin);
  return val;

int getQuadDir(int prevVal, int newVal)

  //because the step order is 0, 1, 3, 2 lets do some switching around

  if (newVal == 3)
      newVal = 2;
    if (newVal == 2) newVal=3;

  if (prevVal == 3)
      prevVal = 2;
    if (prevVal == 2) prevVal=3;
  int quadDir = prevVal - newVal;
  //see if we missed a pulse (i.e. quadDir is 2 or -2)
  if (abs(quadDir) == 2) quadDir = MISSEDPULSE;
  return quadDir;

I haven't actually tried it, but it should work.

The main problem I have when running this codes is
that when the encoder is in connecting position a stream of
messages comes to the terminal, and when the device is in
disconnecting mode the messages stop. This programming might
be the one but with this problem I can't see anything.
It has to stop automaticly when the users stops moving the wheel
and it falls in connecting position.
I'll try to fix it after lunch.
Thanks a lot.

That's odd... the getQuadDir() function will return one of 4 values. Either 0 (the wheel didn't move), 1 (the wheel moved clockwise), -1 (the wheel moved counterclockwise) or 999 (a pulse was missed).

The loop() function should only print something to the terminal if something has changed (i.e. the wheel has been turned or a pulse was missed).

Now you're going to make me break out an old mouse, tear out the wheel and try it out! :wink:

Ok, there might be a connection problem, the pins are connected 2 to the i/o pins of Arduino, and the other one to 5 V. But when I change this
last pin to GND, same thing happens, Why? Anyway I think 5V is the
right way because can't connect them to 5V and GND in different pins
or it will short when connection mode.
Other idea is connecting a pull down 10kohm resistor like in a usual bottom somewhere.

You should connect it like a switch using a pull down resistor perhaps.

You should connect it like a switch using a pull down resistor perhaps.

This is very true with the mechanical knob type encoders, and I think you are correct, but I have never personally messed with optical encoders (like the one on a mouse wheel) so i cant say for sure

I dont feel like drawing up a new schematic right this second (nor do i have any wheel mice to cut open right now either) but maybe you can take reference from a schematic that i did for a totally unrelated project

just ignore the chip connections (a dual flip flop) and button

It is not an optical encoder, is mechanical, when the wheel rotates, it changes the position of the 3 pins from connection to disconnection that's all.
I'll try to connect a pull down resistor today to see what happends.

This is the device we are talking about:

Ok, after some search on the internet, we luckily found it on
an image in google.
Here it explains how to connect the encoder, and with that connection
everything makes sense. Now I'll do the connections and see if the codes
I've been trying in the past work now.

Here is the final solution, it was electrical connection mistake.
The last post was only an aproximation.
With this connection diagram and the software that G-man posted
it works perfectly.
Thanks a lot !! :smiley:

Welcome! :wink: