SurtrTech
Published © GPL3+

SHT21/HTU 21 Measure Temperature and Himidity

With Arduino + OLED and LCD

BeginnerFull instructions provided1 hour22,136
SHT21/HTU 21 Measure Temperature and Himidity

Things used in this project

Story

Read more

Schematics

Wiring 1

Wiring 2

Wiring 3

Code

SHT_21_OLED_1.ino

Arduino
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.ino

Arduino
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.ino

Arduino
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

Credits

SurtrTech

SurtrTech

9 projects • 207 followers
YT Channel bit.ly/35Ai76l, run by Automation and Electrical Engineer, Electronics amateur, no IT background so you may see wreckage in codes

Comments