B4r and Arduino SHT21 humidity and temperature sensor example

The digital SHT2x humidity sensor series is used in high volumes in a wide variety of applications and has today become the de facto industry standard. The SHT2x series consists of a low-cost version with the SHT20 humidity sensor, a standard version with the SHT21 humidity sensor, and a high-end version with the SHT25 humidity sensor. Its an I2C device so again is very simple to connect to any arduino


Size 3 x 3 x 1.1 mm
Output I²C digital, PWM, SDM
Supply voltage range 2.1 to 3.6 V
Energy consumption 3.2µW (at 8 bit, 1 measurement / s)
RH operating range 0 – 100% RH
T operating range -40 to +125°C (-40 to +257°F)
RH response time 8 sec (tau63%)

This is the breakout for the sensor that I bought.

Here is a simple schematic, again be careful as Vcc is 3.3v with this device




Lets look at the required libraries and a basic code example, there is not much to this to be honest most of the work is done in the library




You need to install the following library – https://github.com/markbeee/SHT21

#Region Project Attributes
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 300
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'Public variables can be accessed from all modules.
Public Serial1 As Serial
Public timer1 As Timer
Public sht21humi As Float
Public sht21temp As Float
End Sub
Private Sub AppStart
timer1.Enabled = True
End Sub
Private Sub Timer1_Tick
Log("Temperature is: ", sht21temp, "°C")
Log("Humidity is: ", sht21humi, " %")
End Sub
#if C
#include <Wire.h>
#include "SHT21.h"
SHT21 SHT21;
void setup(B4R::Object* o){
void loop (B4R::Object* o) {
b4r_main::_sht21humi =SHT21.getHumidity();
b4r_main::_sht21temp =SHT21.getTemperature();
#End if



In the log view

Temperature is: 20.9541°C
Humidity is: 47.1311 %
Temperature is: 25.0403°C
Humidity is: 48.8706 %
Temperature is: 26.7027°C
Humidity is: 51.6553 %
Temperature is: 27.8289°C
Humidity is: 50.8161 %
Temperature is: 28.6547°C
Humidity is: 50.0150 %
Temperature is: 29.2982°C
Humidity is: 50.1371 %
Temperature is: 29.7915°C
Humidity is: 49.0003 %
Temperature is: 30.2420°C
Humidity is: 49.0079 %
Temperature is: 30.1562°C
Humidity is: 59.5365 %



these come in at about $7 for a useful breakout board with this sensor

Humidity Sensor – SHT21 Breakout Board