new to arduino, question on switches

hey everyone, i just got my arduino in the post today and im already playing with it!

i had a spare LED in my parts box so i jumped that into pin 13 and gnd and got to work with the code, making it flash different ways, the first thing i coded by hand was morse code S-O-S

i have a rocker switch which i bought a few weeks ago but never used, and id like to learn to use it with the LED, a real basic circuit where the switch turns the LED on

would it damage the arduino for me to plug one side of the switch intot he 5V rail on the board, and the other side into a pin slot also on the board, and then wire it up to say when that pin reads HIGH to turn the LED to HIGH too? or is it totally addamant i use a resistor in the circuit?

im totally new to electronics and im probably jumping ahead by getting an arduino, but its something i couldnt wait to get my hands on, so im not sure whats dangerous to the board and whats not

The correct way is to link the IO port to either +5V or GND using a resistor (10K? is good), and then use the switch to bridge between the IO pin and the other one of +5V or GND.

Having the switch between the IO and ground, and the resistor between the IO and +5V is known as "active low" switching as the IO reads "LOW" when the switch is on.

Swapping the +5V and GND is "Active High" as the IO reads "HIGH" when the switch is on.

There is a more complex circuit with two resistors and a capacitor which "debounces"* the switch, but that can be done with software.

  • When a switch turns on it's not a straight of->on opertaion, but it "bounces" between of and on a few times before settling down.

Hey.

You don't need to use "real" pullup-resistors.
I'm using a much simpler way to interface switches.

Just arrange the switch like this

GND -- SWITCH -- Arduino_PIN

and activate the Arduino's internal pullup-resistor.

Here's the code for that: (place it in the setup-function!)
pinMode(5, INPUT);
digitalWrite(5, HIGH);

Now the Pin is an input with activated internal pullup-resistor, which means the Pin is going LOW as soon as the button will be pressed.

Adding debouncing with internal pull-ups is an arse. I rarely use them.

majenko:
Adding debouncing with internal pull-ups is an arse. I rarely use them.

In what way? Internal vs. external pullups should work pretty much the same. I have good luck with internal pullups.