Read HTS221 Temperature and Humidity with ESP32

Use this page with AI

Explore with your favorite AI

Gemini & other AI

Copy this page’s context into your AI to explore how it works and what you can build.

Open Gemini

Copy the prompt, then paste it into Gemini’s message box.

View prompt

Table of contents

Overview

This sample uses the AP02 ESP32 MCU leaf to read the HTS221 temperature and humidity sensor on the AI01 4-Sensors leaf and display the readings in the Arduino IDE serial monitor. It covers assembling the leaves, running the sketch and applying two-point correction using a reference instrument.

Leaf to use

Use the following leaves.

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

Assembly

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

assemble1

Source code

Write the following program in the Arduino IDE.

The sketch requires additional libraries. If you have not installed them, follow the library installation guide.

//=====================================================================
//  Thermo-hygrometer ESP32
//
//    (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 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

2 point-to-point correction

If the temperature or humidity reported by 4-Sensors differs from the reference value, apply two-point correction. The following example uses temperature.

Measure two temperature points with both 4-Sensors and a reference instrument. Enter the measured values in the sample sketch and run it to bring the displayed 4-Sensors readings closer to the reference values.

Execution Results

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

Leaf photo
Search documentation, sample apps, and Q&A