Nano 33 BLE Senseをデータ入力に使う<BLE>⑪外部I2C温度センサTMP107<BLEペリフェラル>

 いままで、マイコン・ボードNano 33 BLE Senseに搭載されたセンサを扱いました。ここでは、I2Cバス経由で、外部に温度センサTMP117を取り付け、BLEペリフェラルにします。

温度センサTMP117のおもなスペック

  データシート TMP117 高精度、低消費電力、デジタル温度センサ SMBus および I2C 互換インターフェイス搭載

  • 動作電圧 1.8~5.5V
  • 分解能 16ビット、LSBは0.007812℃
  • 動作温度範囲  -55~150℃
  • 確度
    • -20~+50℃ の範囲で±0.1℃ (最大値)
    • -40~+100℃ の範囲で±0.2℃ (最大値)
    • -55~+150℃ の範囲で±0.3℃ (最大値)
  • 消費電流 1Hzの変換サイクルで3.5uA
  • インターフェース I2C(1~400kHz)
  • データ長 2の補数形式の16ビット
  • 平均化出力が可能
  • 温度設定のアラートあり

Adafruitのブレークアウト・ボード

 最初に温度センサを取り上げます。STEMMA QT(JST SH 4ピン)コネクタは2か所に装着されていて、どちらにつないでもかまいません。このボードは、電源電圧は3.3/5Vのどちらでも動きます。Arduino Nano 33 BLE Senseは3.3Vです。

  TMP117 Precision Temperature Sensor

  SDA/SCL信号には10kΩのプルアップ抵抗が入っています。I2Cのスレーブ・アドレスがデフォルトが0x48で、裏面のADDRの部分をショートすると0x49になるようです。試していません。

接続

スケッチ①

 Adafruitのサンプルbasic_test.inoです。


/**
 * @file basic_test.ino
 * @author Bryan Siepert for Adafruit Industries
 * @brief Shows how to specify a
 * @date 2020-11-10
 * 
 * @copyright Copyright (c) 2020
 */
#include <Wire.h>
#include <Adafruit_TMP117.h>
#include <Adafruit_Sensor.h>

Adafruit_TMP117  tmp117;
void setup(void) {
  Serial.begin(115200);
  while (!Serial) delay(10);     // will pause Zero, Leonardo, etc until serial console opens
  Serial.println("Adafruit TMP117 test!");

  // Try to initialize!
  if (!tmp117.begin()) {
    Serial.println("Failed to find TMP117 chip");
    while (1) { delay(10); }
  }
  Serial.println("TMP117 Found!");

}
void loop() {

  sensors_event_t temp; // create an empty event to be filled
  tmp117.getEvent(&temp); //fill the empty event object with the current measurements
  Serial.print("Temperature  "); Serial.print(temp.temperature);Serial.println(" degrees C");
  Serial.println("");

  delay(1000);
}

 実行中の様子です。

スケッチ②

 Wireライブラリを使って、レジスタを直接読み書きします。


#include  <Wire.h> 
#define TMP117address 0x48
#define TemperatureRegister 0x00
#define ConfigurationRegister 0x01

float read_tempdata() {
  Wire.beginTransmission(TMP117address); 
    Wire.write((uint8_t)TemperatureRegister);
  Wire.endTransmission();
  Wire.requestFrom(TMP117address, 2);
  //  wait for response
  while(Wire.available() == 0);
  int16_t T = Wire.read();
  T = T << 8 | Wire.read() ; 
  return ( -(T & 0b1000000000000000) | (T & 0b0111111111111111) ) * 7.8125 /1000.0;
}

void setup() {
  Wire.begin();
  Serial.begin(9600);
  while(!Serial);
  Serial.println("\nTMP117 test");
  Wire.beginTransmission(TMP117address); 
    Wire.write((uint8_t)ConfigurationRegister);
    Wire.write((uint8_t)0x00);  // high
    Wire.write(0x60);        // low 32averaged conversions
  Wire.endTransmission();
}

void loop() {
  float temp = read_tempdata();
  Serial.println(temp,4);
  delay(1000);

}

 実行中の様子です。

