Resistors & 3-position switch on analog input?

Hi all, can i do that :

For use only 1 input w/ my 4 pôle / 3 position switch ?

And if yes, what value for resistors ?

Thx. :slight_smile:

As shown, all positions will read 1023 (or HIGH) on the analog input.

  • If you want to determine what position the 3-position switch is in, try monitoring S2-1 and S2-8 using 2 digital inputs (Ii.e. D3 and D4) with INPUT_PULLUP enabled. Connect S2-5 to GND. No external resistors are needed.

  • Now, if both D3 AND D4 is HIGH, then the switch is in the middle position. No debouncing is necessary. The code will be inherently debounced.

If a 10K resistor was put from pin A0 to ground, you'd find things work a bit better.

What value resistor would be dependent upon what value is desired at A0?

1 Like

You stole my words :slight_smile: That is exactly what i was about to say. Very good.

1 Like

Except! When the switch is moved there will be an OPEN circuit for some small length of time!

Quite right, which it why I suggested putting the 10K on the A0 side to deter some of the bouncing.

Debouncing will probably be needed just like with button switches.As for the switch middle pin, the 10k from the arduino pin to ground (pulled up) will give a fixed value when the switch is in that position just like in any other position where a voltage divider will be formed.

I used this technique in the past to connect a 13 pole switch to an analog input of a pic to read a certain value corresponding to each switch position.

The trick is to simply monitor and write your conditions based on both inputs.
These 7 states could easily be determined where "togxx" depicts direction of travel and all "togxx" states are one-shot:

pos1, pos2, pos3, tog12, tog23, tog32, tog21

Seriously, no debouncing is needed - its inherent.

Pseudocode for pos1, pos2, pos3:

  if (!A && B) {
    // switch is up (pos1)
  }
  else if (!B && A) {
    // switch is down (pos3)
  }
  else if (A && B) {
    // switch is in center position (pos2)
  }

This CAN work with different voltage dividers or a resistor-ladder, but personally I hate to see analog used when you REALLY want digital.

Don't do it unless you have a good reason!!! (Or unless you just want to do it as a learning-experiment.)

@dlloyd
thank you for your feedback, indeed the goal was to use a single analog input, precisely to save PINS. :smirk:

@Idahowalker
I'm not sure I understand correctly, the goal is to have for example 0V - 2.5V - 5V depending on the position of the switch.

Thanks for the link, I had come across this divider bridge story, but I don't understand how to use it in my case. In the first resistor I put 10K (the one to ground)? And with the targeted voltage, I deduce the second resistor to put? Or did I not understand anything?

I tested thank you indeed it works fine, so it's a bit like connecting the 3rd leg of a potentiometer or not?

@DVDdoug
there is indeed a bit of that too, I try to experiment with certain things, it's only my 2nd project. :slight_smile:

In that case position 1 could go to 5V.

Position 2 could go to 2 10K resistors wired in series from 5V to gnd with the resistors connection point going to the switch.

V1 is 5V, R1 and R2 are 10K, V2 to the analog in.

Position 3 would just be ground.

1 Like

Just as a side note your switch as drawn is a SP3T switch, a Single Pole Three Way or Three Throw switch. You are only switching a single pole. Voltage division has already been covered.

Ron

Are you using A0-pin --

as an analog pin?

or

as a digital pin?

I beleve he is analog reading. Otherwise it will not work with a 3 position switch as only 2 states will exist with a large window of uncertainty

Sorry to hear that there isn't even one spare pin left. Perhaps by providing a full circuit diagram we could suggest some new methods to save pins.

There's actually a benefit to having all pins user configured and connected. By default, pins are set to input (i.e. high impedance, High-Z) and act as antennae to bring in noise or EMI. With all pins configured and/or connected, your circuit is more suitable to operation in a noisy environment.

Here's a possible choice for your next Arduino (54 digital input/output pins and 16 analog inputs).

If you have a three position switch, then just select between ground, the midpoint of two resistors, and 5 V. Also usable for more positions with extra points in the divider.

If you have no other use for the analog input, then this is a very reasonable approach - for a good quality switch. For "tact" pushbuttons, not so reliable especially in the long term.

For switches with a centre "OFF", connect switch to ground selecting two resistors of at least 2k and use INPUT_PULLUP.

You need debouncing if it is important to determine the moment the switch is operated. In short, you need debouncing if you actually need debouncing. :face_with_raised_eyebrow:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.