raspberry pi (5) 轻触开关,倾斜开关,振动开关,干簧管传感器,U型光电传感器

  1. 轻触开关
#!/usr/bin/env python3
import RPi.GPIO as GPIO

ButtonPin = 11
Gpin   = 12
Rpin   = 13

def setup():
	GPIO.setmode(GPIO.BOARD)       # Numbers GPIOs by physical location
	GPIO.setup(Gpin, GPIO.OUT)     # Set Green Led Pin mode to output
	GPIO.setup(Rpin, GPIO.OUT)     # Set Red Led Pin mode to output

    # Set BtnPin's mode is input, and pull up to high level(3.3V)
	GPIO.setup(ButtonPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)    
	GPIO.add_event_detect(ButtonPin, GPIO.BOTH, callback=detect, bouncetime=200)

def Led(x):
	if x == 0:
		GPIO.output(Rpin, 1)
		GPIO.output(Gpin, 0)
	if x == 1:
		GPIO.output(Rpin, 0)
		GPIO.output(Gpin, 1)

def Print(x):
	if x == 0:
		print('    ***********************')
		print('    *   Button Pressed!   *')
		print('    ***********************')

def detect(chn):
	Led(GPIO.input(ButtonPin))
	Print(GPIO.input(ButtonPin))

def loop():
	while True:
		pass

def destroy():
	GPIO.output(Gpin, GPIO.HIGH)       # Green led off
	GPIO.output(Rpin, GPIO.HIGH)       # Red led off
	GPIO.cleanup()                     # Release resource

if __name__ == '__main__':
	setup()
	try:
		loop()
	except KeyboardInterrupt:  # When 'Ctrl+C' is pressed, the child program destroy() will be  executed.
		destroy()

raspberry pi (5) 轻触开关,倾斜开关,振动开关,干簧管传感器,U型光电传感器_第1张图片
2. 倾斜开关
用于检测小角度的倾斜

#!/usr/bin/env python3
import RPi.GPIO as GPIO

TiltPin = 11
Gpin   = 12
Rpin   = 13

