Arduino Projects

Interfacing SHT3x Temperature and Humidity sensor with Arduino

Learn how to connect highly reliable SHT3x temperature and humidity sensor with Arduino Uno board.

Introduction

In this Quick tutorial, learn how to make a Realtime temperature and humidity monitor using SHT3x and Arduino by displaying their values on 16X2 LCD display module or on an OLED display.

Before getting started lets learn about SHT3x working and its pin diagram.

SHT3X Temperature and Humidity sensor, How it works and its pinout diagram:

There are many sensor boards out in the market which can measure temperature and humidity wisely, but when you need an accurate, precise and compact only few of them comes under the list. One of them is SHT3x series sensor as they are successors of SHT2x that set the industry level standards in humidity and temperature measurements, SHT3x comes with SHT30 and base version, SHT31 a standard version and All the way goes to SHT35 High end version which can be used in automotive level sensing.

SHT3x is packed with a completely new CMO Sens Chip, which will increase reliability along with improved accuracy in measuring. The SHT3x offers a range of new features when compared with SHT2x and other sensors, such as enhanced signal processing, two distinctive and user-selectable I2C addresses(0x44 or 0x45), an alert mode with programmable humidity and temperature limits, and communication speeds of up to 1 MHz.

Related Project: Digital Thermometer using LM35 and Arduino

SHT3x is more precise and accurate than other sensors like LM35, DHT11, DHT22, Thermistors, DS18B20 and has a greater range of measuring temperature than few of them which is  -40° to +125°C (-40° to +257°F) and relative humidity ranging from 0 to 100%. It also has wide input voltage range from 2.2v to 5.5v.

It has an accuracy of ±3% to ±1.5% in measuring humidity from base version to high-end version respectively, And ±0.3 to ±0.1 in temperature measurements.

Specifications:

Output I²C, Voltage Out
Supply voltage range 2.15 to 5.5 V
Energy consumption 4.8µW (at 2.4 V, low repeatability, 1 measurement / s)
RH operating range 0 – 100% RH
T operating range -40 to +125°C (-40 to +257°F)
RH response time 8 sec (tau63%)

SHT3x Pinout diagram:

Below is the pinout diagram of SHT3X Temperature and Humidity Sensor.

From the above pinout diagram, There you can see 4 pins two pins for Voltage input and Ground and another two pins for I2C connection SCL and SDA.

Interfacing SHT3x Temperature and Humidity sensor with Arduino

In this method we are going to connect SHT31 with Arduino and display the output over the LCD display.

EXTRAS: Using this sensor you can also build and IoT based Health monitoring system using ESP32 for measuring the room temperature and humidity

Required Components:

  • Sensirion SHT31 Humidity Temperature Sensor
  • Arduino UNO R3 Development Board
  • JHD162A 16×2 LCD Display
  • Few connecting wires

Circuit Diagram:

SHT3x Temperature and Humidity sensor with Arduino circuit diagramConnect all the required components as shown in the above schematic diagram. Here we used LCD module with I2C adapter if you wish to connect it directly with out I2C refer the below link.

Connect Arduino with LCD Display with out I2C.

Here we are connecting both devices in I2C connection, So we need to find there I2C addresses, If you already know there addresses you can mention in code, if not you can find them with I2C scanner which we used in our previous examples.

I2C scanner.

After connecting everything upload the code to Arduino, so it works accordingly.

Source Code:

Copy the below code and paste it in Arduino IDE, and select the correct board and serial port and click upload.

You need 2 libraries to run the below code you can directly install them through library manager or download them through below links manually.

  • Adafruit_SHT31.h : Link
  • LiquidCrystal_I2C.h : Link
#include <LiquidCrystal_I2C.h>
#include <Arduino.h>
#include "Adafruit_SHT31.h"
LiquidCrystal_I2C lcd (0x3F, 16,2); 
Adafruit_SHT31 sht31 = Adafruit_SHT31();
byte degree[8] =
{
0b00011,
0b00011,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000
};
 
void setup() {
Serial.begin(9600);
lcd.init ();
lcd.backlight ();
lcd.createChar(1, degree);
 
while (!Serial)
delay(10); // will pause Zero, Leonardo, etc until serial console opens
 
Serial.println("SHT31 test");
if (! sht31.begin(0x44)) { // Set to 0x45 for alternate i2c addr
Serial.println("Couldn't find SHT31");
while (1) delay(1);
}
}
 
