Example Code for Arduino-Read Temperature & Humidity

An example code and setup guide to measure temperature and humidity using an Arduino board and the Gravity Analog SHT30 sensor, including necessary hardware, software preparation, wiring, and sample code, with results displaying readings every 2 seconds.

Hardware Preparation

  • DFRduino UNO R3 (or similar) x 1
  • Gravity: Analog SHT30 Temperature & Humidity Sensor x 1
  • Gravity 3P sensor wire (or Dupont wires) x 1

Software Preparation

Wiring Diagram

Sample Code

/**************************************************************************/
/*
    @file       SHT30_TempRH_Read.ino
    @author     Henry (DFRobot)
    @version    V1.0
    @date       2019-08-12
    @copyright  Copyright (c) 2010 DFRobot Co.Ltd (https://www.dfrobot.com)
    @licence    The MIT License (MIT)
    @breif      This example read and print the temperature and relative humidity.

    This demo and related libraries are for DFRobot Gravity: Analog SHT30 Temperature & Humidity Sensor
    Check out the links below for tutorials and connection diagrams
    Product(CH): https://www.dfrobot.com.cn/
    Product(EN): https://www.dfrobot.com/

*/
/**************************************************************************/

// VREF: Analog reference
// For Arduino UNO, Leonardo and mega2560, etc. change VREF to 5
// For Arduino Zero, Due, MKR Family, ESP32, etc. 3V3 controllers, change VREF to 3.3
#define VREF 5.0
#define TEMPERATURE_PIN A1
#define HUMIDITY_PIN A0
#define ADC_RESOLUTION 1024

float Tc, Tf, RH, analogVolt;

void setup() {
  Serial.begin(115200);
  Serial.println("SHT30 Starts up.");
}

void loop() {
  
  analogVolt = (float)analogRead(TEMPERATURE_PIN) / ADC_RESOLUTION * VREF;
  // Convert voltage to temperature (℃, centigrade)
  Tc = -66.875 + 72.917 * analogVolt;
  // Convert voltage to temperature (°F, fahrenheit )
  Tf = -88.375 + 131.25 * analogVolt;
  Serial.print("Termperature:" );
  Serial.print(Tc, 1);
  Serial.print(" C / " );
  Serial.print(Tf, 1);
  Serial.println(" F" );

  analogVolt = (float)analogRead(HUMIDITY_PIN) / ADC_RESOLUTION * VREF;
  // Convert voltage to relative humidity (%)
  RH = -12.5 + 41.667 * analogVolt;

  Serial.print("Humidity:" );
  Serial.print(RH, 1);
  Serial.println(" %RH" );

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

Result

  • Temperature or relative humidity is printed every 2s.

Was this article helpful?

TOP