Sliding switch problem - SOLVED

For my project I need to be able to determine the position (1 of 5) of a sliding switch.

I have wired a series of 1K ohm resistors between the switch position so that posn 1 would have 1K Ohms resistance, posn 2 would have 2K ohms, etc down to posn 5 with 5K ohms.

When I measure voltage with a meter I get 5 volts down to about 3.5 volts as I slide the switch from posn 1 to posn 5.

However when I try to read it using analogRead (pin A1) I get a pretty much steady 1023 (bounces a bit between 1020 and 1023) regardless which position the switch is in.

Clearly I'm doing something wrong.

Can anyone help?

Thanks!

Here's my code I'm using:

// include the library code:
#include <LiquidCrystal.h> //used to display light values for testing

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

float selectorValueFloat = 0;
float selectorValue = 0;

void setup()
{
lcd.begin(16, 2); // set up the LCD's number of columns and rows, clear the display:
lcd.noDisplay();
lcd.display();
lcd.clear();
}

void loop()
{
selectorValue = analogRead(1);

selectorValueFloat = selectorValue * (5.0 / 1023.0);

lcd.clear();
lcd.setCursor(1,0);
lcd.print(analogRead(1));
delay(5000);
}

Looks like you have wired it up wrong. Can you post a photo not more than 1000 pixels wide / high.

Here's a picture:

As an attachment - can't seem to embed in message.

Well, the idea is we find out how you've wired it to the Arduino...

In particular to GND and 5V? It must be wired to both.

Black wire goes to 5V.

Red wire goes to A1.

Where would I connect to ground?

Doesn't this need to be something like the attached to work?

(Basically like a pot but with discrete positions not a continuous slide....)

sliding switch.PNG

Yes, it has 5 discrete switch positions.

Dblackb99:
Yes, it has 5 discrete switch positions.

But I think the point is that the ends of the resistor snake have to be at 5V and GND, and you tap off the voltage at the various positions. Maybe this pic is better, with a normal pot for comparison.

Thanks everyone for your help for a newbie. Still learning but I think I've got it now. A little testing will confirm and I'll post the outcome. :slight_smile:

Yup, rookie mistake. Needed another resistor and connection to ground. Here's my latest setup and it's working fine. Just need to test for voltage on A1 and I can determine the switch position. Thanks everyone for your help!

Excellent!

Just for future benefit, could you sketch out the actual circuit inside that switch? Or link a data sheet and I'll try do one in Express if you like. .... or is it as I had in my previous post? (Might not have the right number of elements though.)

Also a good idea to go back to top post and add SOLVED to title.

Needed another resistor and connection to ground

Well you could do without that other resistor and connect that point directly to ground if you want.