Connecting switches

Hello, just a simple question, is it necessary to connect serial resistors to switches(0ohm), for short period i suppose that it isnt for short periods but for long periods (few minutes) is needed.
Which resistor do you suggest?

is it necessary to connect serial resistors to switches

No series resistor is necessary. A resistor to Vcc (pullup) or ground (pulldown) is necessary to prevent the pin from floating when a (normally open) switch is open. Have a look at this tutorial.

So i should connect it this way?

How much ohms do you suggest?

10k is pretty standard, anything from 100 ohms to 1M ohm might work, but higher resistances allow more
noise pick up and lower resistances waste more power consumption.

Just connect the switch between pin and ground.
Nothing else.
And use the pull up resistor that is inside the processor.

pinMode(switchPin, INPUT_PULLUP);

The pin will be normally HIGH, and LOW when the switch is pressed.
So change your code accordingly.

Did you read the link in post#1.
It explains when you can use the internal pull up, and when you shouldn’t.
Leo…

Check out this tutorial on YouTube, I think it addresses your question and shows how to configure ground / power on any pin: Make Any Pin a Ground Pin in Arduino - YouTube

A soldered on resistor, three I/O pins used up, and more code
Or one I/O pin and a ground.

The Mega has five ground pins, not two as explained in that video.
Leo..

Although not necessary, a series resistor is not a bad idea because it protects the pin.

Consider what would happen with your circuit if you accidentally set pin 2 to output low. You would have a short when the switch was closed! A serial resistor of an appropriate resistance would prevent this from exceeding the current rating of the pin.

This can also provide some level of protection from electrical noise being picked up by longer unshielded switch wiring. See the "Current Limiting" protection approach described here:

The simplicity of a minimal circuit will usually outweigh the potential benefits of added components at the cost of added complexity for the average Arduino beginner and the AVR microcontrollers can survive some significant abuse. It doesn't get any more easy to wire up and understand than a pin connected to ground via a switch.

pert:
Although not necessary, a series resistor is not a bad idea because it protects the pin.

But in post#2 and in the video there is no series resisor.
Both use a pull down resistor to ground, with 5volt on one or both switch wires.
5volt to switches on a breadboard could be equally 'dangerous'.

Most beginners have trouble understanding reverse logic.
For them an active pin must be HIGH, so they wire the switch between pin and 5volt, with a pull down resistor to ground. Many example sketches also use this method.

KISS. Switch between pin and ground. Nothing else.
Leo..

2017-08-05_20-33-24.jpg

My take on input switches. Inputs

Wawa:
But in post#2 and in the video there is no series resisor.

Yes, but the whole question here was:

cr0wl:
is it necessary to connect serial resistors to switches

Since the question came up and they seemed to have some concern about the potential for damaging a pin, I thought it worth mentioning that there is a reason for a series resistor, though it's not necessary. It seemed like cr0wl might have seen this done somewhere to have gotten the idea it was needed.

Wawa:
5volt to switches on a breadboard could be equally 'dangerous'.

Of course, and would also be protected against a short by a current limiting resistor.

Wawa:
KISS. Switch between pin and ground. Nothing else.

Which I did say in my last reply.