= MAX31855 K型熱電対温度センサのスケッチ (3.3V版、5V版) = * キットの概要はこちら。 [wiki:MAX31855 3.3V版] [wiki:MAX31855-5V 5V版] * 作り方の説明はこちら。 [wiki:MAX6675_MAX31855_Instruction MAX6675,MAX31855 3.3V版/5V版 共通] * キットとして販売しています。 [http://www.switch-science.com/catalog/864/ 3.3V版] [http://www.switch-science.com/catalog/1099/ 5V版] どんなスケッチ(ソフトウェア)で使うのかを説明します。[[br]] == 接続について == MAX31855 K型熱電対温度センサの電源電圧とArduinoの電源電圧が同じで、SPIの信号がセンサの基板と同じに並んでいれば、Arduinoに直接挿し込んで使うことが出来ます。[[BR]] 5V版はArduino Uno([http://www.switch-science.com/catalog/428 初代/R2]/[http://www.switch-science.com/catalog/789/ R3])や[http://www.switch-science.com/catalog/3/ Arduino Duemilanove]等、[[BR]] 3.3V版は[http://www.switch-science.com/catalog/849/ Arduino Pro 328 - 3.3V/8MHz]等に直接挿し込めます[[BR]] Arduino Leonardoや Arduino Mega等は写真の場所にSPIの信号が出ていないので、ジャンパワイヤやブレッドボードなどで配線してください。 [[Image(MAX31855_5V_on_Uno.jpg, 500px)]] [[BR]] 写真のArduino Unoは[http://www.switch-science.com/catalog/511/ Arduino Uno SMD](初代)です。もちろん[http://www.switch-science.com/catalog/1073/ R3]や普通のArduino Unoでも5V版がそのまま挿せます。 == スケッチについて == [attachment:max31855_example2.zip このページの添付ファイル「max31855_example2.zip」]をダウンロードして、展開します。[[BR]] 展開してできたフォルダを、Arduinoのスケッチブックのフォルダに移動します。[[BR]] スケッチブックのフォルダは、Windowsでは標準は「ドキュメント」(Vista/7/8の場合)または「マイドキュメント」(XPの場合)の中にある、「Arduino」というフォルダです。 mac OSの場合は「書類」フォルダの中に「Arduino」というフォルダがあります。 Arduino統合環境を起動して、「ファイル」メニューの「スケッチブック」から「max31855_example2」を探して開いてください。[[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ボードの電源電圧がキットの電源電圧と同じ場合、 // 以下のマクロを有効にしてモジュールを直挿しできます。 // If working voltage of your arduino board is same as the kit, // you can enable macro code next two lines // to put the kit on your arduino directly. /* #define VCC 8 #define GND 9 */ }}} 上の写真の様にモジュールをデジタルの8番~13番に直結したい場合は、これらの定義を有効にしてください。[[BR]] VCCおよびGNDの定義は、デジタルの8番および9番からモジュールに電源供給を行うためです。[[BR]] {{{ #!c #define SLAVE 10 }}} SLAVEの定義は、SPIのスレーブセレクトにデジタルの何番を使うかを決めています。 ※このスケッチはArduinoIDE付属のSPIライブラリを使用しています。 === max31855_example2.ino (Arduino 1.0付属SPIライブラリ対応版 2) === {{{ #!c // K型熱電対温度センサモジュールキット(SPI接続)MAX31855使用(5V版/3.3V版)サンプルスケッチ // Example sketch for MAX31855 Type-K thermocouple sensor module (5V/3.3V) // Switch-Science 2013.5.14(Tue) #include "SPI.h" // Arduinoボードの電源電圧がキットの電源電圧と同じ場合、 // 以下のマクロを有効にしてモジュールを直挿しできます。 // If working voltage of your arduino board is same as the kit, // you can enable macro code next two lines // to put the kit on your arduino directly. /* #define VCC 8 #define GND 9 */ #define SLAVE 10 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); digitalWrite(SLAVE, LOW); // Enable the chip thermocouple = (unsigned int)SPI.transfer(0x00) << 8; // Read high byte thermocouple thermocouple |= (unsigned int)SPI.transfer(0x00); // Read low byte thermocouple internal = (unsigned int)SPI.transfer(0x00) << 8; // Read high byte internal internal |= (unsigned int)SPI.transfer(0x00); // Read low byte internal digitalWrite(SLAVE, HIGH); // Disable the chip 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℃以上 above 0 Degrees Celsius disp = (thermocouple >> 2) * 0.25; } else { // 0℃未満 below zero disp = (0x3fff - (thermocouple >> 2) + 1) * -0.25; } Serial.print(thermocouple,HEX); Serial.print(" : "); Serial.print(disp); Serial.print(" // "); if((internal & 0x8000) == 0){ // 0℃以上 above 0 Degrees Celsius disp = (internal >> 4) * 0.0625; } else { // 0℃未満 below zero 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)'' [[BR]] ''(2013/5/14 - modified for MAX31855 kit by maris)'' [[BR]]