RASPBERRY PI | TOGGLE SWITCH
In this guide, you'll learn how to connect a toggle switch to a Raspberry Pi and write Python code to detect its state. This project is useful for implementing basic input controls, such as turning on/off a system or triggering an action when the switch is toggled.
SECTION 1 | MATERIAL NEEDED
- Raspberry Pi (any model with GPIO pins)
- A toggle switch (SPST)
- 2 x Female-to-female jumper wires
SECTION 2 | THE CONNECTIONS
The toggle switch has two terminals:
WHY IS PULL DOWN NEEDED?
The Raspberry Pi’s internal pull-down resistor ensures that the GPIO pin reads a stable LOW state when the switch is not pressed. Without this, the pin might float between HIGH and LOW, causing unreliable readings. This removes the need for an external resistor.
- One terminal connects to the Raspberry Pi's 3.3V pin (Pin 1).
- The other terminal connects to GPIO Pin 7 (Physical Pin 7, GPIO 4).
WHY IS PULL DOWN NEEDED?
The Raspberry Pi’s internal pull-down resistor ensures that the GPIO pin reads a stable LOW state when the switch is not pressed. Without this, the pin might float between HIGH and LOW, causing unreliable readings. This removes the need for an external resistor.
SECTION 3 | THE CODE
Once wired, we need a Python script to read the state of the toggle switch. The script below detects when the switch is turned ON or OFF and prints a message.
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
# Set pin 7 as an input with a pull-down resistor
GPIO.setup(7, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
print("Waiting for switch toggle...")
previous_state = GPIO.input(7) # Store initial state
while True:
current_state = GPIO.input(7) # Read switch state
if current_state != previous_state: # Detect a change
if current_state == GPIO.HIGH:
print("Switch is ON")
else:
print("Switch is OFF")
previous_state = current_state # Update state
time.sleep(0.1) # Debounce delay to prevent flickering
import time
GPIO.setmode(GPIO.BOARD)
# Set pin 7 as an input with a pull-down resistor
GPIO.setup(7, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
print("Waiting for switch toggle...")
previous_state = GPIO.input(7) # Store initial state
while True:
current_state = GPIO.input(7) # Read switch state
if current_state != previous_state: # Detect a change
if current_state == GPIO.HIGH:
print("Switch is ON")
else:
print("Switch is OFF")
previous_state = current_state # Update state
time.sleep(0.1) # Debounce delay to prevent flickering
void setup() {
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH); // Turn LED on
delay(1000); // Wait for 1 second
digitalWrite(13, LOW); // Turn LED off
delay(1000); // Wait for 1 second
}
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH); // Turn LED on
delay(1000); // Wait for 1 second
digitalWrite(13, LOW); // Turn LED off
delay(1000); // Wait for 1 second
}
SECTION 2 | TROUBLE SHOOTING
1. No Change Detected When Toggling the Switch
- Check that the correct pins are used (Pin 1 for 3.3V and Pin 7 for GPIO input).
- Verify that the switch is functioning by testing it with a multimeter.
- Ensure the script is running without errors.
- Increase the time.sleep(0.1) debounce delay to time.sleep(0.2) if needed.
- Some mechanical switches require hardware debouncing with a capacitor (optional but not required in most cases).
- The wiring might be reversed. Try swapping the connections.
- Modify the code to check for GPIO.LOW instead of GPIO.HIGH