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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/guzhou_diaoke/article/details/88983397
  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()

在这里插入图片描述
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()

在这里插入图片描述
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()

在这里插入图片描述

  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()

在这里插入图片描述
在这里插入图片描述
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()

在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/guzhou_diaoke/article/details/88983397
今日推荐