温湿度センサ読み取り

このページをAIに渡す

いつものAIに相談

Gemini・その他のAI

このページの情報を添えて、仕組みや使い方、つくりたいものを相談できます。

Geminiを開く

質問文をコピーして、Geminiの入力欄に貼り付けてください。

質問文を確認

目次

概要

4-Sensorsリーフに搭載された温湿度センサの値を読み取って、シリアルモニタに表示するサンプルです。

使用するリーフ

以下のリーフを使用します

TypeNameQ’ty
AZ62Connector Cover1
AI014-Sensors1
AZ01USB1
AP03STM32 MCU1
AV01CR20321
CR2032 coin cell battery1
M2*15mm screw2

リーフの組み立て

下図を参考にリーフを組み立ててみましょう。

assemble1

ソースコード

Arduino IDEで下記のプログラム1を書きましょう。

このスケッチを動かすためには、ライブラリのインストールが必要です。 まだインストールしていない場合は開発環境設定ページを参考にライブラリをインストールしてください。

//=====================================================================
//  Thermo-Hygrometer
//
//    (c) 2021 LEAFONY SYSTEMS Co., Ltd
//    Released under the MIT license
//    https://opensource.org/licenses/MIT
//
//      Rev.00 2021/04/01  First release
//=====================================================================
#include <Wire.h>
#include <HTS221.h>

//---------------------------
// Data for two-point correction
//---------------------------
// Temperature correction data 0
float TL0 = 25.0;     // 4-Sensors Temperature measurement value
float TM0 = 25.0;     // Thermometer and other measurements value
// Temperature correction data 1
float TL1 = 40.0;     // 4-Sensors Temperature measurement value
float TM1 = 40.0;     // Thermometer and other measurements value

// Humidity correction data 0
float HL0 = 60.0;     // 4-Sensors Humidity measurement value
float HM0 = 60.0;     // Hygrometer and other measurements value
// Humidity correction data 1
float HL1 = 80.0;     // 4-Sensors Humidity measurement value
float HM1 = 80.0;     // Hygrometer and other measurements value

void setup() {
  // initialize serial communication at 115200 second per second:
  Serial.begin(115200);
  Wire.begin();             // I2C 100kHz
  // initialize i2c communication with HTS221:
  smeHumidity.begin();
  delay(10);
}

void loop() {
  // read temperature and humidity:
  float dataTemp = (float)smeHumidity.readTemperature();
  float dataHumid = (float)smeHumidity.readHumidity();

  // calibration:
  dataTemp = TM0 + (TM1 - TM0) * (dataTemp - TL0) / (TL1 - TL0);      // Temperature correction
  dataHumid = HM0 + (HM1 - HM0) * (dataHumid - HL0) / (HL1 - HL0);    // Humidity correction

  Serial.println(String(dataTemp) + "[℃], " + String(dataHumid) + "[%]");
  delay(1000);
}

Thermo-Hygrometer.ino

2点間補正

表示させたい温度(または、湿度)と4-Sensorsの温度(または、湿度)に、ずれがあるときは、2点間補正を行います。以下は、温度を例に、補正方法を記します。

まず、4-Sensorsと、基準となる計測器で、2点の温度を測定してください。次に、測定した値を、サンプルスケッチに書き込んで、実行していただければ、4-Sensorsの温度が、表示させたい温度に近づきます。 

実行結果

Arduino IDEのシリアルモニタを開き、ボーレートを115200bpsとすると、温度と湿度が表示されます。

Footnotes

  1. プログラムは、AVR MCUリーフと同じです。 ↩

リーフ写真
技術資料・サンプル・Q&Aから探せます