Switch is a component which controls the open-ness or closed-ness of an electric circuit. Hence, the switch is open, circuit is broken and no current flows though the circuit. But when the switch is close current flows through it.
A switch can be of different type:
Let’s say you have evive with one digital pin configured as an input. If there is nothing connected to the pin, and your program reads the state of the pin. Will it be high or low? It is difficult to tell. This phenomena is referred to as floating. To prevent this unknown state, a pull-up or pull-down resistor is connected in-between that ensure that the pin is in either a high or low state, while also using a low amount of current. While using switches you use pull-up or pull-down resistors. With a pull-up resistor, the input pin will read a high state when the button is not pressed. In other words, a small amount of current is flowing between VCC and the input pin (not to ground), thus the input pin reads close to VCC. When the button is pressed, it connects the input pin directly to ground. The current flows through the resistor to ground, thus the input pin reads a low state.
Hence, when the switch is not pressed, you get a HIGH reading on the digital pin and when the switch is pressed you get a LOW reading, when you are using pull-up resistor. Generally you use a resistor of 10 kohm. If you are using pull-down configuration, the result is exactly opposite.
In this example we will change the state of the LED each time you press the switch. Build the circuit given below using a tactile switch, evive, 10k Ohm resistor, and jumper wires.
You can observe that the above configuration is pull-up configuration. For the above configuration the flow chart is given below.
With a pull-down resistor, the input pin will read a low state when the button is not pressed. In other words, no current is flowing between GND and the input pin , thus the input pin reads close to GND. When the button is pressed, it connects the input pin directly to VCC. The current flows through the resistor to ground, thus the input pin reads a high state.
In conlusion, when the switch is not pressed, you get a low reading on the digital pin and when the switch is pressed you get a high reading, when you are using pull-down resistor.
In this example also we are doing the same thing as above. Given below is the circuit diagram, flow chart and the Adruino IDE sketch.