Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Arduino connects the switch


May 15, 2021 Arduino


Table of contents


button or switch to connect the two open terminals in the circuit. /b10> This example turns on the LED on pin 2 when the button switch connected to pin 8 is pressed.

Arduino connects the switch

Drop-down resistor

Use a pull-down resistor in an electronic logic circuit to ensure that Arduino's input signal reaches the expected logic level when the external device is disconnected or in a high impedance state. A lthough nothing is connected to the input pin, this does not mean that it is a logic 0. /b11> The pull-down resistor is connected between the ground and the corresponding pins on the device.

The following illustration shows an example of a drop-down resistor in a digital circuit. /b10> A button switch is connected between the supply voltage and the microcontroller pin. /b11> In such a circuit, the microcontroller input is at a logic high value when on and off, but when the switch is on, the drop-down resistor pulls the input voltage down to ground (logical zero value) to prevent the input from being in an undefined state.

The pull-down resistance must be greater than the impedance of the logic circuit, otherwise the voltage may drop too much, and the input voltage at the pin will remain at a constant logic low, regardless of the position of the switch.

Arduino connects the switch

The required component

You will need the following components:

  • 1 × Arduino UNO board
  • 1 × 330 ohm resistor
  • 1 × 4.7K ohm resistor (pull-down)
  • 1 × LED

Program

Connect according to the circuit diagram, as shown in the following image.

Arduino connects the switch

Sketch

Turn on the Arduino IDE software on your computer. U se arduino to encode and control your circuitry. /b11> Open a new sketch file by clicking New.

Arduino connects the switch

Arduino code

// constants won't change. They're used here to
// set pin numbers:
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);
   }
}

Code description

When the switch is on (the button is not pressed), there is no connection between the two terminals of the button, so the pin is grounded (via a drop-down resistor) and we read low. /b10>When the switch closes (the button is pressed), it establishes a connection between its two terminals, connecting the pins to 5 volts, so that we can read high.

Results

The LED lights up when the button is pressed and the LED lights off when the button is released.