2.1.2 Micro Switch


In this project, we will learn how to use Micro Switch. A Micro Switch is a small, very sensitive switch which requires minimum compression to activate. Because they are reliable and sensitive, micro switches are often used as a safety device.

They are used to prevent doors from closing if something or someone is in the way and other applications similar.

Schematic Diagram

Connect the left pin of the Micro Switch to GPIO17, and two LEDs to pin GPIO22 and GPIO27 respectively. Then when you press and release the move arm of the Micro Switch, you can see the two LEDs light up alternately.

../_images/image305.png ../_images/micro_Schematic.png

Experimental Procedures

Step 1: Build the circuit.


Step 2: Get into the folder of the code.

cd /home/pi/raphael-kit/python

Step 3: Run.

sudo python3 2.1.2_MicroSwitch.py

While the code is running, press the moving arm, then the yellow LED lights up; release the moving arm, the red LED turns on.



You can Modify/Reset/Copy/Run/Stop the code below. But before that, you need to go to source code path like raphael-kit/python. After modifying the code, you can run it directly to see the effect.

#!/usr/bin/env python3

import RPi.GPIO as GPIO
import time

# Set #17 as micro switch pin, #22 as led1 pin, #27 as led2 pin
microPin = 17
led1Pin = 22
led2Pin = 27

# Define a setup function for some setup
def setup():
    # Set the GPIO modes to BCM Numbering
    # Set microPin input
    # Set ledPin output,
    # and initial level to High(3.3v)
    GPIO.setup(microPin, GPIO.IN)
    GPIO.setup(led1Pin, GPIO.OUT, initial=GPIO.HIGH)
    GPIO.setup(led2Pin, GPIO.OUT, initial=GPIO.HIGH)

# Define a main function for main process
def main():
    while True:
        # micro switch high, led1 on
        if GPIO.input(microPin) == 1:
            print ('LED1 ON')
            GPIO.output(led1Pin, GPIO.LOW)
            GPIO.output(led2Pin, GPIO.HIGH)

        # micro switch low, led2 on
        if GPIO.input(microPin) == 0:
            print ('    LED2 ON')
            GPIO.output(led2Pin, GPIO.LOW)
            GPIO.output(led1Pin, GPIO.HIGH)

# Define a destroy function for clean up everything after
# the script finished
def destroy():
    # Turn off LED
    GPIO.output(led1Pin, GPIO.HIGH)
    GPIO.output(led2Pin, GPIO.HIGH)
    # Release resource

# If run this script directly, do:
if __name__ == '__main__':
    # When 'Ctrl+C' is pressed, the program
    # destroy() will be  executed.
    except KeyboardInterrupt:

Code Explanation

if GPIO.input(slidePin) == 1:
            GPIO.output(led1Pin, GPIO.LOW)
            GPIO.output(led2Pin, GPIO.HIGH)

When the moving arm of the micro switch is released, the left pin is connected to the right pin; at this time, a high level will be read on GPIO17, and then LED1 will be on and LED2 will be off.

if GPIO.input(slidePin) == 0:
            GPIO.output(led2Pin, GPIO.LOW)
            GPIO.output(led1Pin, GPIO.HIGH)

When the move arm is pressed, the left pin and the middle pin are connected. At this point a low level will be read on GPIO17, then turns LED2 on and LED1 off.

Phenomenon Picture