Setting up 'switch'

I've been trying to set up something similar to the 'switch' tutorial on the Arduino website. I've written the program but I'm a little unsure about the wiring, here's what I have:
*a red LED
*a small breadboard
*an Arduino Mega 2560
*4 small male jumper wires
*A small button

Here's what I need to know:
*does the button need grounding?
*if so at the input or output side?
*do I need anything else e.g resistors?

Wire one side of the switch to ground and the other side to a digital pin. In setup() use pin mode(digital pin, INPUT_PULLUP) to set the pin to input and enable the internal pull up resistor. You will need a current limiting resistor for the LED. Around 200 ohms is good. Set the pin that you connect the LED to to OUTPUT with another pinMode function. The switch will read LOW when pushed.

Cheers, will I definitely need to connect the LED To a resistor. In the past I've been ok linking it directly to the Arduino although that was for program's where the LED was off for periods of time.

Always, always and always a resistor in series with a LED, always.

Pelle

As Pelle already said: you need always a resistor for a LED. The only question is: how many Ohm should that resistor have?

To answer this question you need 3 values:

  • the output voltage of your Arduino (in V)
  • the forward voltage of the LED (in V)
  • the forward current of the LED (in mA)

You can find the last 2 in the datasheet of the LED. If you have these values you can calculate the requiered resistor as

(Arduino Output Voltage - LED Forward Voltage) x LED Forward Current

Let us assume your LED has 1.8 V forward voltage and 30 mA forward current. If you have a "normal" Arduino (not a DUE) you will have 5 V output signals. Based on the given formular we get:

(5 V - 1.8 V) x 30 mA = 96 Ohm

So we take the next HIGHER resistor which should be 100 Ohm.

With load on the output the voltage are lower than 5volts.
The datasheet maybe gives the answer.

Pelle

The led was given to me along with the Arduino so I'm afraid I do not have a datasheet for it. Would it need to have a forward voltage of 5 volts to be safe for use?

Also would use with the resistor damage the LED or the Arduino?

the missing resistor can damage both the Arduino as well as the LED - but in most cases you will destroy the LED

Can anybody explain the input_pullup thing to me? I don't understand how you can have a circuit with no power.

When the internal pullup is enabled there is a built in resistor switched in between Vcc and the input pin. That way the pin is tied to Vcc and is in a known state with the switch open. Then pin can then be safely pulled to ground by the switch closure and the internal resistor will allow only a small current to flow through the switch to ground.

So is it like an input and an output?

Thanks for the help, guys. I got it working... without the resistor, it seems to be OK but I'm making sure I don't leave the led on for too long.

colonelhomer815:
So is it like an input and an output?

Only if you define "output" as a very weak 5V signal and no way to turn it LOW without losing the "input".

I meant as in it outputs power.

The voltage, with the to pin set to INPUT_PULLUP, comes from an internal Vcc connection to the internal pullup resistor. virtually no current flows into the input with the switch open. When the switch closes the input is pulled to ground and current will flow from the internal Vcc to ground through the resistor (Vcc/pullup value ohms).