Toggle switch and LEDs

Hi! I have making a project (you can view the prototype in istructables). The system has two operation modes.
I want put a toggle switch to change the mode. I have read an old post where an user has the same question. Someone replied and he gave the solution.

For my application I’d have to use the third terminal connecting to another digital input and to GND, true?
I think so, correct me if I’m wrong.
That’s not the problem. I have turn on a LED when I active Mode 1 and turn on another LED when I active Mode 2.
I think I have to connect the LED in the following way:

So, If I press the toggle to the left, the LED turn on and Arduino receive HIGH level from the pin 2?

Regards, thanks and sorry for my English :smiley:

Connect the switch to Gnd and D2 only.
In setup(), use this:
pinMode (2, INPUT_PULLUP); // enable internal pullup resistor

In loop(), use this:

if (digitalRead(2) == LOW){
// code for D2 LOW
// turn on LED1, 
// turn off LED2
}
else {
//code for D2 HIGH
// turn off LED1, 
// turn on LED2
}

No external resistors needed, no chance to short 5V to Gnd.

The fist schematic is already flawed… Pin2 and GND have a hard connection, it’s never going to be high.

What you want, take a 2 position switch like you used in the drawing. Connect middle pin to GND. Connect both left and right to a LED with resistor (so 2, one for each mode). The anode of both LEDs connect to 5V. Now pick left or right (doesn’t really matter) of the switch and connect it to a Arduino pin as well. Use pinMode(pin, INPUT_PULL) Done!

Thanks to both of you!!
I think I have understood you.

With the above setting when I press the toggle to the left the LED will turn on and I'll receive a HIGH level in pin 2 of Arduino (configured as pull-up)?

With the above setting when I press the toggle to the left the LED will turn on

Usually the electrical terminals are opposite of the toggle position. So with the toggle to the right, the switch will (probably) be on. That connects the switched-side of the LED to ground and the LED will turn on.

and I’ll receive a HIGH level in pin 2 of Arduino (configured as pull-up)?

When the switch is off (open) pin 2 is pulled-up by the internal pull-up resistor and by the external resistor and LED. When the switch is on, pin 2 is pulled to ground (through the switch) so it will be LOW.

In other words, when pin 2 is low, the LED is on.

And if you still want one LED for each mode as mentioned in your original post, you’d wire it like so:-
Mode Switching.JPG

Don't forget that you have to configure the pin as INPUT_PULLUP, as mentioned in reply #2. With the circuits above, it's essential.

Thank you so much everyone for your help. You've solve my doubts.
Thanks!!!