def setup():
	GPIO.setmode(GPIO.BOARD)       # Numbers GPIOs by physical location
	GPIO.setup(Gpin, GPIO.OUT)     # Set Green Led Pin mode to output
	GPIO.setup(Rpin, GPIO.OUT)     # Set Red Led Pin mode to output

    # Set BtnPin's mode is input, and pull up to high level(3.3V)
	GPIO.setup(TiltPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
	GPIO.add_event_detect(TiltPin, GPIO.BOTH, callback=detect, bouncetime=200)

def Led(x):
	if x == 0:
		GPIO.output(Rpin, 1)
		GPIO.output(Gpin, 0)
	if x == 1:
		GPIO.output(Rpin, 0)
		GPIO.output(Gpin, 1)

def Print(x):
	if x == 0:
		print('    *************')
		print('    *   Tilt!   *')
		print('    *************')

def detect(chn):
	Led(GPIO.input(TiltPin))
	Print(GPIO.input(TiltPin))

def loop():
	while True:
		pass

def destroy():
	GPIO.output(Gpin, GPIO.HIGH)       # Green led off
	GPIO.output(Rpin, GPIO.HIGH)       # Red led off
	GPIO.cleanup()                     # Release resource

if __name__ == '__main__':     # Program start from here
	setup()
	try:
		loop()
	except KeyboardInterrupt:  # When 'Ctrl+C' is pressed, the child program destroy() will be  executed.
		destroy()

raspberry pi (5) 轻触开关,倾斜开关,振动开关,干簧管传感器,U型光电传感器_第2张图片
3. 振动开关

import RPi.GPIO as GPIO
import time

VibratePin = 11
Gpin   = 12
Rpin   = 13


def setup():
    GPIO.setmode(GPIO.BOARD)       # Numbers GPIOs by physical location
    GPIO.setup(Gpin, GPIO.OUT)     # Set Green Led Pin mode to output
    GPIO.setup(Rpin, GPIO.OUT)     # Set Red Led Pin mode to output

    # Set VibratePin's mode is input, and pull up to high level(3.3V)
    GPIO.setup(VibratePin, GPIO.IN, pull_up_down=GPIO.PUD_UP)    

def Led(x):
    if x == True:
        GPIO.output(Rpin, 1)
        GPIO.output(Gpin, 0)
    else:
        GPIO.output(Rpin, 0)
        GPIO.output(Gpin, 1)
    

def Print(x):
    if x == True:
        print('    **********')
        print('    *     ON *')
        print('    **********')
    else:
        print('    **********')
        print('    * OFF    *')
        print('    **********')

def loop():
    state = True
    while True:
        if GPIO.input(VibratePin):
            state = not state
            Led(state)
            Print(state)
            time.sleep(0.5)

def destroy():
    GPIO.output(Gpin, GPIO.HIGH)       # Green led off
    GPIO.output(Rpin, GPIO.HIGH)       # Red led off
    GPIO.cleanup()                     # Release resource

if __name__ == '__main__':     # Program start from here
    setup()
    try:
        loop()
    except KeyboardInterrupt:  # When 'Ctrl+C' is pressed, the child program destroy() will be  executed.
        destroy()

raspberry pi (5) 轻触开关,倾斜开关,振动开关,干簧管传感器,U型光电传感器_第3张图片

  1. 干簧管传感器
#!/usr/bin/env python3 
import RPi.GPIO as GPIO 

ReedPin = 11
Gpin    = 12    
Rpin    = 13    

def setup():
    GPIO.setmode(GPIO.BOARD)       # Numbers GPIOs by physical location
    GPIO.setup(Gpin, GPIO.OUT)     # Set Green Led Pin mode to output
    GPIO.setup(Rpin, GPIO.OUT)     # Set Red Led Pin mode to output

    # Set BtnPin's mode is input, and pull up to high level(3.3V)
    GPIO.setup(ReedPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)    
    GPIO.add_event_detect(ReedPin, GPIO.BOTH, callback=detect, bouncetime=200)

def Led(x): 
    if x == 0: 
        GPIO.output(Rpin, 1)
        GPIO.output(Gpin, 0)
    if x == 1: 
        GPIO.output(Rpin, 0)
        GPIO.output(Gpin, 1)

def Print(x):
    if x == 0: 
        print ('    ***********************************')
        print ('    *   Detected Magnetic Material!   *')
        print ('    ***********************************')

def detect(chn):
    Led(GPIO.input(ReedPin))
    Print(GPIO.input(ReedPin))

def loop(): 
    while True:
        pass

def destroy():
    GPIO.output(Gpin, GPIO.HIGH)       # Green led off 
    GPIO.output(Rpin, GPIO.HIGH)       # Red led off 
    GPIO.cleanup()                     # Release resource

if __name__ == '__main__':     # Program start from here
    setup() 
    try:    
        loop()
    except KeyboardInterrupt:  # When 'Ctrl+C' is pressed, the child program destroy() will be  executed.
        destroy()

raspberry pi (5) 轻触开关,倾斜开关,振动开关,干簧管传感器,U型光电传感器_第4张图片
raspberry pi (5) 轻触开关,倾斜开关,振动开关,干簧管传感器,U型光电传感器_第5张图片
5. U型光电传感器

#!/usr/bin/env python3 
import RPi.GPIO as GPIO 

PIPin  = 11
Gpin   = 12
Rpin   = 13

def setup():
    GPIO.setmode(GPIO.BOARD)       # Numbers GPIOs by physical location
    GPIO.setup(Gpin, GPIO.OUT)     # Set Green Led Pin mode to output
    GPIO.setup(Rpin, GPIO.OUT)     # Set Red Led Pin mode to output

    # Set BtnPin's mode is input, and pull up to high level(3.3V)
    GPIO.setup(PIPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)    
    GPIO.add_event_detect(PIPin, GPIO.BOTH, callback=detect, bouncetime=200)

def Led(x): 
    if x == 0: 
        GPIO.output(Rpin, 1)
        GPIO.output(Gpin, 0)
    if x == 1: 
        GPIO.output(Rpin, 0)
        GPIO.output(Gpin, 1)

def Print(x):
    if x == 1: 
        print('    *************************')
        print('    *   Light was blocked   *')
        print('    *************************')

def detect(chn):
    Led(GPIO.input(PIPin))
    Print(GPIO.input(PIPin))

def loop(): 
    while True:
        pass

def destroy():
    GPIO.output(Gpin, GPIO.HIGH)       # Green led off 
    GPIO.output(Rpin, GPIO.HIGH)       # Red led off 
    GPIO.cleanup()                     # Release resource

if __name__ == '__main__':     # Program start from here
    setup() 
    try:    
        loop()
    except KeyboardInterrupt:  # When 'Ctrl+C' is pressed, the child program destroy() will be  executed.
        destroy()

raspberry pi (5) 轻触开关,倾斜开关,振动开关,干簧管传感器,U型光电传感器_第6张图片
raspberry pi (5) 轻触开关,倾斜开关,振动开关,干簧管传感器,U型光电传感器_第7张图片

你可能感兴趣的:(python3,raspberry,pi)