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:

Raspberry Pi ON/OFF Switch

Do not confuse:

Hardware Preparation

1×Raspberry Pi 4 Model B
1×ON/OFF Square Switch
1×(Alternative) ON/OFF Round Switch
1×(Optional) Heat Shrink Tubing
1×(Optional) Soldering Iron
1×(Optional) Screw Terminal Adapter for Raspberry Pi

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

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.

ON/OFF Switch pinout

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

The wiring diagram between Raspberry Pi and ON/OFF Switch

This image is created using Fritzing. Click to enlarge image

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


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:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  • Create a Python script file switch.py and add the following code:
# This Raspberry Pi code was developed by newbiely.com # This Raspberry Pi code is made available for public use without any restriction # For comprehensive instructions and wiring diagrams, please visit: # https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-switch import RPi.GPIO as GPIO # Set the GPIO mode to BCM GPIO.setmode(GPIO.BCM) # Define the GPIO pin for your switch SWITCH_PIN = 16 # Define debounce time in milliseconds DEBOUNCE_TIME_MS = 200 # 200 milliseconds # Set the initial state and pull-up resistor for the switch GPIO.setup(SWITCH_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Initialize the switch state and previous state switch_state = GPIO.input(SWITCH_PIN) prev_switch_state = switch_state # Define a function to handle switch presses def switch_callback(channel): global switch_state switch_state = GPIO.input(SWITCH_PIN) # Add an event listener for the switch press GPIO.add_event_detect(SWITCH_PIN, GPIO.BOTH, callback=switch_callback, bouncetime=DEBOUNCE_TIME_MS) try: # Main loop while True: # Check if the switch state has changed if switch_state != prev_switch_state: if switch_state == GPIO.HIGH: print("The switch: ON -> OFF") else: print("The switch: OFF -> ON") prev_switch_state = switch_state if switch_state == GPIO.HIGH: print("The switch: OFF") else: print("The switch: ON") except KeyboardInterrupt: # Clean up GPIO on exit GPIO.cleanup()
  • Save the file and run the Python script by executing the following command in the terminal:
python3 switch.py
  • 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.
PuTTY - Raspberry Pi
The switch: OFF The switch: OFF The switch: OFF The switch: OFF -> ON The switch: ON The switch: ON The switch: ON The switch: ON The switch: ON -> OFF The switch: OFF The switch: OFF The switch: OFF

The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.

Video Tutorial


  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!