Switch Button with Arduino

Hello! I want to make a simple circuit in arduino. I want to use a switch that when I press it, the arduino will light on an LED, and when I release the switch, the Arduino will light off the LED. I suppose I should supply the switch with 5 Volt DC, so that the arduino understands the press of the button --> 5 Volt and the release of the button ---> 0 Volt. My concern is if should I use any resistor in the switch or I just connected directly to the Supply Voltage and the pin of the Arduino? If I use resistors, what value should they have?

Please have a look at the example pages.

https://www.arduino.cc/en/Tutorial/BuiltInExamples

The examples included with the IDE, as helpful as they are, don't show how to wire a circuit.

The best way to use a pushbutton switch is to simply connect it from the input pin to ground, then instead of adding a resistor, use the chip's internal pullup resistor. (No connection to +5V is necessary with this method.)

The internal pullup resistor for a pin can be enabled by using "INPUT_PULLUP" instead of "INPUT" in the 'pinMode()' statement like this:- (Assume that digital pin 2 is to be used as an input for the switch, and digital pin 3 is the output to the LED.)

const byte buttonPin = 2;
const byte ledPin = 3;

void setup()
{
    pinMode(buttonPin, INPUT_PULLUP);
}

The button will register a LOW when pressed, (and HIGH when not pressed). It can then be checked in the code like this:-

void loop()
{
    if (digitalRead(buttonPin) == LOW)  // The button Is pressed.
        digitalWrite(ledPin, HIGH);     // Turn the LED on.
    else                                // The button is not pressed.
        digitalWrite(ledPin, LOW);      // Turn the LED off.

    // Other code here.
}

This is a very basic example, but will help you get started.

Hello, I did that, but the LED is not too bright! I also use a 580 Ohm resistor in series with the LED? Maybe I should remove the resistor?

setup() needs this as well: pinMode (ledPin, OUTPUT); Keep the resistor. Don't go lower then around 200, 220 ohm.

CrossRoads: setup() needs this as well: pinMode (ledPin, OUTPUT); Keep the resistor. Don't go lower then around 200, 220 ohm.

Thank you! It works.

How many of these buttons and how many leds can I connect to an Arduino MEGA 2560? I ask because I have read that supply of the USB can support maximum 500mA, so I have to calculate how much current these parts consume.

alex5678: How many of these buttons and how many leds can I connect to an Arduino MEGA 2560? I ask because I have read that supply of the USB can support maximum 500mA, so I have to calculate how much current these parts consume.

The Mega has 54 I/O pins and 16 Analog Input pins. So, theoretically you can connect about 70 Push buttons.

Use a 680R Resistor for each button.

If you want to connect LEDs, do not directly connect the LED to the Arduino. Use a Transistor as a Switch to operate the LED.

SagarDev: The Mega has 54 I/O pins and 16 Analog Input pins. So, theoretically you can connect about 70 Push buttons.

Use a 680R Resistor for each button.

If you want to connect LEDs, do not directly connect the LED to the Arduino. Use a Transistor as a Switch to operate the LED.

This includes the "trick" of using the INPUT_PULLUP on each switch?

The 680R Resistor I suggested was to limit the Current…

It is better to use the internal pullup to prevent floating values…