Arduino - Connecting Switch



    Sohail Gulam


    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.

    const int buttonPin = 8; // the number of the pushbutton pin
    const int ledPin = 2; // the number of the LED pin
    // variables will change:
    int buttonState = 0; // variable for reading the pushbutton status
    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 {
          // turn LED off:
          digitalWrite(ledPin, LOW);
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.