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) 2021 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>
//---------------------------
// 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);
}
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.