今回は2本のDS18B20を並列につないで、まずは個別IDを取得してみる。
ソースはこちらにお世話になりました。
ちゃんとした結線でなくて見苦しいが、そんなにワニ口クリップなんて持っとらんわ。
ショートが怖い・・・
1個を繋いで実施するとアドレスは1個だけ表示だったが、2本つなぐと2個横連続で表示される。
①:0x28, 0x34, 0xD8, 0x75, 0xD0, 0x01, 0x3C, 0xA6
⑤:0x28, 0x01, 0x2B, 0x75, 0xD0, 0x01, 0x3C, 0x48
という結果になった。表示まではあっという間
続いて並列にしたままでID指定で個別温度を取得してみる。
DallasTemperature.hなる拡張ライブラリを追加でインストールしないといけない様だ。
これは測定精度の指定なども行えるライブラリのようだ。
参考サイト様1(北海道大学)
参考サイト様2(Quiita)
測定精度を上げる方法が書かれている。DallasTemperature::setResolution()
また、電源足2本ともGRNDへ接続する「パラサイトパワーモード」についても記載。これメーカーサイトに書いてあったなあ。
が、やってみたけど-127度になってダメでした・・設定が足りないのかな?
この方の記事ではプルアップ抵抗に5.1KΩを使っても無問題なようだ。
ライブラリ管理で「DallasTemp・・」を検索し、「MoreInfo」をクリックしたら現れる「インストール」ボタン押下
アドレス部分を書き直して書き込む。
無問題で2個のセンサーからの値がっ表示されだした。
まあ、ここまでコピペだから動いて当たり前ですが。
皆さま、ありがとうございます!
-----2個のセンサーから値を読み取る-------
#include "OneWire.h"
#include クソGooに消された
#define ONE_WIRE_BUS 8 // Pin No
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress temp0 = { 0x28, 0x34, 0xD8, 0x75, 0xD0, 0x01, 0x3C, 0xA6 }; // No.1
DeviceAddress temp1 = { 0x28, 0x01, 0x2B, 0x75, 0xD0, 0x01, 0x3C, 0x48 }; // No.5
void setup(void)
{
Serial.begin(9600);
sensors.begin();
}
void loop(void)
{
sensors.requestTemperatures();
Serial.println(sensors.getTempC(temp0));
Serial.println(sensors.getTempC(temp1));
delay(500);
}
-----個別ID取得ソース(ありがとうございます)--------
#include
OneWire ds(8); // This is where DQ of your DS18B20 will connect.
void setup(void) {
Serial.begin(9600);
getDeviceAddress();
}
void getDeviceAddress(void) {
byte i;
byte addr[8];
Serial.println("Getting the address...\n\r");
/* initiate a search for the OneWire object we created and read its value into
addr array we declared above*/
while(ds.search(addr)) {
Serial.print("The address is:\t");
//read each byte in the address array
for( i = 0; i < 8; i++) {
Serial.print("0x");
if (addr[i]
Serial.print('0');
}
// print each byte in the address array in hex format
Serial.print(addr[i], HEX);
if (i < 7) {
Serial.print(", ");
}
}
// a check to make sure that what we read is correct.
if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.print("CRC is not valid!\n");
return;
}
}
ds.reset_search();
return;
}
void loop(void) {
// do nothing
}