Last active
September 4, 2016 11:45
-
-
Save RyosukeKamei/28af8f7933cdfd99bc9392e99621a8be to your computer and use it in GitHub Desktop.
Raspberry Pi 3でpythonを使い磁石スイッチを検出する! ref: http://qiita.com/RyosukeKamei/items/651ec3d4a235ac655827
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ git clone https://github.com/RyosukeKamei/raspberrypi3.git |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ sudo python3 hall_switch.py |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment