A switch is a component which controls the open-ness or closed-ness of an electric circuit. Meaning they control is current will flow in circuit or not. Like when you switch on your fan with a switch on your wall electricity board; it will let pass the current to fan so that it can work.
A switch can only exist in one of two states: open or closed. In the off state, a switch looks like an open gap in the circuit. This, in effect, looks like an open circuit, preventing current from flowing.
In the on state, a switch acts just like a piece of perfectly-conducting wire. A short. This closes the circuit, turning the system “on” and allowing current to flow unimpeded through the rest of the system.
(A circuit diagram with an LED, resistor, and a switch. When the switch is closed, current flows and the LED can illuminate. Otherwise no current flows, and the LED receives no power.)
In this tutorial we will learn the use of tactile push switch. But first we will again review the functionality of tactile push switch. A push switch allows electricity to flow between its two contacts when held in. When the button is released, the circuit is broken. This type of switch is also known as a Normally Open (NO) Switch. (Examples: doorbell, individual keys on a keyboard). In circuits, we will be using Four leg push switch, which has the following functionality:
(Push Switch Connection)
(The circuit between both red dots will be complete when we push the switch down)
Step 1 - Building the circuit
The following circuit enables the Raspberry Pi detect a change in voltage when the button (Switch 1) is pressed and requires three GPIO pins. The first will provide a signal voltage of 3.3V (Vcc), the next will ground the circuit (GND), and the third will be configured as an input (GPIO IN) to detect the voltage change.
When a GPIO pin is set to input, it doesn’t provide any power and consequently has no distinct voltage level. We need the pin to be capable of judging the difference between a high and low voltage, however in a floating state(GPIO INput state) it can incorrectly detect states due to electrical noise. To enable the pin to perceive the difference between a high or low signal we must ‘tie’ that pin (with external voltage), calibrating it to a defined value; 3.3V in this case!
To tie the input pin, we connect it to the Vcc 3.3V pin, hence when Switch 1 is open, the current flows through GPIO IN and reads high. When Switch 1 is closed, we short the circuit and the current is pulled to GND; the input has 0V, and reads low! As we are connecting Vcc directly to GND, which could allow are dangerous current to flow, the large R1 (10k?) resistor ensures that only a little current is drawn. To make the circuit even safer, we add the R2 (1k?) resistor to limit the current to and from GPIO IN.
Step 1. Connect Pi to Ground Rail. Use a black jumper wire to connect GPIO GND [Pin 6] on the Pi to the Negative (-) rail on the breadboard.
Step 2. Connect Pi 3.3V to Positive Rail. Use a red jumper wire to connect GPIO 3.3V [Pin 1] on the Pi to the Positive (+) rail on the breadboard.
Step 3. Plug your switch in. When breadboarding, make sure all of the legs of switch are in separate rows. To achieve this use central channel on the breadboard.
Step 4. Add 10k? Resistor. Connect from Switch Pin 1, to the Positive (+) rail. Orientation of resistors is unimportant.
Step 5. Connect Switch to Ground. Use a breadboard jumper wire to hook Switch Pin 3 to the Negative (-) rail.