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:
Hardware Preparation
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables.
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:
pin 1 | pin 2 | Raspberry Pi Input Pin's State | |
---|---|---|---|
1 | GND | Raspberry Pi Input Pin (with pull-up) | HIGH ⇒ OFF, LOW ⇒ ON |
2 | VCC | 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.
Wiring Diagram
This image is created using Fritzing. Click to enlarge image
To simplify and organize your wiring setup, we recommend using a Screw Terminal Block Shield for Raspberry Pi. This shield ensures more secure and manageable connections, as shown below:
For a secure and reliable wiring connection, we suggest using a Soldering Iron to solder the wires and pins of the ON/OFF switch. Afterwards, use Heat Shrink Tube for additional safety.
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.
Detailed Instructions
- 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.