Skip to content

Instantly share code, notes, and snippets.

@RyosukeKamei
Last active September 4, 2016 11:46
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/3f4572ba7c21e8432e8c5d23bc675d4b to your computer and use it in GitHub Desktop.
Save RyosukeKamei/3f4572ba7c21e8432e8c5d23bc675d4b to your computer and use it in GitHub Desktop.
Raspberry Pi 3でpythonを使い「温度(A/Dコンバーターを利用)」を検出する! ref: http://qiita.com/RyosukeKamei/items/5b5c50835ccfc374dcea
$ git clone https://github.com/RyosukeKamei/raspberrypi3.git
# GPIOを制御するライブラリ
import wiringpi
# タイマーのライブラリ
import time
# MCP3002(A/Dコンバータ)を接続したチャンネルを指定
SPI_CH = 0
# 読み込み対象のMCP3002(A/Dコンバータ)のアナログ入力チェンネルを指定
READ_CH = 0
# SPI初期化
wiringpi.wiringPiSPISetup( SPI_CH, 1000000 )
while True:
# MCP3002(A/Dコンバータ)に送るデータを作成
buffer = 0x6800 | ( 0x1800 * READ_CH )
buffer = buffer.to_bytes( 2, byteorder='big' )
# SPIを使ってCH0の値を取得
wiringpi.wiringPiSPIDataRW( SPI_CH, buffer )
# 値が2バイトに分かれて送られるので、1つの値にまとめる
input_value = ( buffer[0] * 256 + buffer[1] ) & 0x3ff
# 入力値を電圧に変換
volt = input_value * 3.3 / 1023
# 取得した電圧に100をかけると温度が求められる
temperature = volt * 100
# 表示
print ("温度 : ", temperature, " 電圧 : ", volt)
# 1秒ずつ検出
time.sleep(1)
$ sudo python3 temperature_sensor.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment