pull up/down with rotary selector switch

I have a rotary selector switch with six positions, plus a common so seven contacts. I want to keep things simple by wiring each of the six switch legs to an input on the Arduino. Can I:

  • Connect each leg to ground via its own pulldown
  • Also connect each leg to an input pin
  • Use only a single 100 ohm resistor from the common to +5V

If that works, there isn't a way to combine all the pulldowns, is there?

You can use the internal pull-up resistors:

pinMode(2, INPUT);
digitalWrite(2, HIGH); // Enable internal pull-up

Then ground the common terminal. The pin with a LOW signal is the one selected.

You can use external pull-down resistors, one per pin, and connect the common terminal to +5. The pin with the HIGH signal is the one selected.

Common goes to ground.
The 6 positions go to an input pin with pullup resistor enabled.

No pulldowns needed.

Test for a low to see if switch is rotated to a position.

CrossRoads:
Common goes to ground.
The 6 positions go to an input pin with pullup resistor enabled.

No pulldowns needed.

Test for a low to see if switch is rotated to a position.

This.

Excellent! Thank you all. Any resistor needed btwn common pin and ground?

tastewar:
Excellent! Thank you all. Any resistor needed btwn common pin and ground?

No. The internal pull-ups will take care of ohm's law on each separate channel of the switch.

If you want to reduce pin use, another choice is to use resistors and a single analogRead() pin. Connect the Common and a pull-down resistor to the analog input pin. Connect different value resistors between each of the other switch pins and +5. The selected resistor and the pull-down resistor form a voltage divider and the analog input will tell you the voltage. Different settings will give you different readings from the analogRead().

For example, use a pull-down of 10K and pull-ups of:

1: open (no resistor, 0V, returns 0)
2: 100K (10K/110K * 1023 = about 93
3: 10K (2.5V, about 512)
4: 1K (10k/11K * 1023 = about 930)
5: 500 (10K/10.5K * 1023 = about 974)
6: 0 Ohm (direct to +5, returns 1023)

Thanks for the idea, John. I had seen this mentioned elsewhere, but have plenty of pins to spare. Am planning to put all six inputs on one PORT and use a single PORT read to determine which is selected.

I must be doing something very obviously incorrect. I have this wired up as suggested: 6 selector wires to pins 8-13, common to GND. I run the following sketch:

int pbStableVal=0, pbCurVal;
void setup ( )
{
  Serial.begin ( 9600 );
  for ( int p=8; p<=13; p++ )
  {
    pinMode ( p, INPUT );
    digitalWrite ( p, HIGH );
  }
  Serial.println ( "End of Setup" );
}

void loop ( )
{
  pbCurVal = PORTB & B00111111;
  if ( pbCurVal != pbStableVal )
  {
    Serial.println ( pbCurVal );
    pbStableVal = pbCurVal;
  }
}

But all I see on my serial monitor is "End of setup" followed by a single 63.

I hope I'm remembering correctly that my loop function is called from within an outer loop, and that I'm not supposed to be writing a loop. I'll be extra embarrassed if that's it...

I believe I got it -- use PINB rather than PORTB.

so
PORTB = Bxxxxxxxx
is for writing values out,

and
inputByte = PINB
is for reading?

Haven't tried that myself for anything yet.

I see what was happening... Reading PORTB gives you the value of the PORTB register. That's the same PORTB register used to enable the pull-up resistors for the INPUT pins. Naturally it will return 1's for all those pins. The PINB register reads the value at the pin itself, synchronized to the I/O clock. It is the correct choice for reading INPUT pins.

Either will do for reading OUTPUT pins.

Look up coded rotary switches. Using 2 in my next project. Can get them in decimal and hexadecimal form.

Like one of these?
http://www.mpja.com/BCD-ROTARY-SWITCH/productinfo/18252+SW/
http://www.mpja.com/prodinfo.asp?number=19002+SW
or something bigger?

CrossRoads:
Like one of these?
BCD Rotary Switch
HEX Rotary Switch
or something bigger?

That's them. Using 2 of them on this project. Very easy way to get 16 positions evaluated out of 4 pins (hexadecimal version). And they lock in position, no 'guessing' you're in the right place like with potentiometers.

I'm using these-> http://www.mouser.com/Search/ProductDetail.aspx?R=FR01AR10PB-W-Svirtualkey63300000virtualkey633-FR01AR10PB-W-S

Those are very cool, and would have been a great choice. I doubt they stock them at You-Do-It Electronics (my local), and even if they did, I wouldn't have know about them when I was shopping. But I do now! Thanks for educating me.

My hardware/methodology worked fine for my situation, btw, but it would have been much harder if I needed to select among more choices. As it was, I really only needed 5 out of the 6, and had to make up another choice to use that input. BTW, the selector was for choosing among multiple strings to be displayed on a scrolling LED sign that was a prop in our Christmas pageant. Worked flawlessly.