Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
21
Help us understand the problem. What are the problem?

More than 3 years have passed since last update.

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

ラズパイ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

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
21
Help us understand the problem. What are the problem?