Arduino - Connecting Switch
Arduino - Connecting Switch
Pushbuttons or switches connect two open terminals in a circuit. This example turns on the LED on
pin 2 when you press the pushbutton switch connected to pin 8.
Pull-down Resistor
Pull-down resistors are used in electronic logic circuits to ensure that inputs to Arduino settle at
expected logic levels if external devices are disconnected or are at high-impedance. As nothing is
connected to an input pin, it does not mean that it is a logical zero. Pull down resistors are
connected between the ground and the appropriate pin on the device.
An example of a pull-down resistor in a digital circuit is shown in the following figure. A pushbutton
switch is connected between the supply voltage and a microcontroller pin. In such a circuit, when
the switch is closed, the micro-controller input is at a logical high value, but when the switch is
open, the pull-down resistor pulls the input voltage down to the ground (logical zero value),
preventing an undefined state at the input.
The pull-down resistor must have a larger resistance than the impedance of the logic circuit, or else
it might pull the voltage down too much and the input voltage at the pin would remain at a constant
logical low value, regardless of the switch position.
https://www.tutorialspoint.com/arduino/arduino_connecting_switch.htm 1/4
1/20/2021 Arduino - Connecting Switch - Tutorialspoint
Components Required
You will need the following components −
Procedure
Follow the circuit diagram and make the connections as shown in the image given below.
https://www.tutorialspoint.com/arduino/arduino_connecting_switch.htm 2/4
1/20/2021 Arduino - Connecting Switch - Tutorialspoint
Sketch
Open the Arduino IDE software on your computer. Coding in the Arduino language will control your
circuit. Open a new sketch File by clicking on New.
Arduino Code
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
} else {
https://www.tutorialspoint.com/arduino/arduino_connecting_switch.htm 3/4
1/20/2021 Arduino - Connecting Switch - Tutorialspoint
Code to Note
When the switch is open, (pushbutton is not pressed), there is no connection between the two
terminals of the pushbutton, so the pin is connected to the ground (through the pull-down resistor)
and we read a LOW. When the switch is closed (pushbutton is pressed), it makes a connection
between its two terminals, connecting the pin to 5 volts, so that we read a HIGH.
Result
LED is turned ON when the pushbutton is pressed and OFF when it is released.
https://www.tutorialspoint.com/arduino/arduino_connecting_switch.htm 4/4