= MAX31855 K型熱電対温度センサのスケッチ (3.3V版、5V版) = * [wiki:MAX31855 概要についてはこちら。] * [wiki:MAX31855Instruction 作り方の説明はこちら。] * キットとして販売しています。 * [http://www.switch-science.com/products/detail.php?product_id=864 3.3V版] * [http://www.switch-science.com/products/detail.php?product_id=1099 5V版] どんなスケッチ(ソフトウェア)で使うのかを説明します。[[br]] MAX31855 5V版をお使いの場合は、[#スケッチについて]から読み始めてください。 == 接続について == MAX31855 K型熱電対温度センサ 3.3V版をArduinoに接続する方法について解説します。[[BR]] すでにモジュールが完成していて、Arduino UNOと写真のように接続してあるものとします。[[BR]] 参考:/SSとSCKを1KΩと1.5KΩで抵抗分圧し5V→3.3V(3.0V)変換しています。 [[Image(wiki:MAX31855:2.JPG, 500px)]] [[Image(wiki:MAX31855:4.JPG, 500px)]] == スケッチについて == [attachment:max31855_example.zip このページの添付ファイル「max31855_example.zip」]をダウンロードして、展開します。[[BR]] 展開してできたフォルダを、Arduinoのスケッチブックのフォルダに移動します。[[BR]] スケッチブックのフォルダは、Windowsでは標準は「ドキュメント」(Vista/7/8の場合)または「マイドキュメント」(XPの場合)の中にある、「Arduino」というフォルダです。 mac OSの場合は「書類」フォルダの中に「Arduino」というフォルダがあります。 Arduino統合環境を起動して、「ファイル」メニューの「スケッチブック」から「max31855_example」を探して開いてください。[[BR]] あとは、普通にArduinoに書き込んで実行します。[[BR]] 「シリアルモニタ」を有効にすると、定期的に温度が表示されます。[[BR]] {{{ K型熱電対の温度 // 基準接点の温度(max31855の温度) }}} 「ERROR: 」と表示された場合は、熱電対がきちんとつながっていません。[[BR]] 続いて「Short to Vcc, 」と表示された場合は、Vcc側とショートしています。 「Short to GND, 」と表示された場合は、GND側とショートしています。 「Open Circuit, 」と表示された場合は、K型熱電対が接続されていないか、途中で切れています。[[BR]] K型熱電対の温度と基準接点の温度の温度が共に0.0℃の場合、モジュールが正しく接続されていない可能性があります。 スケッチの先頭に、以下の定義があります。 {{{ #!c /* // Arduinoボードの電源電圧が3.3Vの場合、 // 以下のマクロを有効にしてモジュールを直挿しできます。 #define VCC 8 #define GND 9 */ }}} モジュールをデジタルの8番~13番に直結したい場合は、これらの定義を有効にしてください。[[BR]] VCCおよびGNDの定義は、デジタルの8番および9番からモジュールに電源供給を行うためです。[[BR]] この方法が使えるのはArduinoの電源電圧が3.3Vの場合に限ります。Arduino UNOでは出来ません。[[BR]] {{{ #!c #define SLAVE 10 }}} SLAVEの定義は、SPIのスレーブセレクトにデジタルの何番を使うかを決めています。 ※このスケッチはArduinoIDE付属のSPIライブラリを使用しています。 === max31855_example.pde (Arduino 1.0付属SPIライブラリ対応版) === {{{ #!c // K型熱電対温度センサモジュールキット(SPI接続)MAX31855使用(3.3V版)サンプルスケッチ // switch-science 2012.2.8 #include "SPI.h" /* // Arduinoボードの電源電圧が3.3Vの場合、 // 以下のマクロを有効にしてモジュールを直挿しできます。 #define VCC 8 #define GND 9 */ #define SLAVE 10 byte enabled = 255; void SPI_disable() { if(enabled != 255) { digitalWrite(enabled,HIGH); enabled = 255; } } void SPI_enable(byte slaveselecter) { SPI_disable(); digitalWrite(slaveselecter,LOW); enabled = slaveselecter; } byte SPI_read() { return SPI.transfer(0x00); } void setup() { #ifdef GND pinMode(GND, OUTPUT); digitalWrite(GND, LOW); #endif #ifdef VCC pinMode(VCC, OUTPUT); digitalWrite(VCC, HIGH); #endif pinMode(SLAVE,OUTPUT); digitalWrite(SLAVE,HIGH); Serial.begin(9600); SPI.begin(); SPI.setBitOrder(MSBFIRST); SPI.setClockDivider(SPI_CLOCK_DIV4); SPI.setDataMode(SPI_MODE0); } void loop() { unsigned int thermocouple; // 14-Bit Thermocouple Temperature Data + 2-Bit unsigned int internal; // 12-Bit Internal Temperature Data + 4-Bit float disp; // display value delay(500); SPI_enable(SLAVE); thermocouple = (unsigned int)SPI_read() << 8; thermocouple |= (unsigned int)SPI_read() ; internal = (unsigned int)SPI_read() << 8; internal |= (unsigned int)SPI_read(); SPI_disable(); if((thermocouple & 0x0001) != 0) { Serial.print("ERROR: "); if ((internal & 0x0004) !=0) { Serial.print("Short to Vcc, "); } if ((internal & 0x0002) !=0) { Serial.print("Short to GND, "); } if ((internal & 0x0001) !=0) { Serial.print("Open Circuit, "); } Serial.println(""); } else { if((thermocouple & 0x8000) == 0){ // 0℃以上 disp = (thermocouple >> 2) * 0.25; } else { // 0℃未満 disp = (0x3fff - (thermocouple >> 2) + 1) * -0.25; } Serial.print(thermocouple,HEX); Serial.print(" : "); Serial.print(disp); Serial.print(" // "); if((internal & 0x8000) == 0){ // 0℃以上 disp = (internal >> 4) * 0.0625; } else { // 0℃未満 disp = (((0xffff - internal) >> 4) + 1) * -0.0625; } Serial.print(internal,HEX); Serial.print(" : "); Serial.print(disp); Serial.println(""); } } }}} ''(2009/4/5 - original text for MAX6675 kit by sgk)'' [[BR]] ''(2012/2/15 - modified for MAX31855 kit by maris)'' [[BR]] ''(2013/2/7 - modified for MAX31855 5V kit by ohki)''