Want to keep a log of the climate in your greenhouse, build a humidor control system, or track temperature and humidity data for a weather station project? The SHT31 Temperature & Humidity Sensor may be the right choice for you!
SHT31 sensor is factory-calibrated and requires no external components to work. So with just a few connections and some Arduino code, you can start measuring relative humidity and temperature right away.
Hardware Overview
The module carries a low-cost, easy to use, highly accurate, digital temperature & humidity sensor, from Sensirion – SHT31.
The small size of the module allows it to be used for almost anything, such as thermostats, humidistats, indoor weather stations and similar devices, for monitoring or controlling humidity and/or temperature.
The SHT31 sensor is capable of reading humidity over the full range of 0 to 100% RH with a typical accuracy of ±2% over the range of 20% to 80% RH (0.01% RH resolution).
The maximum temperature range of the SHT31 is -40 to 125°C. It has a typical accuracy of ±0.3°C at 25°C (0.015°C resolution).
Warning:
The SHT31 sensor has a small window that exposes the polymer sensing film responsible for measuring temperature and humidity. It is advisable to prevent liquids, dust or other contaminants from coming into contact with it as it may affect the accuracy of the sensor.
Power Requirement
The sensor itself uses 2.4V to 5.5V which makes this module 3V or 5V compliant. So, you can use it with your favorite 3.3V or 5V microcontroller without worry.
The SHT31 consumes less than 0.8mA during measurements and less than 0.2µA during single shot mode(not measuring). This low power consumption allow the implementation in battery driven devices such as handsets, wearables or smart watches.
I2C Interface
The SHT31 is a I2C sensor, meaning it uses the two I2C data/clock wires available on most microcontrollers, and can share those pins with other I2C sensors as long as they don’t have an address collision.
It supports two separate I2C addresses: 0x44Hex and 0x45Hex. This allows two SHT31 modules to be used on the same bus or to avoid address conflicts with another device on the bus.
The AD pin determines the I2C address of the module. This pin has a built-in pull-down resistor. Therefore, when you leave the AD pin unconnected, the default I2C address is 0x44Hex and when you connect it to a high voltage signal, the I2C address becomes 0x45Hex.
Alert Mode
The SHT31 has an alert (AL) output pin that can trigger when the environmental condition (humidity and/or temperature) exceeds user-defined limits. This enables measurements to be interrupt driven instead of using polling, allowing the host microcontroller to perform other tasks while the data is collected by the sensor.
When the humidity and/or temperature crosses the upper limit, the Alert pin goes HIGH and remains HIGH until the temperature drops below the clear limit. Similarly when the humidity and/or temperature exceeds the lower limit, the alert pin goes HIGH and remains HIGH until the temperature rises above the clear limit.
Below image shows the different limits for the Alert Mode.
You can read more about it in a separate application note.
Technical Specifications
Here are the complete specifications:
Power supply | 2.4V to 5.5V |
Current draw | ~0.8mA (during measurements) |
~0.2µA (during single shot mode) | |
Humidity Range | 0 to 100 %RH |
Humidity Accuracy | ±2% over the range of 20% to 80% RH |
Temperature Range | -40ËšC to +125ËšC |
Temperature Accuracy | ±0.3˚C at 25°C |
For more details, please refer below datasheet.
SHT31 Module Pinout
Now let’s have a look at the pinout.
VCC is the power pin. Since the sensor uses 2.4-5.5VDC, give it the same power as the logic level of your microcontroller – e.g. for a 5V micro like Arduino, use 5V.
GND is the common ground for power and logic.
SCL is the I2C clock pin, connect to your microcontrollers I2C clock line.
SDA is the I2C data pin, connect to your microcontrollers I2C data line.
AD pin determines the I2C address of the module. When you leave the AD pin unconnected, the default I2C address is 0x44Hex and when you connect it to a high voltage signal, the I2C address becomes 0x45Hex.
AL pin triggers when the environmental condition (humidity and/or temperature) exceeds user-defined limits.
Wiring up a SHT31 Module to an Arduino
Wiring up the SHT31 sensor is very easy!
There are only four pins that need to be hooked up in order to start using the sensor. One for VCC, one for GND, and two data lines for I2C communication.
Connect the SCL pin to the I2C clock pin and the SDA pin to the I2C data pin on your Arduino. Note that each Arduino Board has different I2C pins which should be connected accordingly. On the Arduino boards with the R3 layout, the SDA (data line) and SCL (clock line) are on the pin headers close to the AREF pin. They are also known as A5 (SCL) and A4 (SDA).
The following illustration shows the wiring.
Library Installation
To get your sensor up and running, you will need to install the Adafruit SHT31 library. It is available from the Arduino library manager.
To install the library navigate to the Sketch > Include Library > Manage Libraries… Wait for Library Manager to download libraries index and update list of installed libraries.
Filter your search by typing ‘SHT31‘ and install the library.
The Adafruit_SHT31 library uses the Adafruit Bus IO helper library internally to abstract away I2C & SPI transactions and registers. So, search the library manager for adafruit bus and install that as well.
Arduino Code – Reading Temperature and Humidity
Below is a basic Arduino sketch. Go ahead and upload it to your Arduino. You will see the current temperature and humidity in your room!
#include <Arduino.h>
#include <Wire.h>
#include "Adafruit_SHT31.h"
Adafruit_SHT31 sht31 = Adafruit_SHT31();
void setup() {
Serial.begin(9600);
if (! sht31.begin(0x44)) { // Set to 0x45 for alternate I2C address
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.print(t); Serial.print("\t\t");
} else {
Serial.println("Failed to read temperature");
}
if (! isnan(h)) { // check if 'is not a number'
Serial.print("Hum. % = "); Serial.println(h);
} else {
Serial.println("Failed to read humidity");
}
delay(1000);
}
Once your code is uploaded, open the serial terminal at 9600bps. You should see something like the output below. Try breathing on the sensor to see both humidity and temperature values change!
Code Explanation:
The code is quite straightforward. At the beginning, Arduino.h
, Wire.h
and Adafruit_SHT31.h
libraries are included and an Adafruit_SHT31 object is created in the global space.
#include <Arduino.h>
#include <Wire.h>
#include "Adafruit_SHT31.h"
Adafruit_SHT31 sht31 = Adafruit_SHT31();
In the setup, we initialize the serial communication with PC and call the begin()
function.
The begin(<address>)
function initializes the sensor, where <address>
is the I2C address of the sensor. By default its 0x44Hex, you can also adjust the sensor for 0x45Hex and then pass that value in. This function returns True if the sensor was found and responded correctly and False if it was not found.
void setup() {
Serial.begin(9600);
if (! sht31.begin(0x44)) { // Set to 0x45 for alternate I2C address
Serial.println("Couldn't find SHT31");
while (1) delay(1);
}
}
Once initialized, you can access object’s (sht31) methods using the dot operator.
sht31.readTemperature() returns floating point (decimal + fractional) temperature reading in °C. You can convert to Fahrenheit by multiplying by 1.8 and adding 32.
sht31.readHumidity() returns humidity reading, also as a floating point value between 0 and 100 (this reads % humidity)
void loop() {
float t = sht31.readTemperature();
float h = sht31.readHumidity();
if (! isnan(t)) { // check if 'is not a number'
Serial.print("Temp *C = "); Serial.print(t); Serial.print("\t\t");
} else {
Serial.println("Failed to read temperature");
}
if (! isnan(h)) { // check if 'is not a number'
Serial.print("Hum. % = "); Serial.println(h);
} else {
Serial.println("Failed to read humidity");
}
delay(1000);
}