SHT21 temperature sensor ESP32-S2

Posted by

Here we will learn how to use the SHT21 temperature sensor with ESP32-S2 Franzininho. The SHT21 is a temperature and humidity sensor manufactured by the Swiss company Sensirion. The sensor itself works with 3.3V and will communicate with our Franzininho via i2c.

The sensor reads temperatures between -40ºC and +120ºC, as well as humidity between 0% and 100% relative humidity. With that in mind, this tutorial was created with information from this link, as well as general internet research and experimentation. The schematic diagram of the test is below, it was mounted on a breadboard.

See that Franzininho WiFi has a 3.3V output, which we will use. Other than that, connect the GND and the SDA and SCL of the i2c communication. Board pinout in this link.

SHT21 with ESP32-S2 schematic diagram
SHT21 with ESP32-S2 schematic diagram

The physical assembly is very simple, photos below. All that is necessary is to connect Franzininho to the computer via micro USB cable.

SHT21 with ESP32-S2 on a breadboard
SHT21 with ESP32-S2 on a breadboard
sht21 temperature sensor breadboard
SHT21 with ESP32-S2 assembly

The code

We will use this library for Arduino. As shown in the image below, in the link I just gave, click on “Code > Download ZIP”. In the Arduino IDE, go to “Sketch > Add library > Add .ZIP library” and select the .ZIP file you just downloaded.

SHT21 library files for Arduino
SHT21 library files for Arduino

To open a code example, go to “File > Examples > STH21-Arduino-Library > SHT21_Demo”. The sketch is seen below.

#include <SHT21.h>  // include SHT21 library

SHT21 sht; 

float temp; 	// variable to store temperature
float humidity; // variable to store hemidity

void setup() {
  Wire.begin();		// begin Wire(I2C)
  Serial.begin(9600); // begin Serial
}

void loop() {

  temp = sht.getTemperature();  // get temp from SHT 
  humidity = sht.getHumidity(); // get temp from SHT

  Serial.print("Temp: ");			// print readings
  Serial.print(temp);
  Serial.print("\t Humidity: ");
  Serial.println(humidity);

  delay(500);	// min delay for 14bit temp reading is 85ms
}

Now just click on the “->” arrow on the Arduino IDE (load) and wait for the program to download to Franzininho WiFi. The result can be seen on the serial monitor just below the screen (or in “Tools > Serial Monitor”) . It displays surrounding temperature and humidity every 500mS (half a second).

SHT21 Arduino IDE temperature readings
SHT21 Arduino IDE temperature readings

It is also possible to find out the serial number of your board, using the “STH21_ReadSerialNumber” sketch available in the Arduino IDE in “File > Examples > STH21-Arduino-Library”. The SHT21 temperature sensor is a very dynamic and precise sensor, enjoy it!.

If you don’t have a SHT21 yet, you can purchase it through my Aliexpress link.

3 responses

Leave a Reply

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