Skip to content
ショップ

Temperature and humidity sensor

This sample reads the value of the temperature and humidity sensor mounted on the 4-Sensors leaf and displays it on the serial monitor.

Use the following leaf.

TypeNameQ’ty
AZ62Connector Cover2
AI014-Sensors1
AX07Back to back1
AP02ESP32 MCU1
AV042V~4.5V1
AZ63Nut Plate2
AAA battery holder1
AAA battery3
M2*8mm screw2
M2*12mm screw2
φ10x2mm magnet1

Let’s assemble leaves as shown in the figure below.

assemble1

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 ESP32
//
// (c) 2020 Trillion-Node Study Group
// Released under the MIT license
// https://opensource.org/licenses/MIT
//
// Rev.00 2020/07/28 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 bit per second:
Serial.begin(115200);
// I2C 初期化
pinMode(21, OUTPUT); // SDA
digitalWrite(21, 0);
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); // 温度補正
dataHumid = HM0 + (HM1 - HM0) * (dataHumid - HL0) / (HL1 - HL0); // 湿度補正
Serial.println(String(dataTemp) + "[℃], " + String(dataHumid) + "[%]");
delay(1000);
}

ESP32_Thermo-Hygrometer.ino

If there is a discrepancy between the temperature (or humidity) that you want to display and the temperature (or humidity) of the 4-Sensors, perform the correction between the two points. The following is a description of the correction method using temperature as an example.

First, measure the temperatures of the two points with the 4-Sensors and a reference instrument. Then, write the measured values in the sample sketch and run it, and the temperature of the 4-Sensors will be close to the temperature you want to display. 

Open the serial monitor in the Arduino IDE and set the baud rate to 115200 bps to display the temperature and humidity.