Table of Contents
Overview:
In this quick tutorial learn how to connect SHT31 temperature and humidity sensor with ESP32 TTGO LoRa32 SX1276 OLED display board to read the temperature and humidity values from the sensor and display over the built in OLED display.
SHT31 Temperature and Humidity sensor
SHT31 sensor is the standard one among its SHT3x family where it can measure temperature and relative humidity accurately and precisely than its predecessor SHT2x series. SHT3x family also has a base SHT30 and High end model SHT35 which you can choose according to your requirement.
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.
SHT31 Technical Specifications:
- Communication: I2C
- Supply voltage range: 2.15 to 5.5 V
- Temperature range: -40 to +125°C (-40 to +257°F)
- Temperature accuracy:±0.3 @-40-90 °C
- Humidity range: 0 – 100% RH
- Humidity accuracy: ±2 @0-100% RH
- RH response time: 8 sec (tau63%)
- Energy consumption: 4.8µW (at 2.4 V, low repeatability, 1 measurement/s)
SHT31 Pinout diagram:
Below is the pinout diagram of SHT31 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.
ESP32 TTGO LoRa32 module pinout diagram
This board has the ESP32 chip with Built in WiFi and Bluetooth same as ESP32. It has few other features like an in built OLED display and SX1276 LoRa module for long range transmissions which we can learn in our other articles.
As you can see from the above pin diagram the board has built in OLED display and few types of GPIO and analog pins which we discussed in our previous article.
Interfacing SHT31 Temperature and Humidity sensor with ESP32 TTGO LoRa32
Before getting started with interfacing lets know what are the required components.
Required components:
Product Name | Quantity | ||
---|---|---|---|
Sensirion SHT31 Humidity Temperature Sensor | 1 | https://amzn.to/34aiUzt | https://amzn.to/3pNgldZ |
ESP32 TTGO LoRa32 SX1276 OLED display board | 1 | https://amzn.to/3C89W20 | https://amzn.to/3vSUwhe |
Few Connecting Wires | https://amzn.to/3H2BV4e | https://amzn.to/3J0WVu2 |
Circuit Diagram:
Connect all the required components listed above as per the below schematic diagram.
As you can see from the above circuit diagram we had connected SDA and SCL pins of SHT31 to 21(GPIO21) and 22(GPIO22) pins of TTGO LoRa32 board respectively through I2C connection. And we powered the SHT31 pins VIN and GND through 5V and GND pins of TTGO board. The I2C OLED display is already connected to 4(SDA) and 15(SCL) by default.
After Connecting everything according to the above image, Its time to upload the code which you can find below.
Source Code:
Connect TTGO LoRa32 board to PC where Arduino IDE is installed. Copy the below code and paste it in the Arduino workspace and Choose the Board as TTGO LoRa32-OLED V1 from the Tools menu. Choose the correct Port where the board is connected. Install the required libraries from the below links or from the library manager and hit Upload.
Required Libraries:
/********* Complete project details at https://www.circuitschools.com/interfacing-sht31-with-esp32-ttgo-lora32-and-display-on-its-oled-display/ No Republishing with out attribution. *********/ #include <Arduino.h> //Libraries for OLED Display #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> //Libraries for SHT31 #include "Adafruit_SHT31.h" //OLED pins #define OLED_SDA 4 #define OLED_SCL 15 #define OLED_RST 16 #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels //SHT21 definition #define SDA 21 #define SCL 22 TwoWire I2Cone = TwoWire(1); Adafruit_SHT31 sht31 = Adafruit_SHT31(); //For more details www.circuitschools.com Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RST); //Initialize OLED display void startOLED(){ //reset OLED display via software pinMode(OLED_RST, OUTPUT); digitalWrite(OLED_RST, LOW); delay(20); digitalWrite(OLED_RST, HIGH); //initialize OLED Wire.begin(OLED_SDA, OLED_SCL); if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3c, false, false)) { // Address 0x3C for 128x32 Serial.println(F("SSD1306 allocation failed")); for(;;); // Don't proceed, loop forever } display.clearDisplay(); display.setTextColor(WHITE); display.setTextSize(1); display.setCursor(0,0); display.print("Display started "); } void startSHT(){ I2Cone.begin(SDA, SCL, 100000); if (! sht31.begin(0x44)) { // Set to 0x45 for alternate i2c addr Serial.println("Couldn't find SHT31"); while (1) delay(1); } } void setup() { //initialize Serial Monitor Serial.begin(115200); startOLED(); startSHT(); } void loop() { float t = sht31.readTemperature(); float h = sht31.readHumidity(); display.clearDisplay(); display.setCursor(0,0); display.setTextSize(1); display.print("Circuitschools"); if (! isnan(t)) { // check if 'is not a number' Serial.print("Temp *C = "); Serial.println(t); display.setCursor(0,20); display.print("Temperature:"); display.setCursor(72,20); display.print(t); } else { Serial.println("Failed to read temperature"); display.setCursor (0,20); display.print("Temperature Error"); } if (! isnan(h)) { // check if 'is not a number' Serial.print("Hum. % = "); Serial.println(h); display.setCursor(0,30); display.print("Humidity:"); display.setCursor(54,30); display.print(h); display.display(); } else { Serial.println("Failed to read humidity"); display.setCursor (0,30); display.print("Humidity Error"); } Serial.println(); delay(10000); }
After uploading the code press reset button on the TTGO board and open serial monitor to check the values and SHT31 status which you can also check on OLED display as shown in the below output image.
Output or result:
Few other ESP32 TTGO LoRa32 boards have different SDA and SCL pins so check with the manufacturer and pinouts to make a correct connection. If you got any errors while working on this project, please feel free to comment below. And if you like this tutorial please follow our Facebook Meta page “Circuit Schools” for interesting projects.
Thanksfor the effort. I will try and get back to you.
I haven’t connected the temp sensor yet but get an error on line 53 when compiling. “call of overloaded ‘begin(int, int, int)’ is ambiguous. Line 53: I2Cone.begin(SDA, SCL, 100000);
Hi John Lewis,
This happens with specific versions of the Arduino framework. While testing someone came across this error specifically when upgrading to arduino-esp32 version 2.0.1.
To solve this, if possible downgrade the or upgrade the arduino esp32 version, or You need to explicitly cast the third argument from 100000 to 400000.
Thanks and hope the error doesn’t appear again.