Up テスト 作成: 2021-03-30
更新: 2021-03-30


    配線


    テストに使用するプログラム──磁気を検知したら,LED が点灯
      磁石を近づけると LED が点灯することを確認
#!/usr/bin/python import RPi.GPIO as GPIO SignalPin = 18 LedPin = 16 def setup(): # Set the GPIO pins as numbering GPIO.setmode(GPIO.BOARD) # Set the SignalPin's mode is input, and (Detected→) ON→ LOW GPIO.setup(SignalPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Set the LedPin's mode is output GPIO.setup(LedPin,GPIO.OUT) # Set the LedPin low GPIO.output(LedPin, GPIO.LOW) def destroy(): # Release resource GPIO.cleanup() def loop(): while True: if GPIO.input(SignalPin) == GPIO.LOW: GPIO.output(LedPin,GPIO.HIGH) else: GPIO.output(LedPin,GPIO.LOW) # 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_021.py

    $ chmod +x ky_021.py

    $ ./ky_021.py