- 轻触开关
import RPi.GPIO as GPIO
ButtonPin = 11
Gpin = 12
Rpin = 13
def setup():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(Gpin, GPIO.OUT)
GPIO.setup(Rpin, GPIO.OUT)
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)
GPIO.output(Rpin, GPIO.HIGH)
GPIO.cleanup()
if __name__ == '__main__':
setup()
try:
loop()
except KeyboardInterrupt:
destroy()
2. 倾斜开关
用于检测小角度的倾斜
import RPi.GPIO as GPIO
TiltPin = 11
Gpin = 12
Rpin = 13
def setup():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(Gpin, GPIO.OUT)
GPIO.setup(Rpin, GPIO.OUT)
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)
GPIO.output(Rpin, GPIO.HIGH)
GPIO.cleanup()
if __name__ == '__main__':
setup()
try:
loop()
except KeyboardInterrupt:
destroy()
3. 振动开关
import RPi.GPIO as GPIO
import time
VibratePin = 11
Gpin = 12
Rpin = 13
def setup():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(Gpin, GPIO.OUT)
GPIO.setup(Rpin, GPIO.OUT)
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)
GPIO.output(Rpin, GPIO.HIGH)
GPIO.cleanup()
if __name__ == '__main__':
setup()
try:
loop()
except KeyboardInterrupt:
destroy()
- 干簧管传感器
import RPi.GPIO as GPIO
ReedPin = 11
Gpin = 12
Rpin = 13
def setup():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(Gpin, GPIO.OUT)
GPIO.setup(Rpin, GPIO.OUT)
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)
GPIO.output(Rpin, GPIO.HIGH)
GPIO.cleanup()
if __name__ == '__main__':
setup()
try:
loop()
except KeyboardInterrupt:
destroy()
5. U型光电传感器
import RPi.GPIO as GPIO
PIPin = 11
Gpin = 12
Rpin = 13
def setup():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(Gpin, GPIO.OUT)
GPIO.setup(Rpin, GPIO.OUT)
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)
GPIO.output(Rpin, GPIO.HIGH)
GPIO.cleanup()
if __name__ == '__main__':
setup()
try:
loop()
except KeyboardInterrupt:
destroy()