Using a 3 position switch

I am developing a multi-part project for my lathe. In part 1 I'm adding an RPM display. Included in the circuit is a 3-position 4-pole switch. I want the Arduino to "know" what position the switch is currently set to.

I figured that I could use an analog pin (say pin 2) to tell the difference by adding a different resistor to each switch position and connecting the 3 resistors to that pin.

My circuit is as follows:

<Arduino 5V pin>----<10K resistor>----<switch position 1>----<Arduino pin 2>
<Arduino 5V pin>----<5K resistor>----<switch position 2>----<Arduino pin 2>
<Arduino 5V pin>--------<switch position 3>----<Arduino pin 2>

I figured that this should produce values using analogRead of 0, 500 (or so) and 1023 depending on the switch setting.

However, calling analogRead on the pin produces 1023 every time.

Am I taking the right approach for this?

Gary

Yes, what is connected to the common? (Try making a hand drawing of it.)

I mean, now the bottom line will just link the 5V to the analog in.

And do you really need to save pins? Otherwise it's way easier to just use 2 inputs :slight_smile:

Nothing is connected to pins.

And do you really need to save pins?

Not sure. This project could grow to be quite complex.

Gary

Then it's up to you, reading them vi a single input saves you one pin but is more complex both hardware and software wise. Not rocket science but a lot more complex then simple a wire and digitalRead().

Instead of the text, try making a hand drawing of how you connected it and upload the photo here. But hopefully you already spot your error when you do :wink:

[edit2][edit]Awwwww, Fred spoiled the learning :stuck_out_tongue:

You posted so fast I hadn't finished adding the drawing so I thought... what the heck, Septillion's got this handled. ;D

Just in case the OP wants to continue with the idea that leads to trouble... image is attached.

I studied the PNG and it made sense - except for the part where I actually obtain the analog reading.

There seems to be no connection to an analog pin in your drawing.

So I did something VERY simple:

<5V>---<10K resistor>-+-
!
+------------

That is I bridged the resistor between 5V and GND. Then I connected the ground side of the resistor to analog pin 2.

This produced values of 0.

I moved the connection to the <5V> side of the resistor and got values of 1023.

Your thoughts?

Gary

Now add two resistors between 5v and GND and repeat your little exploratory lesson with the 3 nodes you now have.
Do this with different resistors and see if you notice anything.

I tried a 20K resistor and a 75K resistor. Both produced the same results as before: 0 and 1023.

Gary

garyrice:
I tried a 20K resistor and a 75K resistor. Both produced the same results as before: 0 and 1023.

Gary

I meant add two resistors end to end so you should have 3 'results'.
5v --- R1 --- R2 --- GND

I tried a 20K resistor and a 75K resistor. Both produced the same results as before: 0 and 1023.

Diagnosis:

  1. Wired incorrectly (no ground)
  2. Any one of the resistors is open
  3. Bad breadboard connection, common with 1/4 w Chinese resistors
  4. Open jumper wire (cheap Chinese stuff? Wouldn't be the first time they were bad)

Time to buy a multimeter.

I meant add two resistors end to end so you should have 3 'results'.
5v --- R1 --- R2 --- GND

Curious. Sampling this setup gave me 1023, 522 and 0 when I connected the sampling tap to A0 and at 5V, between the 2 resistors and at GND.

Now I just have to figure out how to actually wire my switch that way.

Thanks for your help.

Gary

Good it now works, but you need to understand how and why it works, so next time you will be able to figure it out by yourself.

Google "voltage divider".

It sounds like your wiring and measurements are all correct - as in, what it should be based on description, albeit not on what you yourself expected.

Your error: a resistor does not change voltage by itself. Putting a 5k resistor on a 5V line doesn't change the voltage. What you want is a voltage divider - and that's what you got in the final setup.

Your initial conception varied the current. If you put LEDs next to each resistor, you would have gotten 3 different brightnesses.

But the Arduino input looks at voltage, not current. Starting with 5V, you want to split it up across the steps so you can see the division.
Your initial conception was 'refueling' each step to full 5V.
Hope that helps.