以前から自宅のプランターの水分管理に,M5 Atom Liteという小型マイコン(大きさ24x24mmしかありません)と水分センサ・ポンプを組み合わせた自動水やりシステムを自作して使っていました(阿部のプロフィールページの終わりにその写真をこっそり掲載しています)。その際,センサ値の変化を遠隔で記録するためにAmbientというIoTデータ可視化サービスを使っていました。
今回はそのしくみを応用して,研究室の居室・実験室の室温や実験魚の飼育水槽の水温をモニタリングするシステムを組んでみました(講義資料や申請書書きなどに飽きてよそ事してたら興に乗ってしまったのではありません。たぶん。。。)。
M5Atom Liteと温度センサDS18B20を使えば、比較的簡単に温度データを取得し、Ambient上でグラフ化することができます。研究室や自宅の環境モニタリングを手軽に始めたい人には、かなりおすすめです。
必要な材料
- M5Atom Lite
- スイッチサイエンスでは1,771円(税込・送料別(200円)https://www.switch-science.com/products/6262?srsltid=AfmBOoq3fDP10IGEEoqj5ovoq2aAnYnd-4dG2THZB7yrMY_JGo66faCD
- Amazonでは2,580円 https://amzn.asia/d/0em45j2N
- DS18B20
- Amazonでは3本990円から5本1200〜3,500円で中華製の防水プローブとして売られている。テスト用・空気中用であればやすいのでも良いですが,水温測定用にはある程度評価・価格が高いものの方が安心
- USB-CケーブルとAC充電アダプタ
- 100円ショップで購入したもので充分利用可能
- 絶縁テープ
- 端子に水がかからないように,抜けないようにするために利用
- PC(Win・MacどちらでもOK)
- M5Atom Liteへのプログラム書き込みに使用します
準備(M5 Atom Lite側)
1. Arduino IDEをインストールする
使用しているPCにArduino IDEをダウンロードし、インストールします。
Arduino IDEは以下のURLからダウンロードできます。
https://docs.arduino.cc/software/ide
2. M5Stack用のボード情報を追加する
Arduino IDEを起動し、Preferences を開きます。Additional boards manager URLs に以下のURLを追加します。
https://static-cdn.m5stack.com/resource/arduino/package_m5stack_index.json

英語表示が使いにくい場合は、Language を日本語に変更しておくと作業しやすくなります。
3. M5Stackボードをインストールする
Board Manager を開き、M5Stack を検索してインストールします。

4. 必要なライブラリをインストールする
Library Manager を開き、以下のライブラリをインストールします。
- OneWire
- DallasTemperature
- Ambient

5. DS18B20をM5Atom Liteに接続する
DS18B20から伸びている3本のケーブルを、M5Atom Liteに接続します。
今回使用したセンサでは、以下のように接続しました。
- 黄色のケーブル:M5Atom LiteのG22端子
- 赤色のケーブル:3.3V端子
- 黒色のケーブル:GND端子


ただし、DS18B20の製品によってケーブルの色が異なる場合があります。必ず購入したセンサの仕様を確認してください。
接続後、むき出しの端子同士が接触しないように、必要に応じて絶縁テープなどで保護しておきます。
6. M5Atom LiteをPCに接続する
M5Atom LiteをUSB-CケーブルでPCに接続します。
Arduino IDEで、以下を設定します。
ツール > ポートから、M5Atom Liteを接続したポートを選択します(WinとMacで表記が異なるので詳細省略)。ツール > ボード > M5Stack > M5Atomを選択します。
これで、Arduino IDEからM5Atom Liteにプログラムを書き込める状態になります。
7. プログラムを書き込む
Arduino IDEに以下のプログラムを入力します。
#include <WiFi.h>#include <OneWire.h>#include <DallasTemperature.h>#include <Ambient.h>// 以下の4行についてそれぞれの環境に合わせて変更するconst char* WIFI_SSID = "SSID";const char* WIFI_PASSWORD = "パスワード";const unsigned int AMBIENT_CHANNEL_ID = 1234567;const char* AMBIENT_WRITE_KEY = "ライトキー";// Ambientでは複数のAtomからのセンサーデータを受け付けて同時にチャートを描くことができる。そのための機体番号を指定する// Atom 1: 1 sends to Ambient d1// Atom 2: 2 sends to Ambient d2// Atom 3: 3 sends to Ambient d3constexpr uint8_t AMBIENT_DATA_FIELD = 2;// DS18B20 signal line (黄色のライン)を M5Atom LiteのGPIO22端子に接続する.// 赤のラインを3.3V端子に,黒のラインをGND端子に接続するconstexpr uint8_t ONE_WIRE_PIN = 22;constexpr unsigned long SEND_INTERVAL_MS = 60UL * 1000UL;// 初期設定宣言WiFiClient client;Ambient ambient;OneWire oneWire(ONE_WIRE_PIN);DallasTemperature sensors(&oneWire);unsigned long lastSendAt = 0;// 温度センサー(DS18B20)のセンサーIDを取得・シリアルモニタで表示(動作試験用)void printSensorAddress(const DeviceAddress address) { for (uint8_t i = 0; i < 8; i++) { if (address[i] < 16) { Serial.print("0"); } Serial.print(address[i], HEX); }}// WIFI接続をテストし,IPアドレスを表示void connectWiFi() { if (WiFi.status() == WL_CONNECTED) { return; } Serial.print("WiFi connecting"); WiFi.mode(WIFI_STA); WiFi.begin(WIFI_SSID, WIFI_PASSWORD); while (WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(500); } Serial.print("\nWiFi connected: "); Serial.println(WiFi.localIP());}// 温度読み取りbool readTemperature(float& temperatureC) { sensors.requestTemperatures(); temperatureC = sensors.getTempCByIndex(0); if (temperatureC == DEVICE_DISCONNECTED_C || temperatureC < -55.0f || temperatureC > 125.0f) { return false; } return true;}//電源投入後の初期設定void setup() { Serial.begin(115200); delay(500); Serial.println("\nM5Atom Lite DS18B20 Ambient monitor"); Serial.print("OneWire pin: GPIO"); Serial.println(ONE_WIRE_PIN); Serial.print("Ambient data field: d"); Serial.println(AMBIENT_DATA_FIELD); // Uses ESP32's internal pull-up instead of an external resistor. // Keep wiring short and power the DS18B20 with VDD/GND; parasite power is not suitable here. pinMode(ONE_WIRE_PIN, INPUT_PULLUP); sensors.begin(); sensors.setResolution(12); const int deviceCount = sensors.getDeviceCount(); Serial.print("DS18B20 devices found: "); Serial.println(deviceCount); if (deviceCount > 0) { DeviceAddress address; if (sensors.getAddress(address, 0)) { Serial.print("First sensor address: "); printSensorAddress(address); Serial.println(); } Serial.print("Parasite power: "); Serial.println(sensors.isParasitePowerMode() ? "ON" : "OFF"); } connectWiFi(); ambient.begin(AMBIENT_CHANNEL_ID, AMBIENT_WRITE_KEY, &client);}// 測定・送信ループ(電源を切るまで以下の動作を実行し続ける)void loop() { connectWiFi(); const unsigned long now = millis(); if (lastSendAt != 0 && now - lastSendAt < SEND_INTERVAL_MS) { delay(100); return; } lastSendAt = now; float temperatureC = NAN; if (!readTemperature(temperatureC)) { Serial.println("DS18B20 read failed"); return; } Serial.print("Temperature: "); Serial.print(temperatureC, 2); Serial.println(" C"); ambient.set(AMBIENT_DATA_FIELD, static_cast<double>(temperatureC)); if (ambient.send()) { Serial.println("Sent to Ambient"); } else { Serial.print("Ambient send failed, status="); Serial.println(ambient.status); }}
プログラムを入力したら、以下の順で操作します。
スケッチ > 検証・コンパイルでコンパイルするスケッチ > 書き込みでM5Atom Liteに書き込むツール > シリアルモニタを開く

ここまでの手順がうまくいっていれば、シリアルモニタに以下のような表示が出ます。
M5Atom Lite DS18B20 Ambient monitorOneWire pin: GPIO22Ambient data field: d1DS18B20 devices found: 1First sensor address: 使用しているセンサ固有のIDParasite power: OFFWiFi connecting….WiFi connected: 割り当てられたIPアドレスTemperature: 24.69 CSent to Ambient
この時点では、まだAmbient側の設定が完了していないため、Ambientへの送信部分でエラーが出る場合があります。センサが認識され、温度が表示されていれば、ひとまずM5Atom Lite側の基本設定はできています。
Arduino IDEは開いたままにしておき、次にWebブラウザでAmbientの設定を行います。
準備:Ambient側
1. Ambientに登録する
AmbientのWebサイトにアクセスし、ユーザ登録を行います。

画面の指示に従って、必要事項を入力します。

2. チャネルを作成する
Ambientにログインしたら、チャネルを作る ボタンをクリックします。
チャネル一覧に新しいチャネルが作成されます。
3. チャネルIDとライトキーを確認する
作成されたチャネルに表示されている以下の情報を確認します。
- チャネルID
- ライトキー
これらを、Arduino IDEで開いているプログラム中の該当箇所にコピーします。

4. プログラムを再度書き込む
チャネルIDとライトキーを入力したら、再度プログラムをコンパイルし、M5Atom Liteに書き込みます。
書き込み後、シリアルモニタを確認します。
問題なく動作していれば、温度の取得とAmbientへの送信が行われ、以下のような表示が出ます。
Temperature: 24.69 C
Sent to Ambient
5. Ambientのチャネル設定を変更する
Ambientの設定ページに戻ります。
チャネル一覧の「設定」列に表示されている▼をクリックし、設定変更 を選択します。
必要に応じて、以下の項目を設定します。
- チャネル名
- 説明
- データ項目名
たとえば、d1 に温度データを送信している場合は、データ項目名を「室温」や「水温」などに変更しておくと、後から見たときにわかりやすくなります。

6. チャートを確認する
チャネル名をクリックすると、チャート画面が表示されます。

グラフタイトルをクリックすると、チャートの表示設定を変更できます。


ここまで設定すれば、M5Atom Liteで取得した温度データをAmbient上でグラフとして確認できるようになります。
これで、自分のPCやスマートフォンのWebブラウザから、設定したセンサの温度変化を遠隔で確認できます。
応用
M5Atom LiteとDS18B20を複数用意すれば、複数の場所の温度を同時に記録できます。
たとえば、以下のような使い方ができます。
- 研究室の居室の室温を記録する
- 実験室の室温を記録する
- 飼育水槽の水温を記録する
- 恒温室やインキュベータ周辺の温度変化を記録する
Ambient側の設定を変更すれば、複数のセンサから送信された温度データを、同じチャート上に重ね書きすることも、別々のチャートとして表示することもできます。
また、ノイズが多いデータの場合は、一定時間ごとの平均値を表示することで、より見やすいチャートにすることもできます。
Ambient上に蓄積されたデータは、CSV形式でダウンロードすることもできます。そのため、後からExcelやRなどで解析することも可能です。
さらに、チャネルを公開設定にすれば、Ambientの埋め込み用HTMLを自分のWebページに貼り付けることで、Ambientにログインしなくても温度変化のグラフを表示できます。
詳しい設定方法については、Ambientのチュートリアルを参照してください。
Building a Temperature Monitoring System with the Compact M5Atom Lite Microcontroller and DS18B20 Temperature Sensor
I had previously built and used an automatic watering system for managing soil moisture in planters at home. The system combined a compact microcontroller called the M5Atom Lite, which is only 24 × 24 mm in size, with a moisture sensor and a pump. I also used Ambient, an IoT data visualization service, to remotely record changes in sensor values.
This time, I applied the same basic setup to build a system for monitoring room temperature in the laboratory and office, as well as water temperature in experimental fish tanks. This was absolutely not because I got tired of preparing lecture materials or writing grant applications and became overly absorbed in a side project. Probably.
Using an M5Atom Lite and a DS18B20 temperature sensor, it is relatively easy to acquire temperature data and visualize it as graphs on Ambient. I highly recommend this setup for anyone who wants to start simple environmental monitoring in a laboratory or at home.
Materials Needed
- M5Atom Lite
- Available from Switch Science for 1,771 yen, including tax but excluding shipping
- Available on Amazon for 2,580 yen
- DS18B20
- On Amazon, waterproof Chinese-made probes are sold for around 990 yen for three pieces, or 1,200–3,500 yen for five pieces. Cheaper ones are probably fine for testing or measuring air temperature, but for measuring water temperature, I would feel more comfortable using a somewhat better-reviewed and slightly more expensive product.
- USB-C cable and AC power adapter
- Items purchased from a 100-yen shop are sufficient.
- Insulating tape
- Used to prevent water from contacting the terminals and to keep the connections from coming loose.
- PC, either Windows or Mac
- Used to write the program to the M5Atom Lite.
Preparation: M5Atom Lite
1. Install the Arduino IDE
Download and install the Arduino IDE on your PC.
The Arduino IDE can be downloaded from the Arduino website.
2. Add the Board Information for M5Stack
Launch the Arduino IDE and open Preferences.
Add the following URL to Additional boards manager URLs.
If the English interface is difficult to use, changing the Language setting to Japanese may make the process easier.
3. Install the M5Stack Board Package
Open Board Manager, search for M5Stack, and install it.
4. Install the Required Libraries
Open Library Manager and install the following libraries.
- OneWire
- DallasTemperature
- Ambient
5. Connect the DS18B20 to the M5Atom Lite
Connect the three wires extending from the DS18B20 to the M5Atom Lite.
For the sensor used here, the connections were as follows.
- Yellow wire: G22 terminal on the M5Atom Lite
- Red wire: 3.3V terminal
- Black wire: GND terminal
However, the wire colors may differ depending on the DS18B20 product. Be sure to check the specifications of the sensor you purchased.
After connecting the wires, protect the exposed terminals with insulating tape or similar material as needed, so that the terminals do not touch each other.
6. Connect the M5Atom Lite to the PC
Connect the M5Atom Lite to your PC using a USB-C cable.
In the Arduino IDE, configure the following settings.
- From
Tools > Port, select the port to which the M5Atom Lite is connected. The displayed port name differs between Windows and Mac, so the details are omitted here. - From
Tools > Board > M5Stack, selectM5Atom.
You are now ready to write a program to the M5Atom Lite from the Arduino IDE.
7. Write the Program
Enter the program in the Arduino IDE (see above).
After entering the program, proceed as follows.
- Compile the program using
Sketch > Verify/Compile. - Write the program to the M5Atom Lite using
Sketch > Upload. - Open
Tools > Serial Monitor.
If everything has worked correctly up to this point, the serial monitor should display something like the following.
M5Atom Lite DS18B20 Ambient monitorOneWire pin: GPIO22Ambient data field: d1DS18B20 devices found: 1First sensor address: sensor-specific IDParasite power: OFFWiFi connecting….WiFi connected: assigned IP addressTemperature: 24.69 CSent to Ambient
At this point, the Ambient settings may not yet be complete, so an error may appear during the data transmission step to Ambient. As long as the sensor is recognized and the temperature is displayed, the basic setup on the M5Atom Lite side is complete for now.
Keep the Arduino IDE open, and next configure Ambient using a web browser.
Preparation: Ambient
1. Register with Ambient
Access the Ambient website and create a user account.
Follow the instructions on the screen and enter the required information.
2. Create a Channel
After logging in to Ambient, click the Create Channel button.
A new channel will be created in the channel list.
3. Check the Channel ID and Write Key
Check the following information displayed for the newly created channel.
- Channel ID
- Write key
Copy these values into the corresponding lines of the program that is open in the Arduino IDE.
4. Write the Program Again
After entering the channel ID and write key, compile the program again and write it to the M5Atom Lite.
After uploading the program, check the serial monitor.
If everything is working properly, the temperature will be acquired and sent to Ambient, and the following message will be displayed.
Temperature: 24.69 C
Sent to Ambient
5. Change the Ambient Channel Settings
Return to the Ambient settings page.
In the channel list, click the downward arrow shown in the Settings column and select Change Settings.
Configure the following items as needed.
- Channel name
- Description
- Data item names
For example, if temperature data are being sent to d1, changing the data item name to something like “Room temperature” or “Water temperature” will make the chart easier to understand later.
6. Check the Chart
Click the channel name to display the chart screen.
You can change the chart display settings by clicking the graph title.
Once you have completed the setup up to this point, you will be able to view the temperature data acquired by the M5Atom Lite as a graph on Ambient.
You can now remotely check changes in temperature from the configured sensor using a web browser on your PC or smartphone.
Applications
If you prepare multiple M5Atom Lite units and DS18B20 sensors, you can record temperatures at multiple locations simultaneously.
For example, the system can be used in the following ways.
- Recording the room temperature in a laboratory office
- Recording the room temperature in an experimental room
- Recording the water temperature of fish tanks
- Recording temperature changes around a constant-temperature room or incubator
By changing the settings on Ambient, temperature data sent from multiple sensors can either be plotted together on the same chart or displayed as separate charts.
If the data are noisy, displaying averages over fixed time intervals can also make the chart easier to read.
Data accumulated on Ambient can be downloaded in CSV format. Therefore, the data can also be analyzed later using Excel, R, or other software.
Furthermore, if you make the channel public, you can display graphs of temperature changes without logging in to Ambient by pasting the HTML code for embedding Ambient charts into your own web page.
For detailed setup instructions, please refer to the Ambient tutorial.


