Summary of Replace a rotary switch with a potentiometer
This article explains replacing expensive rotary switches with low-cost potentiometers in microcontroller projects using an ADC. It addresses signal instability by implementing hysteresis thresholds, ensuring clean state transitions despite noise. The method uses fewer port pins, offers flexible setpoints for nonlinear compensation, and requires a capacitor for filtering. A key trade-off is the loss of mechanical detent feel.
Parts used in Replace a Rotary Switch with a Potentiometer:
- Microcontroller
- Analog-to-Digital Converter (ADC)
- Potentiometer
- Capacitor
Microcontroller-based products sometimes require rotary switches. As many microcontrollers have an onboard ADC, it is easy to replace the rotary switch with a low cost potentiometer, when a rotary switch is too expensive or unavailable.
Although digitizing a potentiometer setting to act like a switch requires only a few instructions, an immediate problem is that instabilities in value occur at the switching threshold between one value and the next due to electrical or mechanical noise. The solution is to introduce upper and lower hysteresis thresholds about each transition so that the potentiometer needs to move beyond a threshold before another switch state is validated. For every updated switch state, another pair of thresholds replaces the previous. In this manner, the hysteresis provides clean switching between states.
There are many advantages to this: one port pin vs. N port pins for a rotary switch, lower cost, easier availability, and debounced switching. A disadvantage is the loss of detent feel. An additional feature of the setpoints is that they can be at arbitrary positions, for example, to compensate for nonlinear variations in potentiometer responses.
The hysteresis is typically a few counts greater than any noise that would cause unwanted switching. A capacitor is recommended between the potentiometer wiper and ground to filter out any contact noise.
Figure 1 Replacing N-throw Switch
The algorithm is illustrated in Figure 2. Once the pot setting is digitized by the ADC, it is compared with the lower threshold, and if below, the switch state is decremented and limited to zero. If the potentiometer setting is above the upper threshold, the switch state is incremented and limited to the maximum value. If there is a change in switch state, the lower and upper threshold values are updated and the subroutine ends.
Figure 2 Flowchart
For this hysteresis algorithm to work, the potentiometer setting must be read periodically and compared to the last switch state. This is to distinguish between the potentiometer setting crossing a threshold from a different state or being at the same potentiometer setting and the same state.
For more detail: Replace a rotary switch with a potentiometer
- How does the system handle electrical or mechanical noise?
The solution introduces upper and lower hysteresis thresholds so the potentiometer must move beyond a threshold before a new switch state is validated. - What component is recommended to filter contact noise?
A capacitor is recommended between the potentiometer wiper and ground to filter out any contact noise. - Can the switch setpoints be placed at arbitrary positions?
Yes, setpoints can be at arbitrary positions to compensate for nonlinear variations in potentiometer responses. - What is the main disadvantage of this replacement method?
The primary disadvantage is the loss of the detent feel found in physical rotary switches. - Why must the potentiometer setting be read periodically?
It must be read periodically to distinguish between crossing a threshold from a different state versus being at the same setting and state. - Does this method save port pins compared to a rotary switch?
Yes, it uses one port pin instead of N port pins required for an N-throw rotary switch. - What happens if the digitized value is below the lower threshold?
The switch state is decremented and limited to zero. - What happens if the digitized value is above the upper threshold?
The switch state is incremented and limited to the maximum value. - When are the threshold values updated?
The lower and upper threshold values are updated only when there is a change in the switch state. - Is this approach suitable when a rotary switch is unavailable?
Yes, it is easy to replace a rotary switch with a low cost potentiometer when a rotary switch is too expensive or unavailable.

