Skip to content

Instantly share code, notes, and snippets.

@RyosukeKamei
Last active September 4, 2016 11:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RyosukeKamei/28af8f7933cdfd99bc9392e99621a8be to your computer and use it in GitHub Desktop.
Save RyosukeKamei/28af8f7933cdfd99bc9392e99621a8be to your computer and use it in GitHub Desktop.
Raspberry Pi 3でpythonを使い磁石スイッチを検出する! ref: http://qiita.com/RyosukeKamei/items/651ec3d4a235ac655827
$ git clone https://github.com/RyosukeKamei/raspberrypi3.git
# GPIOを制御するライブラリ
import wiringpi
# タイマーのライブラリ
import time
# GPIO端子の設定
hall_switch_pin = 17
# GPIO出力モードを1に設定する
wiringpi.wiringPiSetupGpio()
# GPIOを入力モード(0)にする
wiringpi.pinMode(hall_switch_pin, 0)
# 入力はプルアップに設定
wiringpi.pullUpDnControl(hall_switch_pin, 2)
while True:
# GPIO端子の状態を読み込み
# S極 : 0
# N極 : 1
if( wiringpi.digitalRead(hall_switch_pin) == 1 ):
# 入力が0の時がS極
print ("South Pole")
else:
print ("North Pole")
# 1秒ごとに検出# 出力が1の時がN極
time.sleep(1)
$ sudo python3 hall_switch.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment