10  SHT31 temperature & humidity sensor

10.1 Introduction

In this tutorial, we will learn how to use the ESP32 microcontroller with the SHT31 temperature and humidity sensor. We will cover the basics of the sensor, including how it works and how to connect it to an ESP32. We will also learn how to use the Adafruit_SHT31 library to read temperature and humidity values from the sensor. By the end of this tutorial, you will be able to build your own ESP32 and SHT31-based projects.

10.2 SHT31 Sensor

The SHT31 is a high-precision temperature and humidity sensor that can be used in a wide range of applications. It measures temperature and humidity using a capacitive humidity sensor and a band-gap temperature sensor. It has a wide measurement range of -40°C to 125°C for temperature and 0-100% for relative humidity. The SHT31 is a digital sensor, which means it communicates with the microcontroller over a digital interface. See more details on the Adafruit’s github page for the SHT31 and full specs on the SHT31 datasheet.

10.3 Libraries

To use the SHT31 sensor with an ESP32 microcontroller, we will need to use the Adafruit_SHT31 library. This library is specifically designed to work with the SHT31 sensor and makes it easy to read temperature and humidity values from the sensor.

To install the library, follow these steps:

  1. Open the Arduino IDE
  2. Go to Sketch > Include Library > Manage Libraries
  3. Search for “Adafruit_SHT31” and click the Install button
  4. Wait for the library to install

10.4 Wiring

To connect the SHT31 sensor to the ESP32 microcontroller, we will need to use four wires: VCC, GND, SDA, and SCL. Here is a table that shows how to connect the wires:

SHT31 Pin ESP32 Pin
VCC 3V3
GND GND
SDA GPIO21
SCL GPIO22

Note: The ESP32’s 3V3 pin provides 3.3 volts of power, which is the correct voltage for the SHT31 sensor. Do not use the 5V pin, as it will damage the sensor.

10.5 Code

Here is the code for reading temperature and humidity values from the SHT31 sensor.

Download code
/*
 * This program is a modification of
 * http://www.esp32learning.com/code/esp32-and-sht31-sensor-example.php
 */
#include <Arduino.h>   // Include the Arduino library
#include <Wire.h>      // Include the Wire library for I2C communication
#include "Adafruit_SHT31.h" // Include the SHT31 library from Adafruit

Adafruit_SHT31 sht31 = Adafruit_SHT31(); // Create an instance of the SHT31 object

void setup() {
  Serial.begin(115200);        // Initialize the serial communication with a baud rate of 112500
  if (! sht31.begin(0x44)) {   // Check if SHT31 is connected and start it with address 0x44
    // Print an error message if SHT31 is not found
    Serial.println("Couldn't find SHT31");
    while (1) delay(1);  // Wait indefinitely
  }
}

void loop() {
  // Read temperature from SHT31 and assign it to "temperature"
  float temperature = sht31.readTemperature(); 
  // Read relative humidity from SHT31 and assign it to "humidity"
  float humidity = sht31.readHumidity();

  if (! isnan(temperature)) {     // Check if t is not NaN (not a number)
    Serial.print("temp_deg_C:");  // Print temperature label
    Serial.print(temperature);    // Print temperature value
    Serial.print("\t");           // Write a tab space
  } 
  else {
    // Print an error message if temperature reading fails
    Serial.println("Failed to read temperature");
  }

  if (! isnan(humidity)) {        // Check if h is not NaN (not a number)
    Serial.print("rel_hum_%:");   // Print humidity label
    Serial.print(humidity);       // Print humidity value
  } 
  else {
    // Print an error message if humidity reading fails
    Serial.println("Failed to read humidity");
  }

  Serial.println();    // Break line
  delay(1000);         // Wait 1 second
}