以下は,IR を検知したら LED を点灯するプログラム:
#!/usr/bin/env python
import RPi.GPIO as GPIO
TransmitterPin = 18
ReceiverPin = 16
LedPin = 22
def setup():
# Set the GPIO pins as numbering
GPIO.setmode(GPIO.BOARD)
# Set the ReceiverPin's mode is input, and (Detected→) ON→ LOW
GPIO.setup(ReceiverPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# Set the TransmitterPin's mode is output
GPIO.setup(TransmitterPin, GPIO.OUT)
# Set the TransmitterPin high(+3.3V)
GPIO.output(TransmitterPin, GPIO.HIGH)
# Set the LedPin's mode is output
GPIO.setup(LedPin, GPIO.OUT)
# Set the LedPin low
GPIO.output(LedPin, GPIO.LOW)
def loop():
while True:
if GPIO.input(ReceiverPin) == GPIO.LOW:
GPIO.output(LedPin, GPIO.HIGH)
else:
GPIO.output(LedPin, GPIO.LOW)
def destroy():
# Release resource
GPIO.cleanup()
# The Program will start from here
if __name__ == '__main__':
setup()
try:
loop()
# When control c is pressed child program destroy() will be executed.
except KeyboardInterrupt:
destroy()
|
$ vi ky_005_plus_ky_022.py
$ chmod +x ky_005_plus_ky_022.py
$ ./ky_005_plus_ky_022.py
transmitter と receiver の間が離れると,感度が悪くなる。
そんなときは,鏡を使ってテストするとよい。
|