7
\$\begingroup\$

Would this work? I'm pretty sure in theory it should, but I don't see it often or at all. In theory, the voltage divider from 2:1 to 1:2 should give 3.3v at one end and 1.6v at the other end of the pot, giving the ADC a wide range to work with. And if the button is pressed, the R1+RV will act as a max 20k pullup, so the line would be down to 0v, which the ADC can be coded to recognize as a unique event, allowing both a button and a pot to exist on the same input pin, allowing the ADC to serve both purposes.

An input pin is saved, without any significant code changes, as the adc is already being polled for the pot.

schematic

simulate this circuit – Schematic created using CircuitLab

Would this work, and if so, as a curiosity, why isn't this more popular.

\$\endgroup\$
7
  • 3
    \$\begingroup\$ It would work fine. Perhaps add a small capacitor across the switch, to bypass pot sweep spikes and provide some debouncing when using the switch. \$\endgroup\$ Nov 8, 2013 at 7:17
  • \$\begingroup\$ Not exactly the same, but look at this circuit diagram and try to find something that looks familiar: sainsmart.com/zen/documents/20-011-901/schematic.pdf \$\endgroup\$
    – jippie
    Nov 8, 2013 at 7:52
  • \$\begingroup\$ @jippie no, I know about resistive ladders as multiple buttons, that's fairly common (car steering wheel radio controls, ipod/iphone inline controls). It's specifically the use of a pot and buttons on the same adc I was asking about, I've never seen it. \$\endgroup\$
    – Passerby
    Nov 8, 2013 at 16:38
  • 2
    \$\begingroup\$ You do sacrifice some resolution in the reading of the pot position this way. That might be perfectly acceptable, depending on your application and the nature of the control the user thinks they are turning. \$\endgroup\$
    – RBerteig
    Nov 21, 2013 at 23:00
  • 1
    \$\begingroup\$ You could identify a second switch pulling the input to VCC, as long as there's no chance both switches are pressed simultaneously. So decoding one analog adjustment and two mutually exclusive switch closures with a single ADC input isn't bad, and may be a useful trick in a low-pin-count micro. Think position sense with hard limit switches at each end. Then they are mechanically exclusive, and even fit with the analog signal. \$\endgroup\$
    – RBerteig
    Nov 25, 2013 at 19:43

6 Answers 6

6
+50
\$\begingroup\$

Yes, it should work although there are some issues to watch out for.

The tricky issue is that you have to be careful to detect and ignore the transistions between the 0 V switch level and the pot level. Some of them are going to look like valid pot levels, so you have to take multiple samples into account to decide if what appears to be a pot level is real or just a intermediate voltage while slewing between the switch and the pot. Keep in mind that real switches bounce, so this is more tricky than you probably imagine. One thing you know about a valid pot voltage is that it can't change that fast. This should help in weeding out intermediate readings.

Another issue is that you can't read the pot when the switch is pressed. There is nothing you can do about this with this setup. Whether that matters depends on the system and what the meaning of the pot position and a pressed switch are.

I can't say whether this is done "often" or not. Pot inputs by themselves are unusual, but of course they do exist. For this scheme to make sense, you have to have a system that needs both a pushbutton and a continuous setting from the user, and where you really really don't want to spend the extra pin. If this is the difference between fitting into a 28 pin micro or having to use a 44 pin micro, I'd probably do it. If I have other pins left over, I wouldn't do this because it's better to keep the complexity low. Separate pins for the pot and the button are going to be easier, and therefore less likely to have bugs, in the firmware.

\$\endgroup\$
2
\$\begingroup\$

I've used ADCs as inputs without issue, in a topology very similar to yours.

I didn't have the pot, but I did have a two-resistor divider to reduce the input voltage (it was on an ATxmega, which has a ADC input max of 2/3 Vcc), and a switch to pull the input to ground.

I think it'll work fine.

One thing you should probably have in mind is the button may not get you entirely to ground. Depending on the switch resistance, you may still have a few millivolts on the input, so you shouldn't assume that the button being pressed results in an ADC value of 0, but rather a ADC value of < ~10 counts, or thereabouts (test this!).

\$\endgroup\$
1
  • \$\begingroup\$ I've seen ADC used for conductive rubber switches, with the rationale that the software could then compensate for increased resistance due to aging. \$\endgroup\$
    – starblue
    Nov 8, 2013 at 16:26
