Raspberry Pi-硬體的LED Hello World

點亮LED幾乎已經是硬體入門的Hello World.

需要材料

  • 發光二極體
    LED
  • 1K 歐姆電阻
  • 連接線

線路圖

  • 接地(黑色)接到 Pi 上的 pin 6
  • 電源(紅色)接到 Pi 上的 pin 12
    Led helloworld
    外觀:
    LED Hello World

透過Python來控制

  • LED 開與關
#!/user/bin/python

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)

LED_PIN=12

GPIO.setup(LED_PIN, GPIO.OUT)

print("LED is on")
GPIO.output(LED_PIN, GPIO.HIGH)
time.sleep(20)
print("LED is off")
GPIO.output(LED_PIN, GPIO.LOW)
time.sleep(3)
GPIO.cleanup()
  • 閃爍 LED
#!/user/bin/python

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)

LED_PIN=12

GPIO.setup(LED_PIN, GPIO.OUT)

try:
   while True:
      GPIO.output(LED_PIN, GPIO.HIGH)
      time.sleep(1)

      GPIO.output(LED_PIN, GPIO.LOW)
      time.sleep(1)
except KeyboardInterrupt:
   print("End")
finally:
   GPIO.cleanup()