小電流變大電流(繼電器)
-
- 說明
繼電器(Relay)是一種電子控制元件,它具有控制系統(又稱輸入迴路)和被控制系統(又稱輸出迴路),通常應用於自動控制電路中,我們可以透過樹莓派的針腳所提供的較小的電流去控制較大電流的外接裝置,透過「繼電器」所提供的一種「自動開關」,在電路中扮演轉換電路的角色。
- 電路
- 程式
#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ #|R|a|s|p|b|e|r|r|y|P|i|.|c|o|m|.|t|w| #+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ # Copyright (c) 2016, raspberrypi.com.tw # All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. # # Author : sosorry # Date : 06/22/2014 # Revised by Jean 02/20/2025 import RPi.GPIO as GPIO import time from pynput.keyboard import Key from pynput.keyboard import Listener GPIO.setmode(GPIO.BOARD) LED_PIN = 12 GPIO.setup(LED_PIN, GPIO.OUT) def on_press(key): print('{0} pressed'.format(key)) def on_release(key): print('{0} release'.format(key)) if key == Key.up: print("LED is on") GPIO.output(LED_PIN, GPIO.HIGH) if key == key.down: print("LED is off") GPIO.output(LED_PIN, GPIO.LOW) if key == Key.esc: # Stop listener GPIO.cleanup() return False # Collect events until released with Listener( on_press=on_press, on_release=on_release) as listener: listener.join()