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.

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


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 ;)

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

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.

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