September 1, 2009
Update: A version of this article was published in the April 22, 2010 Issue of Electronics Design News (EDN) Magazine. It appears on page 71 of the magazine in the Design Ideas column entitled "Read multiple switches and a potentiometer setting with one microcontroller input pin." Introducing..."The Varswistor"A Varswistor is a portmanteau that I came up with to describe a simple analog electronic device consisting of a variable resistor and a number of switches which provide a analog and digital input to a microcontroller using a single pin. It's kind-of a combination of variable resistor and switch. The interesting part of this application has to do with the careful selection of the resistor values to provide not only an independent analog input but also a number of discrete switch inputs providing digital information.
I came up with this idea while working with a small 8-bit microcontroller and finding I had only one-pin left for any type of input I might want to add. I know I needed at a minimum a control knob (a variable resistor) but also wanted a number of switch inputs as well. Unfortunately only one pin was available, but it did have an A/D converter on it. So I wondered; could the A/D converter input be used for both analog control as well as switch inputs at the same time? Sure. Why not!
The challenge in working with small 8-bit microcontrollers is to effectively manage what limited resources are available. Recently one of my projects required the ability to tweak several operating parameters while the software was running. Having additional analog inputs from a potentiometer as well as several pushbuttons would be ideal. The problem however was that the microcontroller I was using had only one unused pin remaining. So after dreadfully considering the possibility that I might need to choose a different microcontroller with more pins I decided to try to figure out a way to cleverly add the inputs I needed with only one available input pin. Fortunately the available pin had an Analog-to-Digital Converter (ADC) function attached to it which could easily be used to read the variable resistor, but what about the pushbuttons? Then it came to me…what I really need is a Varswistor!
The Varswistor is a portmanteau that I came up with to describe a simple analog electronic device consisting of a single variable resistor and a number of SPST switches. The device provides a way to convey mixed analog and digital inputs into a microcontroller using a single input pin. The pushbuttons allow the user to select different modes, states or options while the analog input provides a method to convey a parameter which can be tweaked during run time.
The implementation of a Varswistor is quite simple as it requires only the analysis of a parallel resistor circuit and voltage divider. The interesting part of this application however has to do with the careful selection of the resistor values to provide not only discernable analog input, but also a number of discrete pushbutton input states as well. A basic schematic diagram is shown in Figure 1 above which contains two pushbuttons and a single variable resistor (R
The selection of the resistor values is a multi-step process, thus creating a spreadsheet aids in performing the calculations (see attached). In this example a 5k potentiometer ( R one can arrive at a direct analog input centered about the 0-255 range of the ADC (e.g. 78-178). To compute the appropriate high and low side bias resistor values we solve this circuit as a simple voltage divider. _{bias}
Substituting and solving for V = 255; _{max}V = 78; _{low}V=178 and _{high}R=5k_{adj}
The computed value of
Since the position of _{ }affects the overall resistance seen by the circuit when either switch is pressed, a range of values must be interpreted for each switch. To determine R for either PB1 or PB2 we solve this as a parallel resistor network at both extremes of the potentiometer’s position. _{sw}
For PB1 when R) seen by the voltage divider to the ADC is _{eff}Rsw in parallel with R and _{adj}R in series. Likewise at the minimum position the effective resistance (_{bias}Reff) is simply R in parallel with _{sw}R._{bias}
The value produced when PB1 is pressed (V Reff. _{max}
Observe that when less than the smallest value R produces by itself in order to uniquely determine the switch has been pressed. So _{adj}Reff must produce a value less than _{max}V as computed earlier. Simultaneously evaluating these equations we get;_{low}
Substituting and solving this equation for
Using the spreadsheet to compute
The code used to interpret both the switch positions as well as the analog input is shown below.
// Process both Analog and Digital I/O
{
// Sample ADC Measure(); // Trigger a measurement & wait GetValue8(&raw); // Get measurement value
// The following is good for; // RbiasLow = 3.3k ohms // RbiasHigh = 3.3k ohms // //
// Extract PB1, PB2 and Analog Percentage *switches |= PB2; *perct = 100; // 100% *perct = raw - 78; // n% (normalized to %) *perct = 0; // 0% *switches |= PB1; }
The limitation of this technique is of course that no more than one pushbutton can be pressed at any one time. Furthermore the potentiometer’s position can be read only when no other pushbuttons are pressed. But if these limitations meet your requirements, this might just be the way to go.
This example showed how two pushbuttons could be used within the Varswistor concept, but the number of pushbuttons which is not limited to only two. The graph in Figure 2 below shows various input ranges for up to 10 separate pushbuttons and one potentiometer all sharing the same input pin. Although the computed ranges do not overlap and are unique, it is doubtful than your ADC hardware is able to reliably distinguish these bands under all circumstances. As noted above choosing smaller resistor values will separate these bands farther apart creating a larger “guard range”. From the values computed however, using this technique for at least four pushbuttons and one potentiometer is well within reason. Experimenting with the spreadsheet provided helps make quick work of determining just the right series resistor values for each switch and its output range.
Downloads |

Home > Electronics Projects >