超精密温度計の製作②マイコンはESP32
執筆時、Arduino IDEは1.8.13です。通信機能を使いたいので、ESP32ボードを利用します。最新のESP32-S2はまだ技適が取れていないので、Wi-Fi機能が使えませし、日本では流通していません。現在流通しているESP32ボードは、Wi-FiとBLEが使えます。
動作電圧は3.3Vです。
温度センサTMP117は、動作温度範囲;-55~+150℃で、確度;-20~+50℃の範囲で±0.1℃(最大値)と、小数点第一位までが信頼できます。読み出すデータは16ビットで0.0078125°C (1LSB)の分解能があります。
●I2Cで接続
利用したESP32ボードはLOLIN32です。I2CのSCLは22ピンに、SDAは21ピンに出ています。電源は3.3Vです。
TMP117ボードを接続します。ケーブルは約10cmにしました。I2Cインターフェースは本来プリント基板上のデバイス間をつなぐインターフェースなので、短い距離で使います。裏側には、SCLとSDAそれぞれ、プルアップ抵抗4.7kΩをはんだ付けしました。
切り離したセンサ部分の基板上にはR16が取り付けられているので、スレーブ・アドレスは0x48です。
●スケッチ
Arduino IDEのライブラリ管理でTMP117で検索すると、二つ見つけました。SparkFunのライブラリをインストールしました。
サンプルはいくつか登録されていて、その中のExample1_BasicReadingsを読み込んで動かしました。
摂氏の出力だけにするスケッチにしました。
12行目は、センサの最大データ転送速度400kHzを指定しています。Arduinoは、何も指定しないとデフォルトの100kHzです。
// Madison Chodikov @ SparkFun Electronics May 29 2019
#include <Wire.h> // Used to establish serial communication on the I2C bus
#include <SparkFun_TMP117.h> // Used to send and recieve specific information from our sensor
// The default address of the device is 0x48 = (GND)
TMP117 sensor; // Initalize sensor
void setup(){
Wire.begin();
Serial.begin(115200); // Start serial communication at 115200 baud
Wire.setClock(400000); // Set clock speed to be the fastest for better communication (fast mode)
Serial.println("TMP117 Example 1: Basic Readings");
if (sensor.begin() == true) // Function to check if the sensor will correctly self-identify with the proper Device ID/Address
{
Serial.println("Begin");
}
else
{
Serial.println("Device failed to setup- Freezing code.");
while (1); // Runs forever
}
}
void loop(){
// Data Ready is a flag for the conversion modes - in continous conversion the dataReady flag should always be high
if (sensor.dataReady() == true) // Function to make sure that there is data ready to be printed, only prints temperature values when data is ready
{
float tempC = sensor.readTempC();
float tempF = sensor.readTempF();
// Print temperature in °C and °F
Serial.println(); // Create a white space for easier viewing
Serial.print("Temperature in Celsius: ");
Serial.println(tempC,4);
delay(1000); // Delay added for easier readings
}
}