Temperature and humidity sensor
Overview
This sample reads the value of the temperature and humidity sensor on the 4-Sensors Leaf and displays it on a serial monitor.
Leaf to use
Use the following leaves.
Type | Name | Q’ty |
---|---|---|
AZ62 | Connector Cover | 1 |
AI01 | 4-Sensors | 1 |
AZ01 | USB | 1 |
AP01 | AVR MCU | 1 |
AV01 | CR2032 | 1 |
CR2032 coin cell battery | 1 | |
M2*15mm screw | 2 |
Assembly
Let’s assemble leaves as shown in the figure below.
Source code
Write the following program in the Arduino IDE.
In order to make this sketch work, you need to install the libraries If you haven’t installed it yet, refer to Environment to install it.
//=====================================================================
// Thermo-hygrometer
//
// (c) 2020 Trillion-Node Study Group
// Released under the MIT license
// https://opensource.org/licenses/MIT
//
// Rev.00 2020/05/05 First release
//=====================================================================
#include <Wire.h>
#include <HTS221.h>
//---------------------------
// 2点補正用データ
//---------------------------
// 温度補正用データ0
float TL0 = 25.0; // 4-Sensors温度測定値
float TM0 = 25.0; // 温度計等測定値
// 温度補正用データ1
float TL1 = 40.0; // 4-Sensors温度測定値
float TM1 = 40.0; // 温度計等測定値
// 湿度補正用データ0
float HL0 = 60.0; // 4-Sensors湿度測定値
float HM0 = 60.0; // 湿度計等測定値
// 湿度補正用データ1
float HL1 = 80.0; // 4-Sensors湿度測定値
float HM1 = 80.0; // 湿度計等測定値
void setup() {
// initialize serial communication at 115200 second per second:
Serial.begin(115200);
// 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); // 温度補正
dataHumid = HM0 + (HM1 - HM0) * (dataHumid - HL0) / (HL1 - HL0); // 湿度補正
Serial.println(String(dataTemp) + "[℃], " + String(dataHumid) + "[%]");
delay(1000);
}
Two-point correction
If there is a discrepancy between the temperature (or humidity) you want to display and the 4-Sensors' temperature (or humidity), perform a two-point correction. The following is an example of how to compensate for this, using temperature as an example.
First, measure the temperature at two points with the 4-Sensors and a reference instrument. Then, write the measured values into a sample sketch and execute them, and the temperature of the 4-Sensors will be close to the temperature you want to display.
Execution Results
Open the serial monitor of the Arduino IDE and set the baud rate to 115200 bps and you will see the temperature and humidity.
