Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
The push button and slide switch are widely used in Arduino projects. Though seemingly simple, there are areas of confusion when implementing into your project like wiring it. In this post, we will learn the working principles of the button/switch and how to use them.
The push button is also called a tactile button. The button commonly used for the breadboard is the four-legged button. These legs are internally connected in pairs. The upper left (Leg A) and upper right (Leg B) legs are connected to each other, and the lower left (Leg C) and lower right (Leg D) legs are also connected to each other.
Because of the internal connection, we only need to use two unconnected legs. However, the four-legged design makes the button stand stable during use.
The push button is designed to fit across the gap in the middle of the breadboard. This design only allows the legs to cross the central divider when oriented correctly (not when it is rotated 90 degrees), which makes the unconnected legs be located in different rows, avoiding a short circuit.
Now we use Leg A and Leg C to build the circuit. Connect Leg A to the power and Leg C to Arduino pin 2. When the button is pressed, we will have the path from 5V through Leg A, Leg C to pin 2. Pin 2 will be 5V.
However, when we release the button, there is no connection from the button to pin 2. The pin has no state/signal (HIGH or LOW). This is called a floating pin. We need to fix this problem.
If we connect Leg C directly to the ground, we will have the path from the ground to pin 2 when the button is released. But there is a problem when the button is pressed. We will have a short circuit between 5V and 0V: from 5V through Leg A, Leg C to the ground.
So what we do is to use a resistor, typically 10k Ohms, to connect the row of Leg C to the ground. When we push the button, very little current will flow between 5V and 0V. When the button is released, there is still a connection between the row of Leg C and the ground, which will pull the voltage on pin 2 down to 0V, and give the pin a LOW state. The resistor is called a pull-down resistor.
For Arduino projects using the push button, visit Automatic Pet Feeder and Bicycle Speedometer with Hall Sensor.
The slide switch has one input pin (pole) in the middle and two output pins (throws) at the ends. We call this switch the Single Pole – Double Throw (SPDT) switch. The output pins are either connected to the input pin or not depending on the location of the switch. When toggled, it disconnects from one output and connects to the other.
When we toggle the switch to Position 1, the middle pin will connect to the pin on top and then connect to 5V. So the input will be 5V. When the switch is toggled to Position 2, the middle pin will connect to the pin on the bottom and then connect to the ground. The input will be 0V.