How do you hook a rotary encoder up to an arduino?

Hi. I am trying to get some rotary encoders hooked up to an arduino via my breadboard.

I'm following along with the Arduino playground - rotary encoders page. It seems to imply that I need to hook one pin up to +5v, and the other two pins up to analog pins. When I do this, however, I get no reading from the pins other than "LOW," regardless of how I twist the nob.

I feel like I need to hook something up to ground. I got a potentiometer working just fine by hooking up one pin to +5v, one pin to analog 0, and one pin to ground. But then, there are only three pins.

Can somebody clue me in here?

Best way to wire a standard mechanical encoder switch with a common connection is too ground the common connections (this would wire to a Arduino ground terminal) and wire the a and b contacts to arduino digital input pins. Then in your software activate the soft pull ups for those two inputs.

Lefty

Thank you for the response but I am still a bit stuck (I am an electronics neophyte.)

The rotary encoder I purchased...

...has three pins in the front. It also has two pins in the back but I gather those are for the push-button functionality. Are the A and B contacts specific pins, or can I use any of the three pins in front as the A and B contacts? Also, what are the common connections and how do I ground them?

The sparkfun page has a link to the data sheet. Towards the bottom of the data sheet they show the pin out of the encoder. The middle pin of the three is the common terminal (this would wire to a Arduino ground terminal) and the outside terminals are the B and A contacts. The two contacts on the other side is the independent push switch.

Lefty

Hi,
I can't tell from the datasheet, but this may be an open collector configuration. If that is the case, then pin C is a +5v (HI) input. Pins A and B, however, will not just read hi/lo. They will read HI and "unconnected". To get clean readings you will need pull down resistors, so that the unconnected state is forced to ground. Basically, hook the A pin to an arduino digital in pin, and then from there through a 10K resistor to ground.
There's a decent tutorial about pull-up and pull-down on this page:
http://www.ladyada.net/learn/arduino/lesson5.html

Hope this helps,

Here's a recent posting "on another network" :wink: by someone who used a similar (possibly even functionally-identical) encoder:

http://forums.ladyada.net/viewtopic.php?f=22&t=5417

Ran

Thanks so much guys. That tutorial about pull-ups and pull-downs was very enlightening, and now I've got my encoders working just how I want them to.

Little follow up question. Does anyone know where I can get rotary encoders that spin very freely? I am looking for some encoders that are as easy to spin as mouse-wheels are.

I suppose I could just go saw open some mice and try to steal the encoders out of them, but that seems like overkill.

the encoder you bought has detents. If you want a smooth rotary motion, you want an encoder without detents. Digikey, mouser, etc, should have what you are looking for.

There are encoder add-ons for robot wheels that might be adaptable to your purpose. Try a few places that cater to robo-hobbyists, like pololu.com, solarbotics, and the Seattle Robotics Society.

HP used to make (maybe still does) motor shaft encoders with very high resolution. You might be able to find some surplus. I think they were pretty pricey new, though (on the order of $100 apiece??).

Ran

I think an optical sensing method would be the best way to do this. The suggestion to look for robot wheel sensors is a good one! You can make/print an encoder wheel with black stripes that can be read optically with a reflective opto sensor.

I need to use 2 rotary encoders for map scrolling on a table projection, In terms of hardware what do I need and how do i hook it up?
Im thinking
2 stepless rotary encoders (where do i buy these?)
1 duemila nove
breadboard
wiring(schematics anyone)
resistors?

Here is an example of a code that works for the Arduino.

It's not mine, I found it here : .g7nbp :: {random noise}: Jogging and shuttling with Arduino.

/*
Jog test - 1
(a minimal quadrature type decoder for jog dials)
*/

int clock = 2;             // Define encoder pin A
int data = 3;              // Define encoder pin B
int count = 0;             // pre-init the count to zero
int c = LOW;               // pre-init the state of pin A low
int cLast = LOW;           // and make its last val the same - ie no change
int d = LOW;               // and make the data val low as well

void setup() {
 pinMode (clock,INPUT);  // setup the pins as inputs
 pinMode (data,INPUT);
 Serial.begin (9600);    // and give some serial debugging
}

void loop() {
 c = digitalRead(clock); // read pin A as clock
 d = digitalRead(data);  // read pin B as data

 if (c != cLast) {       // clock pin has changed value... now we can do stuff
   d = c^d;              // work out direction using an XOR
   if ( d ) {
     count--;            // non-zero is Anti-clockwise
   }else{
     count++;            // zero is therefore anti-clockwise
   }
   Serial.print ("Jog:: count:");
   Serial.println(count);
   cLast = c;            // store current clock state for next pass
 }
}