0
1

AtomS3 LiteとDS18B20で温度測定できずにハマった

Last updated at Posted at 2024-07-28

はじめに

WiFi接続で外の温度を測るためにAtomS3 Lite + DS18B20で測定ノードを組んだが難航中。
色々ソフトをひねくり返してM5.beginが原因のようだと判断しました。
なんとか動きました。

ハードの説明

AtomS3 Lite
DS18B20
ATOMICプロトキット
 温度センサDS18B20 DQ端子をプルアップ抵抗4.7kΩを内蔵して3.3Vに接続
 5V,GNDを引き出してニッケル水素電池×4本で駆動
 部品故障時はVH-3.96コネクタの端子ネジを緩めて交換、ハンダいらず
EXT.IO2
 I2C接続で8チャネル入出力に拡張

IMG_9072.JPG

ソフトの説明

 Arudino IDE 2.3.2
 M5AtomS3 1.0.0
 DallasTemperature 3.9.0
 M5Unified 0.1.16
 M5Unit-EXTIO2 1.0.3

サンプルプログラム

Arduino+DS18B20で温度測定してみたを参考にしています。
GPIOはG5です

#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into port 5 on the Arduino
#define ONE_WIRE_BUS 5

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

void setup(void)
{
  // start serial port
  Serial.begin(9600);
  Serial.println("Dallas Temperature IC Control Library Demo");

  // Start up the library
  sensors.begin();
}

void loop(void)
{ 
  // call sensors.requestTemperatures() to issue a global temperature 
  // request to all devices on the bus
  Serial.print("Requesting temperatures...");
  sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.println("DONE");

  Serial.print("Temperature for the device 1 (index 0) is: ");
  Serial.println(sensors.getTempCByIndex(0));

  delay(500);
}

正常な出力です

Temperature for the device 1 (index 0) is: 29.50
Requesting temperatures...DONE
Temperature for the device 1 (index 0) is: 29.50
Requesting temperatures...DONE
Temperature for the device 1 (index 0) is: 29.50
Requesting temperatures...DONE
Temperature for the device 1 (index 0) is: 29.50
Requesting temperatures...DONE

これにM5.unified.hをつけてM5.beginを実行すると

#include <M5Unified.h>
#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into port 5 on the Arduino
#define ONE_WIRE_BUS 5

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

void setup(void)
{
  auto cfg = M5.config(); // 設定用の構造体を代入。
  cfg.serial_baudrate = 9600;
  M5.begin(cfg);
  // start serial port
  Serial.println("Dallas Temperature IC Control Library Demo");

  // Start up the library
  sensors.begin();
}

void loop(void)
{ 
  // call sensors.requestTemperatures() to issue a global temperature 
  // request to all devices on the bus
  Serial.print("Requesting temperatures...");
  sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.println("DONE");

  Serial.print("Temperature for the device 1 (index 0) is: ");
  Serial.println(sensors.getTempCByIndex(0));

  delay(500);
}

-127.00℃ ぇぇぇぇなんで

Requesting temperatures...res...DONE
Temperature for the device 1 (index 0) is: -127.00
Requesting temperatures...DONE
Temperature for the device 1 (index 0) is: -127.00
Requesting temperatures...DONE
Temperature for the device 1 (index 0) is: -127.00
Requesting temperatures...DONE
Temperature for the device 1 (index 0) is: -127.00

AtomS3の内臓スイッチも使いたいのでコレ困る。
M5Unifiedの完成度かと考えて#include AtomS3.hに置き換えても-127.00℃。
ハードの初期化がらみかな。
しばらく仕様書読んで試行錯誤です。

追記2024/07/31 初期化コードを追加して動いた

ののやま様がM5stack用に書かれたコードをよく見て・・・
pinMode(ONE_WIRE_BUS,INPUT);
sensors.begin();
の順番で初期化

#include <M5Unified.h>
#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into port 5 on the Arduino
#define ONE_WIRE_BUS 5

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

void setup(void)
{
  auto cfg = M5.config(); // 設定用の構造体を代入。
  cfg.serial_baudrate = 9600;
  M5.begin(cfg);
  // start serial port
  Serial.println("Dallas Temperature IC Control Library Demo");
  pinMode(ONE_WIRE_BUS,INPUT); //【農系IoT】より初期化コード追加

  // Start up the library
  sensors.begin();
}

void loop(void)
{ 
  // call sensors.requestTemperatures() to issue a global temperature 
  // request to all devices on the bus
  Serial.print("Requesting temperatures...");
  sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.println("DONE");

  Serial.print("Temperature for the device 1 (index 0) is: ");
  Serial.println(sensors.getTempCByIndex(0));

  delay(500);
}

動いた。長かった。

Temperature for the device 1 (index 0) is: 29.50
Requesting temperatures...DONE
Temperature for the device 1 (index 0) is: 29.50
Requesting temperatures...DONE
Temperature for the device 1 (index 0) is: 29.50
Requesting temperatures...DONE
Temperature for the device 1 (index 0) is: 29.50

DallasTemperatureのサイトに「動かないときは各処理系で聞いてね(意訳)」とあったのはこういうことか。本番プログラムに移植してWiFi飛ばします。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1