2
\$\begingroup\$

All these answers and their comments are good, and add much insight. I am trying to see which one deserves the bounty, but wanted to add this as well.

Found a detailed app note on this very thing. Not just switches, but switches plus a pot on the same adc input. PDF Version with Graphics

enter image description here

The article includes formulas (and an Excel 2007 spreadsheet to automate things) on how to select the bias resistors and pot, though the example code for a microcontroller is no longer available.

The limitation of this technique is that you cannot press more than one pushbutton at any time. In addition, the microcontroller can read the potentiometer's position only when you are not pressing any other pushbuttons. This example shows how to use two pushbuttons, but the number of pushbuttons can vary. Input ranges are available for as many as 10 pushbuttons and one potentiometer, all of which share the same input pin (Figure 2). Although the computed ranges do not overlap and are unique, it is doubtful that your ADC hardware can reliably distinguish these bands under all circumstances. Choosing smaller resistor values keeps these bands farther apart, creating a larger guard range.

Using this technique with four pushbuttons and one potentiometer is well within reason. Experimenting with the spreadsheet helps make quick work of determining just the right series-resistor values for each switch and its output range.

Hackaday Comment Thread on this app note.

More than one button in the same pin is also a good resource question.

\$\endgroup\$
1
  • \$\begingroup\$ An embedded system we designed uses coded voltages to identify most of its buttons, following an example set in a reference design for the SOC at its core. Lots of keys, one GPIO pin. Works pretty well since all the buttons are physically separated from each other and there's no reason for a user to attempt to press more than one at a time. \$\endgroup\$
    – RBerteig
    Nov 30, 2013 at 19:48
1
\$\begingroup\$

This will work for sensing the position of a pot that is not connected to ground, or with other analog sources that you know will not go near ground, providing you don't mind loosing some ADC resolution.

In the more general case, many analog sensor inputs will be referenced to ground, and may go to ground under some circumstances, so this scheme could not be used. Also, many analog sources may object to being grounded - often by emitting the magic smoke.

This circuit could be used if you are really desperate for one more digital input, and are aware of the limitations, but I wouldn't recommend it for general use.

\$\endgroup\$
6
  • 1
    \$\begingroup\$ And that's likely the reason it isn't that common. But if you have a knob and a switch (click shaft to enter?) it would allow reading both from a single pin. \$\endgroup\$
    – RBerteig
    Nov 21, 2013 at 22:58
  • \$\begingroup\$ @RBerteig I completely forgot about those types of combo pots/switches when asking this. That's a very appropriate application. \$\endgroup\$
    – Passerby
    Nov 23, 2013 at 0:01
  • \$\begingroup\$ @PeterBennett when you say the analog sensor inputs may go to ground or analog sources objecting to being grounded, can you explain/expound on that? In my specific case, it's a general MCU adc input, which shouldn't have any problem with getting a direct VCC or Gnd referenced input. \$\endgroup\$
    – Passerby
    Nov 23, 2013 at 0:03
  • \$\begingroup\$ @Passerby: If the output of the sensor that is sharing the ADC input with the button goes to ground (or close to it), your program won't be able to distinguish between a low sensor reading and a button press, and will not detect a button press when the analog sensor output is low. If the analog sensor has a "totem pole" output or other low-impedance output stage, it may let the magic smoke out when the push button pulls the line low. \$\endgroup\$ Nov 23, 2013 at 0:53
  • \$\begingroup\$ @PeterBennett so that's only a concern if I'm sharing that ADC input between the pot/button and another sensor? If I'm only doing the above port/button combo, it shouldn't be an issue right? \$\endgroup\$
    – Passerby
    Nov 23, 2013 at 0:56
1
\$\begingroup\$