BLE部分を追加


#include <Wire.h>
#include <ArduinoBLE.h>
#define TMP117address 0x48
#define TemperatureRegister 0x00
#define ConfigurationRegister 0x01

// BLE Service
#define TMP117_SERVICE1_UUID   "F000AA30-0451-4000-B000-000000000000"
BLEService Sensor_TMP117_Service1(TMP117_SERVICE1_UUID);

// BLE  Characteristic
#define TMP117_Temp_Characteristic_UUID       "F000AA31-0451-4000-B000-000000000000"
BLEFloatCharacteristic    TMP117_Temperature(TMP117_Temp_Characteristic_UUID, BLERead | BLENotify);

// BLE  Descriptor
#define TMP117_Temp_Descriptor_UUID       "2901"
BLEDescriptor   TMP117_Temp_Descriptor(TMP117_Temp_Descriptor_UUID, "Temperature ; `C");

#define localNAME  "Nano33BLE_Temp"
#define DeviceNAME "Nano33BLE"

float read_tempdata() {
  Wire.beginTransmission(TMP117address); 
    Wire.write((uint8_t)TemperatureRegister);
  Wire.endTransmission();
  Wire.requestFrom(TMP117address, 2);
  //  wait for response
  while(Wire.available() == 0);
  int16_t T = Wire.read();
  T = T << 8 | Wire.read() ; 
  return ( -(T & 0b1000000000000000) | (T & 0b0111111111111111) ) * 7.8125 /1000.0;
}

float previousMillis = 0;  // last time value was checked, in ms

void setup() {
  Serial.begin(9600);
  while(!Serial);
  Serial.println("\nTMP117 test");
  
  Wire.begin();
  Wire.beginTransmission(TMP117address); 
    Wire.write((uint8_t)ConfigurationRegister);
    Wire.write((uint8_t)0x00);  // high
    Wire.write(0x60);        // low
  Wire.endTransmission();

  if (!BLE.begin()) {
    Serial.println("starting BLE failed!");
    while (1);
  }
  BLE.setLocalName(localNAME);
  BLE.setDeviceName(DeviceNAME);

  //// set the service1
  BLE.setAdvertisedService(Sensor_TMP117_Service1);

  // add characteristic
  Sensor_TMP117_Service1.addCharacteristic(TMP117_Temperature);  

  // add descriptor
  TMP117_Temperature.addDescriptor(TMP117_Temp_Descriptor);

  //// add service
  BLE.addService(Sensor_TMP117_Service1);

  // set initial value for this characteristic
  TMP117_Temperature.writeValue(25.0);

  // start advertising
  BLE.advertise();
  Serial.println("\nBluetooth device active, waiting for connections...");
}

void loop() {
  // wait for a BLE central
  BLEDevice central = BLE.central();

  // if a central is connected to the peripheral:
  if (central) {
    delay(100);
    Serial.print("\nConnected to central: ");
    // print the central's BT address:
    Serial.println(central.address());

    // check data every 500ms
    // while the central is connected:
    while (central.connected()) {
      long currentMillis = millis();
      // if 500ms have passed, check value:
      if (currentMillis - previousMillis >= 500) {
        previousMillis = currentMillis;
        updateValue();
        delay(1000);
      }
    }
    // when the central disconnects
    Serial.print("Disconnected from central: ");
    Serial.println(central.address());
  }

  delay(1000);
}

void updateValue() {
  float temp = read_tempdata();
  Serial.println(temp,4);
  float valueof_TMP117_Temperature = temp;
  // update  characteristic
  TMP117_Temperature.writeValue(valueof_TMP117_Temperature);
}

 RLS10のBLEセントラルの画面です。実数はIEEE 754 binary32のフォーマットで送られてきます。

前へ

Nano 33 BLE Senseをデータ入力に使う<BLE>⑩9軸慣性センサ<BLEセントラル>

次へ

Nano 33 BLE Senseをデータ入力に使う<BLE>⑫外部I2C温度センサTMP107<BLEセントラル>