Let's Make Robots!

Reading a Rotary Switch

In my current project I have a rotary switch as a sort of menu selector.

I could use 6 IO pins to read the state of the switch but with adding resistors I can find the position using one analog input pin.
This is probably common knowledge, but it’s new to me as I have never had the use for a rotary switch before. But for anyone that could have a use for it, this is what I did.
Between the contacts (in my case 6 positions) I added a 1k resistor. Then feeding 5V to the first value in the chain you get different voltage from any position in the resistor chain. This is easily read by an analogue input on any microcontroller.
So with using resistors and one analogue pin you can read any rotary switch position and with a couple of them probably make a very complex menu system.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Nice tip, Geir! Thanks for finding time to share! :D

Good tip, Geir! Got me thinking :)

Love to see your enhancements on this, as it seems to be an overlooked function here at LMR. I haven’t seen it used anyway.

A different solution though you will need more different resistor values but are able to easier deside/calculate the output voltage for each position.

I guess it is similar to this:

Resistor divider

Your probably right and thanks for the input. I haven’t hooked anything up yet so I’ll be sure to add the ground resistor. But is the capacitor necessary or just good practice? Is it for debouncing?

A quick calculation shows:


1: output = 1.25V

2: output = 1.67V

3: output = 2.5V

4: output = 5.0V

I would probably find resistor size that would give a more linear output. In my example I only use 4 positions but with 6 positions the following output voltages would be 1V and 0.83V.

I hope you can see where I am going ;)

Yes, mainly for de-bouncing.

You should also in your code be prepared to handle the situation where the analog input is read exactly when you switch (between two positions).

I would guess that for one position the voltage on the input can change a little depending on the stability of you powersupply. So I would check for the values within ranges depending on your experiences.

A good article about de-bouncing (fo digital inputs though) http://www.ganssle.com/debouncing.htm


I would guess that you also will need at 'ground/bottom' resistor to make a voltage divider? If yes what resistor value did you use?

Correct me if I am wrong using this method will not create a linear input scale (not that there anything wrong with this).

I have also run across pages that mention, rather than using an analog pin, one can also add a capacitor(connected to ground?) and then to read an input from a resistor ladder. First, one makes the pin an output and charges the cap. Then, switch the pin to input and measure the time required to discharge the cap. The R/C time constant will be different for different positions on the resistor ladder.