Skip to content

Using an External Switch with Raspberry Pi

Introduction

In this guide, we will explore how to interface an external switch with a Raspberry Pi to control an LED and print an interrupt detection message on the terminal. The external switch will serve as a simple input device that, when pressed, will trigger an action on the Raspberry Pi. We will utilize the GPIO (General Purpose Input/Output) pins on the Raspberry Pi to establish a connection with the switch and control the LED accordingly. Additionally, we will implement an interrupt-based approach to detect the switch press event. By following this guide, you will gain the necessary knowledge to integrate an external switch with the Raspberry Pi and create interactive applications or automation systems that respond to user inputs. Let's get started and explore the exciting possibilities of using external switches with the Raspberry Pi.

Problem Statement

Switching on an external led using an external switch utilizing interrupts using a Raspberry Pi.

Requirements

  1. Raspberry Pi
  2. External switch
  3. LED
  4. Resistor
  5. Jumper wires
  6. Breadboard

Working

When integrating an external switch with a Raspberry Pi, you can create a simple yet interactive setup that allows you to control an LED and print interrupt detection messages on the terminal while keeping track of the count. By connecting the switch to one of the Raspberry Pi's GPIO pins, you can monitor its state changes. When the switch is pressed, it triggers an interrupt, notifying the Raspberry Pi of the event. The Raspberry Pi's software can then respond to the interrupt by turning on the LED and printing a message on the terminal, indicating that an interrupt has been detected.

Circuit Diagram

Follow the circuit diagram below to connect position encoder and Raspberry Pi:

  • Connect one end of LED to GPIO18(PIN 12) of Raspberry Pi
  • Connect one side of push button to GPIO17(PIN 11) of Raspberry Pi
  • Connect other end of LED to resistor
  • Connect one end of resistor and other end of push button to GND(PIN9) Raspberry Pi

Raspberry Pi

Code

Now let's write the Python code for obtaining ECG values. Follow the steps below:

  1. Connect to your Raspberry Pi using VNC or Desktop environment.
  2. Launch the Geany Python IDE or open a text editor to write the code.
  3. Create a new file named external_switch_interrupt.py and save the following code into it
  4. You can copy and paste the following code from here into the file.
py
import RPi.GPIO as GPIO

# Set GPIO mode and setup the input pin for the switch
GPIO.setmode(GPIO.BCM)
switch_pin = 17  # GPIO pin connected to the switch
GPIO.setup(switch_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)

# Set up the output pin for the LED
led_pin = 18  # GPIO pin connected to the LED
GPIO.setup(led_pin, GPIO.OUT)
GPIO.output(led_pin, GPIO.LOW)  # Turn off the LED initially

# Define the interrupt callback function

def switch_callback(channel):
    if GPIO.input(channel) == GPIO.LOW:
        print("Switch pressed")
        GPIO.output(led_pin, GPIO.HIGH)  # Turn on the LED

    else:
        print("Switch released")
        GPIO.output(led_pin, GPIO.LOW)  # Turn off the LED

# Add interrupt event detection
GPIO.add_event_detect(switch_pin, GPIO.BOTH,
                      callback=switch_callback, bouncetime=200)

try:
    # Your main program logic can go here
    while True:
        # Perform other tasks
        pass

except KeyboardInterrupt:
    # Clean up GPIO settings on program exit
    GPIO.cleanup()
  1. After saving the file execute it by clicking on messenger like icon on geany editor or open terminal and navigate tothe folder you saved the file, execute it by typing python external_switch_interrupt.py

Output

The led switches on when the switch is pressed and led switches off when switch is released. When switch is pressed, the corresponding message will be printed in the Terminal as well.

Conclusion

By following this guide, you have successfully integrated an external switch with a Raspberry Pi 3B+ to control an LED. Using interrupts, you have learned how to detect switch presses and trigger actions accordingly. This knowledge opens up a wide range of possibilities for applications such as home automation, interactive systems, and user interfaces. With the ability to turn the LED on and off based on the switch state, you can create interactive lighting setups, button-controlled devices, or even develop custom interfaces for your Raspberry Pi projects. The integration of an external switch with a Raspberry Pi empowers you to add user-friendly input controls and enhance the interactivity of your projects. Enjoy exploring the potential of external switches in your Raspberry Pi endeavors!