1wireサーマルセンサー(DS18B20) 2本をarduino nanoのD2ピンに接続 – connect two 1wire thermal sensors (DS18B20) into arduino nano’s D2 pin. –


// arduino nanoに1wire DS18B20 2本をD2に接続。
// arduino 1wireシリアル出力
#include "OneWire.h"
// 定義
#define MAX_DS1820_SENSORS 2
#define ONEWIRE_IOPORT 2
// グローバル変数
OneWire ds(ONEWIRE_IOPORT);
byte addr[MAX_DS1820_SENSORS][8];
byte sensor;
int HighByte, LowByte, TReading, SignBit, Tc_100, Whole, Fract;
char buf[20];
//
// セットアップ関数
//
void setup(void)
{
  // シリアルポート初期化
  Serial.begin(9600);
  // LED初期化
  pinMode(13, OUTPUT); //LED on Model B
  // 1wire温度センサー検索
  ds.reset_search();
  for (sensor = 0; sensor < MAX_DS1820_SENSORS; sensor++)
  {
    if (!ds.search(addr[sensor]))
    {
      Serial.print("_T,-3,,,Search failed.\n");
      ds.reset_search();
      return;
    }
  }
}
//
// ループ関数
//
void loop(void)
{
  byte i;
  byte present = 0;
  byte data[12];
  int type;
  for (sensor = 0; sensor < MAX_DS1820_SENSORS; sensor++)
  {
    if (OneWire::crc8(addr[sensor], 7) != addr[sensor][7])
    {
      Serial.print("_T,-1,,,CRC is not valid!\n");
      return;
    }
    if (addr[sensor][0] == 0x10)
    {
      type = 0;
    }
    else if (addr[sensor][0] == 0x28)
    {
      type = 1;
    }
    else
    {
      Serial.print("_T,-2,,,Device family is not recognized: 0x");
      for (i = 0; i < 8; i++)
      {
        Serial.print(addr[sensor][i], HEX);
      }
      Serial.print("\n");
      return;
    }
    ds.reset();
    ds.select(addr[sensor]);
    ds.write(0x44, 1);      // start conversion, with parasite power on at the end
    digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
    delay(1000);            // wait for a second
    digitalWrite(13, LOW);  // turn the LED off by making the voltage LOW
    delay(1000);
    Serial.print("_T,");
    Serial.print(type);
    Serial.print(",");
    for (i = 0; i < 8; i++)
    {
      Serial.print(addr[sensor][i], HEX);
    }
    Serial.print(",");
    // we might do a ds.depower() here, but the reset will take care of it.
    present = ds.reset();
    ds.select(addr[sensor]);
    ds.write(0xBE); // Read Scratchpad
    // センサーデータを温度に変換
    for (i = 0; i < 9; i++)
    { // we need 9 bytes
      data[i] = ds.read();
    }
    LowByte = data[0];
    HighByte = data[1];
    TReading = (HighByte << 8) + LowByte;
    SignBit = TReading & 0x8000; // test most sig bit
    if (SignBit)                 // negative
    {
      TReading = (TReading ^ 0xffff) + 1; // 2's comp
    }
    Tc_100 = (6 * TReading) + TReading / 4; // multiply by (100 * 0.0625) or 6.25
    Whole = Tc_100 / 100;                   // separate off the whole and fractional portions
    Fract = Tc_100 % 100;
    if (SignBit) // If its negative
    {
      Serial.print("-");
    }
    Serial.print(Whole);
    Serial.print(".");
    if (Fract < 10)
    {
      Serial.print("0");
    }
    Serial.print(Fract);
    Serial.print(",degree");
    Serial.print("\n");
    delay(1000);
  }
}

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です