void loop() {
float t = sht31.readTemperature();
float h = sht31.readHumidity();
 
if (! isnan(t)) { // check if 'is not a number'
Serial.print("Temp *C = "); Serial.println(t);
lcd.print("Temp = ");
lcd.print(t);
lcd.write(1);
lcd.print("C");
} else {
Serial.println("Failed to read temperature");
lcd.print("Temperature Error");
}
 
if (! isnan(h)) { // check if 'is not a number'
Serial.print("Hum. % = "); Serial.println(h);
lcd.setCursor (0,1);
lcd.print("Hum. = ");
lcd.print(h);
lcd.print(" %");
} else {
Serial.println("Failed to read humidity");
lcd.setCursor (0,1);
lcd.print("Humidity Error");
}
Serial.println();
delay(1000);
lcd.clear();
}

After Uploading the code open the serial monitor to check the output and also you can see the temperature and Relative humidity on the LCD display which we connected.

SHT3x hygrometer and thermometer with Arduino outputAs you can see from the above image the LCD Display shows the temperature and Humidity values in Realtime so this Example is used to build Thermometer and Hygrometer.

Displaying output on OLED display:

This method is very easy if you read the above LCD I2C method. Everything is same in the circuit diagram except replacing the LCD display with OLED display as it has the same 4 pins.

Required components:

  • Sensirion SHT31 Humidity Temperature Sensor
  • Arduino UNO R3 Development Board
  • OLED Display
  • Few connecting wires

Circuit Diagram:

SHT3x Temperature and Humidity sensor with Arduino OLED display circuit diagramConnect all the required components as shown in the above schematic diagram. And upload the below given code to work as a Hygrometer and Thermometer.

Both the modules are connected in I2C method so if you need the I2C address use I2c Scanner, Link given above.

Source Code:

 

#include "Adafruit_SHT31.h"
Adafruit_SHT31 sht31 = Adafruit_SHT31();
#include <Wire.h>
#include <Adafruit_GFX.h> 
#include <Adafruit_SSD1306.h> 
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void setup() {
      Serial.begin(9600);
      delay(10);
      Serial.println("SHT31 test");
      if (! sht31.begin(0x44)) {   // Set to 0x45 for alternate i2c addr
      Serial.println("Couldn't find SHT31");
      while (1) delay(1);
      }
        if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }
  delay(2000);
  display.clearDisplay();
}
void loop() {
  
    float t = sht31.readTemperature();
    float h = sht31.readHumidity();
    
    if (! isnan(t)) {  // check if 'is not a number'
    Serial.print("Temp *C = "); Serial.println(t);
    }
    else {    
    t=0.0;
    Serial.println("Failed to read temperature");
    }
    
    if (! isnan(h)) {  // check if 'is not a number'
    Serial.print("Hum. % = "); Serial.println(h);
    }
    else { 
    h=0.0;
    Serial.println("Failed to read humidity");    
    }
    Serial.println();
    delay(500);
display.clearDisplay();
    
    // display temperature
    display.setTextSize(1);
    display.setCursor(0,7);
    display.print("Temperature: ");
    display.setTextSize(2);
    display.setCursor(0,18);
    display.print(t);
    display.print(" ");
    display.setTextSize(1);
    display.cp437(true);
    display.write(167);
    display.setTextSize(2);
    display.print("C");
    
    // display humidity
    display.setTextSize(2);
    display.setCursor(0, 45);
    display.print(h);
    display.print("%");
    display.setTextSize(1);
    display.setCursor(74, 50);
    display.print(" ");
    display.print("Rel H");  
    display.display(); 
}

After uploading the code Open serial monitor and Check the OLED display to check the values as shown in the below image.

SHT3x hygrometer and thermometer with Arduino OLED outputIf you found this tutorial helpful please share it with your friends and if you have any doubts please feel free to contact us through Facebook page.

CircuitSchools Staff

We at CircuitSchools publish on basics of electronics and electric components and everything related to evolution of electrical technology with complete analysis of development boards and modules along with latest projects with innovative ideas.

Related Articles

3 Comments

  1. Hi, I am using ttgo lora esp32 that has ssd1306 oled display at sda/4 and scl/15 . I am trying to attach a SHT31 temp/humidity sensor via IIC to same sda/scl or a different sda/scl but somehow it couldnt find the sensor. When i use your code, which i assume uses the default sda/scl (GPIO 21/22) then the sensor works but the display does not as the display is connected to 4/15. Can you help?

Leave a Reply to Riaz Ahmed Cancel reply

Your email address will not be published. Required fields are marked *

Back to top button