Project tutorial
SHT21/HTU 21 Measure Temperature and Himidity

SHT21/HTU 21 Measure Temperature and Himidity © GPL3+

With Arduino + OLED and LCD

  • 13 views
  • 0 comments
  • 0 respects

Components and supplies

About this project


Tutorial if needed

Products used: GY-21/SHT-21/HTU-21 Temperature and Humidity sensor0.96″ OLED display 128×64 Blue/Yellow

So this tutorial is about interfacing the SHT-21 or HTU-21 or GY-21, call it as you want, temperature and humidity sensor, we gonna see how to test it first then how to use it with OLED and LCD i²c screens, the humidity will be displayed in %RH and for the temperature it will be with (°C) Celsius and (°F) Fahrenheit.

Here are some quick data taken from the module datasheet:

*The accuracy changes depending on the conditions:

So this is the module I'm using:

SHT 21

As you can see the module has 4 pins (Vin/GND) are for power using 3.3V/GND from Arduino board, and SCL/SDA for i²c communication.

Test 1

For the test 1 and the rest we’ll need of course an Arduino board, here I’m using UNO, you can add some jumpwires and a breadboard… And we gonna test the module with the serial monitor and check the values given by it.

Wiring

Wiring_1 can be found below.

This is the direct wiring, the module is powered by 3.3V and for i²c interface we need SDA/SCL wired with A4/A5.

Test 0

Here before I use any library/code or add other modules I prefer to test if the modules works first, to do so, wire the module and upload the i²c scanner code from the Arduino Playground:Get the Code from the Arduino Playground

Once the code is uploaded, open the serial monitor you’ll be able to see that a device is found and its address.

Library

Now we can proceed to our next steps, test the module and check its values…

This the library I used you can download it from Github:Download the SHT-21 Library by e-radionica.com

Once the.ZIP file is downloaded go to Arduino IDE -> Sketch -> Include Library -> Add.ZIP Library and look for the ZIP file to be installed.

Code

The code I used is the first example from the library, after you install the library go to:File -> Examples -> SHT21-Arduino-Library-Master -> SHT21_Demo

Result

After uploading the code open the serial monitor you’ll be able to see the temperature in Celsius and the humidity in %RH.

Test 2

In this test we gonna add an OLED screen, here I used 128×64 you can use 128×32 aswell but you have to modify the code.

Wiring

wiring_2 can befound below

Both modules uses 3.3V and i²c interface, so they share all the pins:

Libraries

In addition to the SHT-21 library we have to add libraries for the OLED:

Download here  Adafruit OLED SSD1306 library.Download here Adafruit GFX Library (may not be required, but in case you want to add some shapes).

Download and install just like before.

Codes

Here I’ve used 2 different codes, they have little differences:

  • The first code displays the (T/H) simultaneously in (°C/%RH)
  • The second code displays the T for 2s and then H for 2s in (°F/%RH)

Codes can be found below.

Results

This is the result for the code 1:

Here is the result for the code 2:

Each of them is displayed every 2s, and I also converted the values to “Int”

Test 3

In this test, instead of using the OLED we gonna use the LCD i²c for the people who have this display…

Wiring

Wiring_3 can be found below

Again all the modules share all the pins.

Libraries

Beside the SHT21 library, we’ll need LCD i²c library:Download the LCD i²c library.

It’s installed the same way as a normal Arduino library.

Code

I’ve created one code only, it displays the Temperature in °C and Humidity in %RH, you can modify it to display the °F…

Code can be found below

Result

Here is the result for the test 3:

That's all folks !

*Sorry if I don't respond to comments, I don't receive notifications.

Code

SHT_21_OLED_1.inoArduino
Code for OLED type 1
/* This code works with SHT-21/HTU-21/GY-21 Digital temperature and humidity sensor and 128*64 OLED screen
 * It displays the Temperature in Celsius and Humidity in %RH in real time
 * Refer to www.surtrtech.com for more details
 */

#include <SHT21.h>               //SHT21 and OLED libraries
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET    -1 // Reset pin # (or -1 if sharing Arduino reset pin)

SHT21 sht;                      //SHT on OLED entities
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); 
 

float Temp;          //Here we store the temperature and humidity values
float Humidity;

void setup() {
  
  Wire.begin();                              //Start the ic communication
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //Start the OLED display
  display.clearDisplay();
  display.display();

}

void loop() {
  
  Temp = sht.getTemperature();       //To get the temperature and humidity values and store them in their respective variable
  Humidity = sht.getHumidity();

  display.clearDisplay();            //Clear the display everytime
  display.setTextSize(2);            //Setting the text size and color         
  display.setTextColor(WHITE);             
  display.setCursor(0,17);           //Setting the cursor position
  display.print("T: ");              //Display the temperature and humidity as "T: 24.64 C
  display.print(Temp);               //                                         H: 59.12 %"
  display.print(" C");
  display.setCursor(0,40); 
  display.print("H: ");
  display.print(Humidity);
  display.print(" %");
  display.display();               //The display takes effect
                                   //You can add a delay here to avoid the fluctuations

}
SHT_21_OLED_2.inoArduino
Code for OLED type 2
/* This code works with SHT-21/HTU-21/GY-21 Digital temperature and humidity sensor and 128*64 OLED screen
 * It displays the Temperature in Fahrenheit and Humidity in %RH only one of them every 2s
 * Refer to www.surtrtech.com for more details
 */

#include <SHT21.h>               //SHT21 and OLED libraries
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET    -1 // Reset pin # (or -1 if sharing Arduino reset pin)

SHT21 sht;                      //SHT on OLED entities
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); 
 

float Temp;          //Here we store the temperature and humidity values
float Humidity;

void setup() {
  
  Wire.begin();                              //Start the ic communication
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //Start the OLED display
  display.clearDisplay();
  display.display();

}

void loop() {
  
  Temp = sht.getTemperature();       //To get the temperature and humidity values and store them in their respective variable
  Humidity = sht.getHumidity();

  display.clearDisplay();            //Clear the display everytime
  display.setTextSize(3);            //Setting the text size and color                     
  display.setTextColor(WHITE);                             
  Print_T();                         //Function that displays the Temperature in F
  display.display();
  delay(2000);                       //Wait 2s
  display.clearDisplay();
  Print_H();                         //Function that displays the Humidity in %RH
  display.display();
  delay(2000);

}

void Print_T(){
  display.setCursor(0,22);
  display.print("T: ");
  Temp=Temp*1.8+ 32;         //The value is given by C here we convert it to F
  display.print(Temp,0);     //Here since I'm using a bigger text size, I've chosen to display the float variables as integers: Temp,0 means it will display no number after the comma
  display.print(" F");
}
void Print_H(){
  display.setCursor(0,22);
  display.print("H: ");
  display.print(Humidity,0);
  display.print(" %");
}
SHT_21_LCD.inoArduino
Code for LCD
/* This code works with SHT-21/HTU-21/GY-21 Digital temperature and humidity sensor and 16x2 LCD screen
 * It displays the Temperature in Celsius and Humidity in %RH in real time
 * Refer to www.surtrtech.com for more details
 */

#include <SHT21.h>              //SHT21 and LCD ic libraries
#include <LiquidCrystal_I2C.h>

#define I2C_ADDR 0x27 //I2C adress, you should use the code to scan the adress first (0x27) here, it may be 0x3F
#define BACKLIGHT_PIN 3 // Declaring LCD Pins
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7

SHT21 sht;                //SHT and LCD entities
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
 
float Temp;            //Here we store the temperature and humidity values
float Humidity;

void setup() {
  
  Wire.begin();
  lcd.begin (16,2);
  lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
  lcd.setBacklight(HIGH); //Lighting backlight
  lcd.home();  

}

void loop() {
  
  Temp = sht.getTemperature();           //To get the temperature and humidity values and store them in their respective variable
  Humidity = sht.getHumidity();

  lcd.clear();                         //Clear the LCD and set the cursor position
  lcd.setCursor(0,0);
  lcd.print("Temp: ");                //Print the temperature and humidity as "Temp: 23.18 C
  lcd.print(Temp);                    //                                      "Humi: 64.13 %"
  lcd.print(" C");
  lcd.setCursor(0,1);
  lcd.print("Humi: ");
  lcd.print(Humidity);
  lcd.print(" %");
  delay(1000);                     //You can modify or remove the delay
}
Github
https://github.com/e-radionicacom/SHT21-Arduino-Library
Github
https://github.com/adafruit/Adafruit_SSD1306
Github
https://github.com/adafruit/Adafruit-GFX-Library

Schematics

Wiring 1
Wiring 1 bb wb13kx7cga
Wiring 2
Wiring 2 bb a9djp3f9ph
Wiring 3
Wiring 3 bb fygrjhwtie

Comments

Similar projects you might like

Arduino Temperature - Humidity - Rain Sensor

Project showcase by Rick_Findus

  • 16,251 views
  • 6 comments
  • 19 respects

Arduino Based Digital Temperature Sensor

Project showcase by Abishek Bhalaaji

  • 18,794 views
  • 1 comment
  • 16 respects

Temperature sensor

Project tutorial by Grant

  • 26,327 views
  • 6 comments
  • 44 respects

Contactless Temperature Sensor MLX90614

by SurtrTech

  • 13,468 views
  • 4 comments
  • 24 respects

Pressure Airflow Measure Device with Analog Sensor

Project tutorial by Team ItMightBeWorse

  • 7,667 views
  • 3 comments
  • 13 respects

Temperature Indicator with Arduino

Project tutorial by PCBWay and Silícios Lab

  • 5,731 views
  • 2 comments
  • 21 respects
Add projectSign up / Login