Arduino UNO R4 Minimaでセンサ・インターフェーシング ⑧ 温度センサADT7410
Adafruitから入手した温度センサADT7410(アナログ・デバイセズ)を利用します。
●AdafruitのStemma QT/Qwiicボード
Stemma QT/Qwiic(JST SH 4ピン)コネクタは2か所に装着されていて、どちらにつないでもかまいません。このコネクタを使ってI2Cで制御する場合、特に、ジャンパ線をつなぐなどは不要です。
コネクタは、表と裏のどちらも差し込めそうですが、ピンが内部の上部に並んでいるので、差し込める方向は一意です。ロック機構はないですが、すぐに抜けるということはありません。
●温湿度センサADT7410のおもなスペック
- 動作電圧 2.7〜5.5V
- 平均動作電流 250uA
- 温度の確度 −40℃〜+105℃(2.7 V〜3.6 V)で ±0.5℃
- 分解能 16ビット時;0.0078℃、デフォルトの13ビット時;0.0625℃
- 熱応答時間 25->85℃ 5秒
●使用環境
- Arduino UNO R4 Minima
- Arduino IDE 2.1.1
- Windows10 22H2
●接続
Arduino UNO R4 MinimaのI2C信号とセンサをJSTコネクタでつなぎます(Stemma QT/Qwiicボードの写真の比率は大きい)。電源は3.3Vです。
Adafruitの温度センサADT7410のスレーブ・アドレスはデフォルトでは0x48ですが、変更できるので、複数個同時に使えます。
●スレーブ・アドレスを確認
従来からよく使われているi2cScanner.inoを動かしてスレーブ・アドレスを確認します。電源は3.3Vです。
0x48を見つけてきました。
購入時、ボードにあるA0とA1はショートされていませんが、はんだをもってジャンパ部分をショートすると下記のようにアドレスを変更できます。
A0 | A1 | スレーブ・アドレス |
---|---|---|
- | - | 0x48 |
ショート | - | 0x49 |
- | ショート | 0x4a |
ショート | ショート | 0x4b |
●ライブラリの用意
ADT7410で検索して、見つかったAdafruit ADT7410ライブラリをインストールします。
インストールを始めると次のパネルが出ました。
全てをインストールを選びました。
●スケッチ
メニューのファイル->スケッチ例から、Adafruit ADT7410Libraryのadt7410test.inoを選択します。
setup{}は、次のように修正しました。
- Serial.begin()に待ち時間を追加
- tempsensor.begin(0x48) スレーブ・アドレス0x48はデフォルト値なので省略されているが、明示的に追加した
void setup() {
Serial.begin(115200);
while (!Serial)
delay(10);
Serial.println("ADT7410 demo");
// Make sure the sensor is found, you can also pass in a different i2c
// address with tempsensor.begin(0x49) for example
if (!tempsensor.begin(0x48)) {
Serial.println("Couldn't find ADT7410!");
while (1);
}
実行例です。
●7セグメントLED表示をつなぐ
連載の第4回目の記事を参照しながら表示器を接続します。
Arduino UNO R4 Minimaでセンサ・インターフェーシング ④ 温湿度センサSi7021の測定結果を7セグメントLEDに表示
●7セグメントLED表示をつないで測定値を表示
上記の表示をするスケッチと、温度測定のスケッチを合体させます。
// K.Townsend (Adafruit Industries)
// Written by Limor Fried/Ladyada for Adafruit Industries.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_ADT7410.h"
#define ADT7410_CONFIG 0x3 ///< Configuration register
#define ADT7410_address 0x48
Adafruit_7segment matrix = Adafruit_7segment();
Adafruit_ADT7410 tempsensor = Adafruit_ADT7410(); // Create the ADT7410 temperature sensor object
void setup() {
Serial.begin(115200);
while (!Serial)
delay(10);
Serial.println("\nADT7410 + 7segment LED");
// Make sure the sensor is found, you can also pass in a different i2c
// address with tempsensor.begin(0x49) for example
if (!tempsensor.begin(0x48)) {
Serial.println("Couldn't find ADT7410!");
while (1);
Wire.beginTransmission(ADT7410_address);
Wire.write((byte)ADT7410_CONFIG);
Wire.write((byte)0x80); // 16bit
Wire.endTransmission();
}
// sensor takes 250 ms to get first readings
delay(250);
matrix.begin(0x70);
matrix.setBrightness(0x05); // default 0x0E
}
void loop() {
// Read and print out the temperature
float c = tempsensor.readTempC();
Serial.print("Temp: "); Serial.println(c,4);
matrix.print(c,2);
matrix.writeDisplay();
delay(5000);
}
ライブラリのソースはこちらです。デフォルトの13ビットで測定し、16ビットのデータを読み出しているように読めました。なので、上記のスケッチでは、最高分解能である16ビットの測定に変更してあります。
実行例です。
<補足>
// K.Townsend (Adafruit Industries)
// Written by Limor Fried/Ladyada for Adafruit Industries.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define ADT7410_CONFIG 0x03 ///< Configuration register
#define ADT7410_address 0x48
#define ADT7410_TemperatureRegiste 0x00
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3D ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
Serial.begin(115200);
while (!Serial)
delay(10);
Serial.println("\nADT7410 + SSD1306");
Wire.beginTransmission(ADT7410_address); // 16bit
Wire.write((byte)0x2f); // reset
Wire.endTransmission();
delay(10);
Wire.beginTransmission(ADT7410_address); // 16bit
Wire.write((byte)ADT7410_CONFIG);
Wire.write((byte)0b11100000); // 16bit
Wire.endTransmission();
delay(200);
display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
display.clearDisplay();
display.drawRect(0, 0, display.width(), display.height(), SSD1306_WHITE);
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(4,3);
display.println("Temperature:");
display.display();
delay(200);
}
void loop() {
Wire.beginTransmission(ADT7410_address);
Wire.write((byte)0x00);
Wire.endTransmission(false);
Wire.requestFrom(ADT7410_address, 2);
//wait for response
while(Wire.available() == 0);
int T = Wire.read();
T = T << 8 | Wire.read() ;
Serial.println(T,BIN);
float tt = (-(T & 0b1000000000000000) | (T & 0b0111111111111111) ) / 128.0;
Serial.print("T: "); Serial.println(tt,4);
display.setTextSize(2); // Draw 2X-scale text
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(10,17);
display.fillRect(10, 17, 70, 16, SSD1306_BLACK);
display.println(tt);
display.display();
delay(5000);
}
