19
24

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Raspberry PiのGPIOを使って電子工作を楽しむ

Last updated at Posted at 2017-08-15

ラズパイ3ではGPIOをpythonから操作するライブラリが標準で入っている

  1. 準備:import RPi.GPIO as GPIO
  2. モードの指定をする:GPIO.setmode(GPIO.BOARD)
    • GPIO.BOARD: 物理ピン番号(左上からの連番)
    • GPIO.BCM: 役割ピン番号(broadcomが命名しているもの)
  3. ピンの設定:GPIO.setup(channel, GPIO.out)
    • GPIO.in: 入力
    • GPIO.out: 出力
  4. ピンの操作:
    • GPIO.output(channel, GPIO.HIGH)
    • GPIO.output(channel, GPIO.LOW)
    • val = GPIO.input(channel)
スクリーンショット 2017-07-15 17.41.36.png

例1: LEDチカチカ

led-blink.py
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.OUT)

while True:
    GPIO.output(11, True)
    time.sleep(2)
    GPIO.output(11, False)
    time.sleep(2)

例2: ボタン入力を検出

led-blink.py
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)

GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)

while True:
    input_state = GPIO.input(18)
    if input_state == False:
        print('Button Pressed')
        time.sleep(0.2)
スクリーンショット 2017-07-15 17.41.36.png
19
24
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
24

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?