8
7

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制御する時の初期値設定(Python)

Posted at

はじめに

Raspberry PiでGPIO制御をしていた際にハマったことと解決方法です。

環境

  • Raspberry Pi 3 Mobile B+
  • Raspbian 9.4
  • Python 3.6

ハマったこと

GPIO制御の初期化時に、INPUT or OUTPUT を選択します。
このとき、初期値はHighまたはLow のどちらかとなりますが、その初期値が望まない値になります。

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)
GPIO.setup(7, GPIO.OUT)     #初期値はHighがいいけど、Lowになっている
GPIO.setup(11, GPIO.OUT)    #初期値はLowがいいけど、Highになっている
GPIO.cleanup()

たとえば、下記のようなギャップが発生するため、動いてほしくない動作が発生する場合があります。
(プログラム実行時、最初の1回だけ余計なリモコン信号を発してしまう、など)

  • 期待する初期値: Low
  • 実際の初期値: High

解決方法

setupメソッドで初期値の設定ができました。

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)
GPIO.setup(7, GPIO.OUT, initial=GPIO.HIGH)
GPIO.setup(11, GPIO.OUT, initial=GPIO.LOW)
GPIO.cleanup()

参考

8
7
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
8
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?