Last active
September 4, 2016 11:46
-
-
Save RyosukeKamei/e0ba42187dd3f789faaf5e70a3654a66 to your computer and use it in GitHub Desktop.
Raspberry Pi 3でpythonを使いA/Dコンバーターでアナログ信号を検出する! ref: http://qiita.com/RyosukeKamei/items/387e4a1fafb1d27a220f
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 adc.py |
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 | |
# MCP3002(半固定抵抗)を接続したチャンネルを指定 | |
SPI_CH = 0 | |
# 読み込み対象のMCP3002(半固定抵抗)のアナログ入力チェンネルを指定 | |
READ_CH = 0 | |
# SPI初期化 | |
wiringpi.wiringPiSPISetup( SPI_CH, 1000000 ) | |
while True: | |
# MCP3002(半固定抵抗)に送るデータを作成 | |
buffer = 0x6800 | ( 0x1800 * READ_CH ) | |
buffer = buffer.to_bytes( 2, byteorder='big' ) | |
# SPIを使ってCH0の値を取得 | |
wiringpi.wiringPiSPIDataRW( SPI_CH, buffer ) | |
# 値が2バイトに分かれて送られるので、1つの値にまとめる | |
ch0_value = ( buffer[0] * 256 + buffer[1] ) & 0x3ff | |
# 値を電圧に変換 | |
volt = ch0_value * 3.3 / 1023 | |
# 0.5秒ごと | |
time.sleep(0.5) | |
print ("値 :" , ch0_value , " 電圧 :", volt , "V") |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment