Raspberry Pi - Switch
The toggle switch, commonly referred to as the ON/OFF switch, has two states: ON (closed) and OFF (open). When pressed, the switch will toggle between the two states, and the state will be maintained even when released.
This tutorial instructs you how to use Raspberry Pi with the ON/OFF switch. In detail, we will learn:
- How to connect the Raspberry Pi to the ON/OFF switch.
- How to program the Raspberry Pi to read the state of the ON/OFF switch.
- How to program the Raspberry Pi to check if the ON/OFF switch is changed or not.
Do not confuse:
|Raspberry Pi 4 Model B
|ON/OFF Square Switch
|(Alternative) ON/OFF Round Switch
|(Optional) Heat Shrink Tubing
|(Optional) Soldering Iron
|(Optional) Screw Terminal Adapter for Raspberry Pi
Or you can buy the following sensor kit:
|DIYables Sensor Kit 30 types, 69 units
Overview of ON/OFF Switch
A switch that alters its condition from ON to OFF, or OFF to ON, when pressed and maintains that state even after being released is known as an ON/OFF Switch. To modify the state, it must be pressed again.
The Switch Pinout
There are two varieties of the ON/OFF Switch: two-pin and three-pin.
In this tutorial, we will use a two-pin switch. With this type, there is no need to differentiate between the two pins.
How It Works
There are two methods for using an ON/OFF switch. The following is a wiring table for the ON/OFF switch, as well as the reading state on Raspberry Pi for both methods:
|Raspberry Pi Input Pin's State
|Raspberry Pi Input Pin (with pull-up)
|HIGH ⇒ OFF, LOW ⇒ ON
|Raspberry Pi Input Pin (with pull-down)
|HIGH ⇒ ON, LOW ⇒ OFF
We must select one of the two options. The remainder of the tutorial will use the first one.
This image is created using Fritzing. Click to enlarge image
Raspberry Pi Code - ON/OFF Switch
Similar to a button, an ON/OFF switch also requires debouncing (for more information, please refer to Why needs debounce for the button, ON/OFF switch?). T
※ NOTE THAT:
There are two common applications:
- The first: If the switch is set to ON, perform an action. If the switch is set to OFF, do the opposite.
- The second: If the switch is toggled from ON to OFF (or OFF to ON), take action.
- Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
- Make sure your Raspberry Pi is connected to the same local network as your PC.
- Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
- If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
- Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows. See to how connect your PC to Raspberry Pi via SSH.
- Make sure you have the RPi.GPIO library installed. If not, install it using the following command:
- Create a Python script file switch.py and add the following code:
- Save the file and run the Python script by executing the following command in the terminal:
- Turn the switch to the ON position.
- Check out the result on the Terminal.
- Then turn the switch to the OFF position.
- Check the result on the termial.
The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.