This should work. But you could make it better, which is why I doubt it is common.

  1. Assuming you have a full-range (0 to 5V) ADC, reducing R1 and R2 will increase your dynamic range and hence resolution of the potentiometer's position. Of course, you cannot reduce R2 to zero or you lose the distinctness of an activation of the switch.

  2. It is not as low-power as it could be. If you can afford a ceramic capacitor, say 10nF, to connect across the switch, you can easily increase your resistors by a factor of 10 or even 100, reducing power consumption accordingly. The capacitor will also help accuracy and repeatability, by low-pass filtering the voltage seen by the ADC and providing a low-impedance voltage source. And, finally, it will de-bounce the switch (you probably know that almost all mechanical switches exhibit contact bounce, quickly making and breaking contact multiple times when operated once, requiring de-bouncing either in software or hardware). As pointed out previously, such a capacitor is also important to get well-defined behavior as the potentiometer is turned, because this may produce transients, at the very least in the form of intermittently high-impedance.

    Of course, with such a capacitor, C*R will be your time constant (so when wanting (1-e)^3 accuracy within 0.1 s of a switch release, you had better stay below the combination of 10 nF and 3 Mega-ohm...)

  3. Your software requires some care. You will see transients, both from the switch and from mechanical motion inside the potentiometer. It's not hard to code for, but more involved then simply querying a single ADC conversion result. You'll at least need to check if the value you read is sufficiently stable across multiple conversions to assume you are not in a transient.

  4. You may be including unnecessary components: What good is R1 for (assuming your ADC input range goes all the way to the positive rail)? If R1 is supposed to limit the maximum output voltage to fit into your ADC range, then why isn't the potentiometer powered from a reference voltage at or closely below the positive ADC rail? That would require a current-limiting resistor at the output at the potentiometer instead, but it would be better. As such an analog supply voltage could easily be made much stabler than the IC supply voltage (which I'll assume your 5V battery is to symbolize), you can then get less technical noise in your ADC conversions.

    And finally, even if R1 is not needed to reduce the maximum output voltage, the same change in the circuit, if combined with a move to an analog supply which could be as simple as connecting to +5V elsewhere, brings the combined benefit of the above and of better utilizing your ADC input range without any additional component.

\$\endgroup\$
7
  • 1
    \$\begingroup\$ Point 4 is wrong. R1 serves a necessary purpose, which is to limit the power supply current when the switch is pressed and the pot is at its top setting. Without R1, the switch would short the power supply when the pot is at top. \$\endgroup\$ Nov 22, 2013 at 14:41
  • \$\begingroup\$ You are right! I stand corrected. I'm not sure yet how to salvage the rest of point 4, but yes, not all of it is correct as it stands now. \$\endgroup\$
    – user30985
    Nov 22, 2013 at 14:44
  • \$\begingroup\$ I think I managed to correct the mistake whilst offering the same potential improvement. \$\endgroup\$
    – user30985
    Nov 22, 2013 at 14:54
  • \$\begingroup\$ I undid the downvote because you corrected the blatant error. However, I still disagree. It is better to leave the top of R1 connected to the same supply powering the A/D because that makes the measurement ratiometric with the supply. When the A/D and resistor divider chain use the same reference voltages, variations in that voltage cancel out. \$\endgroup\$ Nov 22, 2013 at 17:30
  • \$\begingroup\$ Thanks. With a ratiometric configuration, the +5 V is the appropriate reference voltage, and removing R1 does not change that. But it helps by taking advantage of a larger range of the still ratiometric setup. The only conceivable disadvantage is if R2 and Rv have vastly different temperature coefficients. Then the original circuit cancels the effect if, and only if, the potentiometer is set at exactly 50%. But the effect is very small, and, as said, this, well, compensation works only for one potentiometer setting anyways. \$\endgroup\$
    – user30985
    Nov 22, 2013 at 18:51
1
\$\begingroup\$

I would not recommend voltage divider for one reason:

When you connnect any high impedance device for measuring, it will have an aquivalent resistance between the device and any resistor of the circuit (parallel equivalence), changing the resistors relationship.

For example, if you configure the resistors and pot to be 50-50%, when you connect the device it will have a 49-51%. It will not change too much due to the high impedance of the ADC, but you will loose accuracy. I mean, you can see the ADC as an another resistor which will change the equivalent resistance.

schematic

simulate this circuit – Schematic created using CircuitLab

\$\endgroup\$
2
  • \$\begingroup\$ Wouldn't that mean any use of a pot with an adc will have that inherent inaccuracy? \$\endgroup\$
    – Passerby
    Nov 26, 2013 at 21:55
  • \$\begingroup\$ Not only beacause of the pot, because the ADC itself has an impedance and if you connect it to the voltaje dividor, the relation will change because it would be like having a parallel resistor to R2. \$\endgroup\$
    – Chirry
    Nov 27, 2013 at 14:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.