Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [here] and join today!
2.3 Slide Switch
Introduction
In this project, we will learn how to use a slide switch to control two LEDs. Slide switches are commonly used as power switches in electronic circuits. Here, we demonstrate its functionality using a breadboard setup. While slide switches are often soldered onto PCBs for stability, we will use it in a flexible breadboard configuration to better understand its operation.
What You’ll Need
To complete this project, gather the following components:
COMPONENT |
PURCHASE LINK |
|---|---|
- |
|
Raspberry Pi |
- |
Circuit Diagram
The Slide Switch middle pin is connected to GPIO17, while two LEDs are connected to GPIO22 and GPIO27. The switch toggles between the two LEDs, alternately lighting them up.
Wiring Diagram
Follow these steps to build the circuit:
Connect the middle pin of the Slide Switch to GPIO17.
Connect one side pin of the Slide Switch to GND and the other to VCC.
Connect LED1’s anode to GPIO22 via a current-limiting resistor.
Connect LED2’s anode to GPIO27 via a current-limiting resistor.
Connect the cathodes of both LEDs to GND.
Running the Example
All example code used in this tutorial is available in the ai-lab-kit directory.
Follow these steps to run the example:
cd ~/ai-lab-kit/python/
sudo python3 2.3_SlideSwitch.py
This Python script demonstrates the use of a slide switch to control two LEDs on the Fusion HAT+. When executed:
Slide Switch Activated:
Turns off LED1 (connected to GPIO pin 22).
Turns on LED2 (connected to GPIO pin 27).
Slide Switch Not Activated:
Turns on LED1.
Turns off LED2.
The program continuously checks the state of the slide switch every 0.5 seconds and adjusts the LEDs accordingly.
The script runs indefinitely until interrupted by pressing
Ctrl+C.
Code
Here’s the Python code to control the LEDs based on the state of the Slide Switch:
#!/usr/bin/env python3
from fusion_hat.pin import Pin, Mode, Pull
from time import sleep # Import sleep for delay
# Initialize slider (Button) on GPIO pin 17
slider = Pin(17, mode=Mode.IN, pull=Pull.DOWN)
# Initialize LED1 connected to GPIO pin 22
led1 = Pin(22,mode=Mode.OUT)
# Initialize LED2 connected to GPIO pin 27
led2 = Pin(27,mode=Mode.OUT)
try:
# Continuously monitor the state of the slider and control LEDs accordingly
while True:
if slider.value() == 1: # Check if the slider is pressed
led1.off() # Turn off LED1
led2.on() # Turn on LED2
else: # If the sensor is not pressed
led1.on() # Turn on LED1
led2.off() # Turn off LED2
sleep(0.5) # Pause for 0.5 seconds before rechecking the sensor state
except KeyboardInterrupt:
# Handle a keyboard interrupt (Ctrl+C) for a clean exit from the loop
pass
Understanding the Code
Library Imports
Import necessary libraries for GPIO control and adding delays.
from fusion_hat.pin import Pin, Mode, Pull from time import sleep # Import sleep for delay
Component Initialization
Configure the Slide Switch as a Button and initialize two LEDs connected to GPIO pins 22 and 27.
# Initialize slider (Button) on GPIO pin 17 slider = Pin(17, mode=Mode.IN, pull=Pull.DOWN) # Initialize LED1 connected to GPIO pin 22 led1 = Pin(22,mode=Mode.OUT) # Initialize LED2 connected to GPIO pin 27 led2 = Pin(27,mode=Mode.OUT)
State Monitoring and LED Control
In the main loop, the program monitors the state of the Slide Switch. When the switch is toggled, it alternates between turning on LED1 and LED2.
# Continuously monitor the state of the slider and control LEDs accordingly while True: if slider.value() == 1: # Check if the slider is pressed led1.off() # Turn off LED1 led2.on() # Turn on LED2 else: # If the sensor is not pressed led1.on() # Turn on LED1 led2.off() # Turn off LED2 sleep(0.5) # Pause for 0.5 seconds before rechecking the sensor state
Graceful Exit
The script terminates gracefully when interrupted using Ctrl+C.
except KeyboardInterrupt: pass
Troubleshooting
Slide Switch Does Not Respond
Cause: The slide switch is not connected properly, or the pull-up resistor configuration is incorrect.
Solution: Confirm that the slide switch is connected to GPIO pin 17 and ground. Ensure
PULL_DOWNmatches the switch configuration.
LEDs Flicker or Behave Erratically
Cause: Debounce issues with the slide switch.
Solution: Add software debounce to stabilize the input signal:
if slider.value() == 1: sleep(0.05) # Debounce delay
Extendable Ideas
Two-Way Switch Control
Combine two slide switches to control LEDs in a more complex setup, such as a simple light control panel.
Audio Feedback
Add a buzzer that sounds when the slide switch is activated:
from fusion_hat import Buzzer buzzer = Buzzer(Pin(22)) if slider.value() == 1: buzzer.on() else: buzzer.off()
Conclusion
This project demonstrates how to use a Slide Switch with the Fusion HAT+ to control LEDs. Slide Switches are versatile and durable components, often employed in user interfaces and control systems. Experimenting with them opens up possibilities for creating advanced and interactive projects.