Measuring temperature with the SHT20, SHT35, and SHT40

FunduinoLast updated on

Drei I2C-Sensoren für Temperatur und Luftfeuchtigkeit im Vergleich

Temperatur und Luftfeuchtigkeit zu messen gehört zu den häufigsten Aufgaben für einen Mikrocontroller. Die Sensoren SHT20, SHT35 und SHT40 erledigen beides in einem Bauteil und liefern ihre Werte fertig umgerechnet über den I2C-Bus – du brauchst also nur vier Leitungen und musst nichts kalibrieren.

Diese Anleitung zeigt für alle drei Sensoren die Verkabelung, die jeweils passende Bibliothek und ein lauffähiges Programm.

Small board with the SHT40 temperature and humidity sensor
The SHT40 sensor module
Circuit diagram with Arduino and SHT40 module
Connecting the SHT40 to an Arduino via the I2C bus
1 / 2

You need this

GY-SHT4x temperature and humidity sensor - I2C, -40 to +125 °C, 2 versions GY-SHT4x temperature and humidity sensor - I2C, -40 to +125 °C, 2 versions — GY-SHT40-D 4,42 € *Item no. F23107628About the product — GY-SHT4x temperature and humidity sensor - I2C, -40 to +125 °C, 2 versions — GY-SHT40-D
Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino 11,12 € *Item no. F23107480About the product — Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino
Breadboard Cables Male-to-Male - 40 Pieces, 20 cm Breadboard Cables Male-to-Male - 40 Pieces, 20 cm 1,74 € *Item no. FUN-1010558About the product — Breadboard Cables Male-to-Male - 40 Pieces, 20 cm

All 3 parts together 17,28 € *

* All prices include statutory VAT, plus shipping costs.

Material

For this project, you need:

  • 1× microcontroller with a compatible USB cable
  • 1× SHT40, SHT35, or SHT20 sensor module
  • 4× male-to-female jumper wires

You only need one of the three sensors. The instructions show the right program for each one - the wiring is the same for all three.

The three sensors at a glance

Measuring temperature and humidity is one of the most common tasks for a microcontroller. The sensors in the SHT family mainly differ in their measurement accuracy and the library they require.

All three connect via the I2C bus. In addition to power and GND, they only need two more wires:

  • VCC → 3.3V
  • GND → GND
  • SDA → A4 (on the Arduino UNO)
  • SCL → A5 (on the Arduino UNO)

Be sure to check the labeling on your module. The pin order is not the same on all boards, and connecting the pins incorrectly can damage the sensor.

Small board with the SHT40 temperature and humidity sensor
The SHT40 sensor module
Small board with the SHT35 temperature and humidity sensor
The SHT35 sensor module
1 / 2

The SHT40

For the SHT40, you need the library Adafruit SHT4x. Install it via Sketch → Include Library → Manage Libraries or using the icon in the left-hand sidebar. Confirm the prompt about dependencies by clicking “Install all”.

After uploading, the temperature and humidity readings appear every two seconds in the Serial Monitor at 9600 baud.

The SHT40 can measure at three accuracy levels. A higher level provides more stable readings, but takes a little more time and power. SHT4X_HIGH_PRECISION is set in the sketch.

Circuit diagram with Arduino and SHT40 module
Connecting the SHT40 to an Arduino via the I2C bus
Arduino IDE Library Manager with the Adafruit SHT4x library
The “Adafruit SHT4x” library in the Library Manager
Serial monitor with temperature and humidity readings
Displaying temperature and humidity in the Serial Monitor
1 / 3
Sketch for the SHT40
// Benoetigte Bibliothek einbinden
#include "Adafruit_SHT4x.h"

// Name des Sensors (sht4)
Adafruit_SHT4x sht4 = Adafruit_SHT4x();

void setup()
{
  Serial.begin(9600);
  delay(1000);

  // SHT40 starten, im Fehlerfall Meldung anzeigen
  Serial.println("SHT40 Test");
  if (!sht4.begin()) Serial.println("Sensor SHT40 nicht gefunden");
  else Serial.println("Sensor SHT40 gefunden!");

  /*
    Genauigkeit bestimmen, moegliche Werte:
    SHT4X_LOW_PRECISION  (niedrig)
    SHT4X_MED_PRECISION  (mittel)
    SHT4X_HIGH_PRECISION (hoch)
  */
  sht4.setPrecision(SHT4X_HIGH_PRECISION);
}

