Help with a DPDT switch

Hey, I've got a DPDT switch of the on-off-on type.

I have a wire soldered to each pin on the switch, they are arranged like this:

5v------ o | o -----gnd
dig 11-- o | o --dig 12
gnd----- o | o -----gnd

Running a simple script:

/*
  DigitalReadSerial
 Reads a digital input on pin 2, prints the result to the serial monitor 
 
 This example code is in the public domain.
 */

void setup() {
  Serial.begin(9600);
  pinMode(11, INPUT);
  pinMode(12, INPUT);
}

void loop() {
  int sensorValueTw = digitalRead(12);
  int sensorValueTh = digitalRead(13);
  Serial.print("PIN 11 = ");
  Serial.print(sensorValueTw, DEC);
  Serial.print("  |  PIN 12 = ");
  Serial.println(sensorValueTh, DEC);
}

When the switch is in the middle, pin 11 flucuates between 0 and 1, moving switch to left makes it permanently 1 and moving switch to right has absolutely no effect, pin 12 remains a constant 0.

any help please?

'fluctuates' suggests 'floating'

5v------ o | o -----gnd
dig 11-- o | o --dig 12
gnd----- o | o -----gnd <<< change this gnd to 5v

Add external pull-down resistors to pins 11 and 12 and ground. Otherwise you will have floating input pin conditions when the switch is in it's center off position.

Lefty

Apologies, I had that setup (sans resistors) that you suggested, but had made a mistake on my ascii diagram lol!

THe resistors have fixed the floating value when it's off (i do get an occasional 1 in with my 0s though...) However not a cheep out of the pin 12.

i do get an occasional 1 in with my 0s though.

I would suggest that is because you don't have any software debouncing logic in your code. That may or may not be a problem for your specific application, but is solvable if required.

Lefty

No, no debouncing, I've got 11 digital inputs (9 pushbuttons + 3way switch), might be able to implement it on the 3-way as I don't see it being used as frequently.

However, still no life from pin 12 :frowning:

Spot anything:-

 pinMode(11, INPUT);
  pinMode(12, INPUT);

and you use:-

  int sensorValueTw = digitalRead(12);
  int sensorValueTh = digitalRead(13);

it works better if you read the right pins. :wink: