ESP8266 NodeMCU with SHT31 Temperature & Humidity Sensor

In this user guide, we will learn to interface SHT31 Temperature and Humidity Sensor module with ESP8266 NodeMCU. Firstly, we will introduce you to SHT31 sensor including its pinout, features, and interfacing with ESP8266. Secondly, we will install the Adafruit SHT31 library in Arduino IDE to access the sensor data. Lastly, we see two examples to display temperature and humidity values on a serial monitor and on the SSD1306 OLED display.

We have similar guides with ESP32 and Arduino Uno:

SHT31 Sensor Module Introduction

The SHT31 temperature and sensor module comes in a compact user-friendly size that features the SHT31 chip from Sensirion. This chip is in fact the temperature and humidity sensor that offers various functions, interfaces and extremely easy to use with a microcontroller. The sensor is able to measure temperature readings in the range of -40 to 125°C with a good accuracy of ±0.2°C. Similarly, the SHT32 chip also offers a humidity sensor which offers an operating humidity range of 0-100% with a typical relative humidity accuracy of 2%.

Due to its small size, low cost, and wide operating voltage range of 2.15-5.5V, the SHT31 sensor is a great choice to be used with microcontrollers for temperature and humidity measurements e.g. thermostats, weather stations, etc.

SHT31 Sensor Module hardware overview

I2C Interface

The SHT31 Sensor Module uses I2C communication protocol to transmit sensor data to the microcontroller. The sensor module features the SCL (serial clock) and SDA (serial data) pins which connect with the I2C interface of ESP8266 or other sensors supporting the same interface.

One interesting thing to note here is that, the SHT31 sensor module comes with two I2C addresses which are 0x44 and 0x45. By default, the I2C address is set as 0x44. The user can change the I2C address through the I2C Address Selection Pin commonly known as AD.

SHT31 Sensor Module AD Pin

The AD pin consists of an internal pull-down resistor which ensures that when this pin is unconnected (low state), the I2C address is 0x44 (default). Whereas, when the state of this pin is set high then the I2C address is 0x45. This address changing feature gives the users the liberty to use multiple SHT31 sensor modules on the same bus by setting the same I2C address for each sensor.

Alert Output Pin

The SHT31 sensor module has a unique feature that it features an alert output pin commonly known as AL to set the module in Alert Mode. By using this pin, the user can enable the sensor to work alongside the microcontroller where the sensor keeps on obtaining readings and the microcontroller works on different tasks. This happens whenever the temperature and humidity readings are greater than the user defined range. The AL pin therefore acts as a triggering pin that works like an interrupt.

The AL pin goes to a high state whenever either of the humidity or temperature reading crosses the upper or lower user defined limit. The pin remains in the high state until the values go back to the normal limit range.

SHT31 Sensor Module AL Pin

SHT31 Sensor Module Pinout

The following figure shows the pinout diagram of SHT31 sensor module. SHT31 sensor module consists of six pins.

SHT31 Sensor Module Pinout

The following lists the pinout of the SHT31 sensor module and their brief description.

SHT31 Module PinDescription
VINThis is the pin that supplies power to the sensor module in the range 2.4-5.5V. Connect it with 3.3V or Vin pin of ESP8266.
GNDThis is the ground pin for providing the common ground between the devices.
SCLThis is the serial clock pin which generates the clock signal.
SDAThis is the serial data pin which is used to send and receive data
ADThis is the I2C address selection pin.
ALThis is the Alert output pin.

Specifications

The table below shows the specifications of the SHT31 temperature and humidity sensor.

Typical relative humidity accuracy2 %RH
Operating relative humidity range0 – 100 %RH
Response time (τ63%)8 s
Typical temperature accuracy0.2 °C
Operating temperature range-40 – 125 °C
Response time (τ63%)less than 2 s
Supply voltage2.4 – 5.5 V
Average supply current1.7 uA
Maximum supply current1500 uA

Interface SHT31 Module with ESP8266

In this section, let us show you how to connect the SHT31 sensor module with ESP8266.

SHT31 with ESP8266

You will need the following components

  • ESP8266 NodeMCU board
  • SHT31 Sensor Module
  • Jumper wires

The connection of SHT31 with the ESP8266 board is super simple. Connect the VCC terminal of SHT31 module with 3.3V of ESP8266, ground with the ground (common ground), SCL of the sensor with SCL pin of ESP8266, and SDA of the sensor with the SDA pin of ESP8266.

For ESP8266, the default I2C pins for SDA are GPIO4 (D2) and for SCL are GPIO5 (D1). The connections between the two devices can be seen in the table below.

ESP8266SHT31 Module
3.3VVin
GPIO4 (D2)SDA
GPIO5 (D1)SCL
GNDGND

Follow the schematic diagram below for the ESP8266 module and connect them accordingly.

SHT31 with ESP8266 connection diagram

Install SHT31 Arduino Library

Before we proceed further, you should make sure that you have the latest version of Arduino IDE installed on your system. Moreover, you should also install an ESP8266 add-on in Arduino IDE. If your IDE does not have the plugin installed you can visit the link below:

Installing ESP8266 library in Arduino IDE and upload code

As we are connecting the SHT31 sensor module with ESP8266, therefore, we will have to install the libraries to access the sensor data. We will require two libraries for this project:

  1. Adafruit SHT31
  2. Adafruit BusIO

We will use the Library Manager in our Arduino IDE to install the latest versions of the libraries. Open your Arduino IDE and go to Sketch > Include Libraries > Manage Libraries. Type Adafruit SHT31 in the search bar and install the latest version.

Install Adafruit SHT31 Library

Type Adafruit BusIO in the search bar and install it as well.

Install Adafruit BusIO Library

Arduino Code – Get SHT31 Readings on Arduino Serial Monitor

This sketch displays current temperature and humidity readings on Arduino serial monitor after every second.

#include <Arduino.h>
#include <Wire.h>
#include "Adafruit_SHT31.h"

Adafruit_SHT31 sht31 = Adafruit_SHT31();

void setup() {
  Serial.begin(115200);

  if (! sht31.begin(0x44)) {   
    Serial.println("Check circuit. SHT31 not found!");
    while (1) delay(1);
  }
}

void loop() {
  float temp = sht31.readTemperature();
  float hum = sht31.readHumidity();

  if (! isnan(temp)) { 
    Serial.print("Temperature(°C): "); 
    Serial.print(temp); 
    Serial.print("\t\t");
  } else { 
    Serial.println("Failed to read temperature!");
  }
  
  if (! isnan(hum)) {  
    Serial.print("Humidity(%): "); 
    Serial.println(hum);
  } else { 
    Serial.println("Failed to read humidity!");
  }

  delay(1000);
}

How Code Works?

Now, let us understand how each part of the code works.

The code starts with including all the necessary libraries which are needed for the proper functionality of the code. The Wire.h will allow us to communicate through the I2C protocol. The Adafruit_SHT31.h library will enable us to initialize and read the SHT31 sensor.

#include <Arduino.h>
#include <Wire.h>
#include "Adafruit_SHT31.h"

Next we create the Adafruit_SHT31 object called ‘sht31.’

Adafruit_SHT31 sht31 = Adafruit_SHT31();

setup()

Inside the setup() function, we start the serial communication at a baud rate of 115200 and then initialize the SHT31 sensor. If the sensor initialization is not a success, a relevant message will be printed on the serial monitor.

void setup() {
  Serial.begin(115200);

  if (! sht31.begin(0x44)) {   
    Serial.println("Check circuit. SHT31 not found!");
    while (1) delay(1);
  }
}

loop()

Inside the loop() function, the microcontroller first obtains the SHT31 sensor readings after every second and stores them in their respective float variables, ‘temp’ for temperature and ‘hum’ for humidity. Temperature readings are acquired through sht31.readTemperature(). Humidity readings are accessed using the sht31.readHumidity() function. If the sensor fails to output correct readings, the serial monitor will print a failure message regarding it.

void loop() {
  float temp = sht31.readTemperature();
  float hum = sht31.readHumidity();

  if (! isnan(temp)) { 
    Serial.print("Temperature(°C): "); 
    Serial.print(temp); 
    Serial.print("\t\t");
  } else { 
    Serial.println("Failed to read temperature!");
  }
  
  if (! isnan(hum)) {  
    Serial.print("Humidity(%): "); 
    Serial.println(hum);
  } else { 
    Serial.println("Failed to read humidity!");
  }

  delay(1000);
}

Demonstration

Choose the correct board and COM port. Go to Tools > Board and select NodeMCU 1.0. Next, go to Tools > Port and select the appropriate port through which your board is connected.

select ESP8266 NodeMCU board

Click on the upload button to upload the code into the ESP8266 board. After you have uploaded your code to the board press its RST button.

ESP8266 NodeMCU reset button

In your Arduino IDE, open up the serial monitor and set the baud rate to 115200. The serial monitor will start displaying the temperature and humidity readings along with the units. New readings will be displayed after every second.

SHT31 with ESP8266 Display Sensor Data on Serial Monitor

Display SHT31 Sensor values on OLED Display

In this section, we will see how to display SHT31 sensor readings on a 0.96 SSD1306 OLED display using Arduino IDE and ESP8266. 

Installing SSD1306 OLED Library in Arduino IDE

To use the OLED display in our project, we have to install the Adafruit SSD 1306 library in Arduino IDE. Follow the steps below to successfully install it.

Open Arduino IDE and click on Sketch > Library > Manage Libraries. The following window will open up.

search library Arduino IDE

Type ‘SSD1306’ in the search tab and install the Adafruit SSD1306 OLED library.

Install OLED SSD1306 Library Arduino IDE

Schematic – OLED with ESP8266 and SHT31

SHT31 with ESP8266 and OLED

You will need the following components

  • ESP8266 NodeMCU board
  • SHT31 Sensor Module
  • SSD1306 OLED
  • Breadboard
  • Jumper wires

The table below shows the terminals of the three devices which should be connected together.

OLED DisplayESP8266SHT31
VCC3.3VVIN
GNDGNDGND
SCLGPIO5 (D1)SCL
SDAGPIO4 (D2)SDA

Assemble the circuit as shown in the schematic diagram below:

SHT31 with ESP8266 and OLED connection diagram

As you can see above, we have connected all the VCC terminals with 3.3V pin of ESP32. The SCL terminals are connected with D1 and the SDA terminals are connected with D2 of the ESP8266 board. The grounds are also common.

Arduino Sketch to Display SHT31 Readings on OLED

Copy the following code to your Arduino IDE and upload it to ESP8266 after assembling the above circuit diagram.

#include <Arduino.h>
#include <Wire.h>
#include "Adafruit_SHT31.h"
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

Adafruit_SHT31 sht31 = Adafruit_SHT31();
Adafruit_SSD1306 display = Adafruit_SSD1306(128, 64, &Wire, -1);

void setup() {
  Serial.begin(115200);

  if (! sht31.begin(0x44)) {   
    Serial.println("Check circuit. SHT31 not found!");
    while (1) delay(1);
  }

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  
   // init done
  display.display();
  delay(100);
  display.clearDisplay();
  display.display();
  display.setTextColor(WHITE);
}

void loop() {
  float temp = sht31.readTemperature();
  float hum = sht31.readHumidity();

  if (! isnan(temp)) {  
    Serial.print("Temperature *C = "); 
    Serial.print(temp); 
    Serial.print("\t\t");
  } else { 
    Serial.println("Failed to read temperature!");
  }
  
  if (! isnan(hum)) {  
    Serial.print("Humidity % = "); 
    Serial.println(hum);
  } else { 
    Serial.println("Failed to read humidity!");
  }

  display.setCursor(0,0);
  display.clearDisplay();

  display.setTextSize(1);
  display.setCursor(0,0);
  display.print("Temperature: ");
  display.setTextSize(2);
  display.setCursor(0,10);
  display.print(temp);
  display.print(" ");
  display.setTextSize(1);
  display.cp437(true);
  display.write(167);
  display.setTextSize(2);
  display.print("C");
  
  // display humidity
  display.setTextSize(1);
  display.setCursor(0, 35);
  display.print("Humidity: ");
  display.setTextSize(2);
  display.setCursor(0, 45);
  display.print(hum);
  display.print(" %");
  
  display.display();
  delay(1000);
}

How the Code Works?

The maximum portion of the code is the same as we have discussed earlier except for the OLED display part. Therefore, we will only explain the OLED display part here.

We will first include all the required libraries for the SHT31 sensor as well as the OLED display which we just installed before.

#include <Arduino.h>
#include <Wire.h>
#include "Adafruit_SHT31.h"
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

Now, we will create another object named ‘display’ which will be handling the OLED display. Also, define the size of the OLED display by passing arguments to the Adafruit_SSD1306() function.

Adafruit_SSD1306 display = Adafruit_SSD1306(128, 64, &Wire, -1);

Initialize the OLED display by calling the begin() method on the display object.

display.begin(SSD1306_SWITCHCAPVCC, 0x3C); 

Next, we will clear the OLED screen by calling clearDisplay() function. Also, we set the color of the text using setTextColor() function and pass WHITE as an argument. If we have a dark background, we will display our text in white and if we have a bright background then we will display the text in black.

  display.clearDisplay();
  display.display();
  display.setTextColor(WHITE);

Display Readings on the OLED

Inside the loop() function, we obtain the SHT31 sensor readings and print them on the OLED display.

We display the temperature and humidity values on the serial monitor on the OLED one by one. This block of code displays SHT31 sensor values on OLED and updates the values after every 1 second.

 display.setCursor(0,0);
  display.clearDisplay();

  display.setTextSize(1);
  display.setCursor(0,0);
  display.print("Temperature: ");
  display.setTextSize(2);
  display.setCursor(0,10);
  display.print(temp);
  display.print(" ");
  display.setTextSize(1);
  display.cp437(true);
  display.write(167);
  display.setTextSize(2);
  display.print("C");
  
  // display humidity
  display.setTextSize(1);
  display.setCursor(0, 35);
  display.print("Humidity: ");
  display.setTextSize(2);
  display.setCursor(0, 45);
  display.print(hum);
  display.print(" %");
  
  display.display();
  delay(1000);

In the above code, the setTextSize() function is used to set the size of the font. We used low size for simple text such as “Temperature” and “Humidity” and high size font to display actual temperature and humidity readings. We will use the setCursor() function to denote the x and the y-axis position from where the text should start. Finally, the print() function writes the text on the defined position.

Demonstration

Choose the correct board and COM port. Go to Tools > Board and select NodeMCU 1.0. Next, go to Tools > Port and select the appropriate port through which your board is connected.

select ESP8266 NodeMCU board

Click on the upload button to upload the code into the ESP8266 board. After you have uploaded your code to the board press its RST button.

ESP8266 NodeMCU reset button

Once the code is successfully uploaded to the board, the OLED will start displaying the readings.

SHT31 with ESP8266 Display Sensor Data on OLED

Video demo:

You may also like to read:

Leave a Comment