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?
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:
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 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.
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.
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.