void loop()
{
  // Sensorwerte erfassen (Luftfeuchtigkeit, Temperatur)
  sensors_event_t Luftfeuchtigkeit, Temperatur;
  sht4.getEvent(&Luftfeuchtigkeit, &Temperatur);

  // Gemessene Temperatur in String umwandeln und Punkt durch Komma ersetzen
  String gemesseneTemperatur = String(Temperatur.temperature);
  gemesseneTemperatur.replace(".", ",");
  Serial.println("Temperatur: " + gemesseneTemperatur + " Grad C");

  // Gemessene Luftfeuchtigkeit in String umwandeln und Punkt durch Komma ersetzen
  String gemesseneLuftfeuchtigkeit = String(Luftfeuchtigkeit.relative_humidity);
  gemesseneLuftfeuchtigkeit.replace(".", ",");
  Serial.println("Luftfeuchtigkeit: " + gemesseneLuftfeuchtigkeit + "%");

  Serial.println("------------------------");
  delay(2000);
}

The SHT20

The SHT20 is wired the same way as the SHT40, but needs a different library: SHT2x by Rob Tillaart. You can also find it in the Library Manager.

The sketch outputs temperature and humidity to two decimal places.

Library Manager of the Arduino IDE with the SHT2x library
The “SHT2x” library for the SHT20 in the Library Manager
Sketch for the SHT20
#include "SHT2x.h"

// sht dem I2C-Bus (Wire) zuordnen
SHT2x sht(&Wire);

void setup()
{
  Serial.begin(9600);
  delay(1000);

  // I2C-Bus starten
  Wire.begin();

  // SHT20 starten, im Fehlerfall Meldung anzeigen
  Serial.println("SHT20 Test");
  if (!sht.begin()) Serial.println("Sensor SHT20 nicht gefunden");
  else Serial.println("Sensor SHT20 gefunden!");
}

void loop()
{
  // Sensor lesen
  sht.read();

  // Gelesene Temperatur mit 2 Nachkommastellen in einen String umwandeln
  String gemesseneTemperatur = String(sht.getTemperature(), 2);
  gemesseneTemperatur.replace(".", ",");
  Serial.println("Temperatur: " + gemesseneTemperatur + " Grad C");

  // Gelesene Luftfeuchtigkeit mit 2 Nachkommastellen in einen String umwandeln
  String gemesseneLuftfeuchtigkeit = String(sht.getHumidity(), 2);
  gemesseneLuftfeuchtigkeit.replace(".", ",");
  Serial.println("Luftfeuchtigkeit: " + gemesseneLuftfeuchtigkeit + "%");

  Serial.println("------------------------");
  delay(2000);
}

The SHT35

The SHT35 is part of the SHT3x sensor series. For it, use the library Adafruit SHT31 – it supports the entire SHT3x series, including the SHT35.

The wiring is the same as for the other two sensors.

If the sketch reports “Sensor SHT35 not found” even though everything is wired correctly, your module is probably using I2C address 0x45 instead of 0x44. In that case, write sht31.begin(0x45) on the corresponding line.

Library Manager of the Arduino IDE with the Adafruit SHT31 library
The “Adafruit SHT31” library covers the entire SHT3x series.
Sketch for the SHT35
// Benoetigte Bibliothek einbinden
#include "Adafruit_SHT31.h"

// Name des Sensors (sht31)
Adafruit_SHT31 sht31 = Adafruit_SHT31();

void setup()
{
  Serial.begin(9600);
  delay(1000);

  // SHT35 starten, im Fehlerfall Meldung anzeigen.
  // Die Standardadresse ist 0x44. Manche Module sitzen auf 0x45,
  // dann lautet die Zeile: sht31.begin(0x45)
  Serial.println("SHT35 Test");
  if (!sht31.begin()) Serial.println("Sensor SHT35 nicht gefunden");
  else Serial.println("Sensor SHT35 gefunden!");
}

void loop()
{
  // Sensorwerte erfassen und Punkt durch Komma ersetzen
  String Temperatur = String(sht31.readTemperature());
  String Luftfeuchtigkeit = String(sht31.readHumidity());
  Temperatur.replace(".", ",");
  Luftfeuchtigkeit.replace(".", ",");

  Serial.println("Temperatur: " + Temperatur + " Grad C");
  Serial.println("Luftfeuchtigkeit: " + Luftfeuchtigkeit + "%");

  Serial.println("------------------------");
  delay(2000);
}
Rate the instructions

Comments

Ask a question or share an experience

Contact us if something doesn’t work. Please include your board and the error message.

We review every comment before publication. Your email address remains with us.