Example Code for Arduino-Read Sensor Data via I2C

Last revision 2026/01/21

Connect the wires according to the wring diagram; Burn the program into Arduino UNO via Arduino IDE, and open the serial monitor to view the current temperature data.

Hardware Preparation

  • DFRduino UNO R3 x 1
  • SEN0434 Fermion STS35 High Accuracy Digital Temperature Sensor x 1
  • Jumper wires

Software Preparation

Wiring Diagram

Other Preparation Work

  • Connect the module to Arduino according to the wiring diagram above, you can also use it with Gravity I/O Sensor Expansion Board V7.1 to build the project more easily and quickly.
  • The default I2C address is 0x4B. If you want to modify the I2C address, you can connect the ADDR pin on the module to GND and change letter B of the code DFRobot_STS3X sts(&Wire, STS3X_IIC_ADDRESS_B) to letter A.
  • Open Arduino IDE and upload the following code to Arduino UNO.
  • Open the serial monitor of Arduino IDE, set the baud rate to 9600, and observe the temperature printing result.

Sample Code

/*!
 * @file getTemperature.ino
 * @brief Enable the period measurement mode of the sensor (set the period measurement frequency, equivalent to sending a command to enable the period measurement mode),
 * @n Get the measured temperature data in the sensor period measurement mode
 * 
 * @copyright    Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
 * @licence     The MIT License (MIT)
 * @author [LuoYufeng]([email protected])
 * @version  V1.0
 * @date  2021-9-01
 * @url https://github.com/DFRobot/DFRobot_STS3X
 */
#include "DFRobot_STS3X.h"

/*!
 * Determine the I2C address based on pull high or pull low of ADDR pin
 * ADDR pin pull low: STS3X_IIC_ADDRESS_A   0x4A
 * ADDR pin pull high (by default): STS3X_IIC_ADDRESS_B   0x4B
 */
DFRobot_STS3X sts(&Wire, STS3X_IIC_ADDRESS_B);


void setup(void)
{
    Serial.begin(9600);
    /*Wait for the chip to be initialized completely, and then exit*/
    while(sts.begin() != true){
        Serial.println("Failed to init chip, please check if the chip connection is fine. ");
        delay(1000);
    }

    /*!
     *@brief Set measurement frequency
     *@param freq: Select e2S, e1Hz, e2Hz, e4Hz and e10Hz modes in the enum variable eFreq_t
     */
    sts.setFreq(sts.e10Hz);

}

void loop() {
    Serial.print(sts.getTemperaturePeriodC());
    Serial.println(" ℃");
    delay(1000);
}

Result

Additional Information

Repeat Mode Select

  • Adjust data repeat mode by changing the parameters in parentheses.
  • Different modes correspond to different measurement times. Measurement time: the time required for collecting samples to output the measured data once.
Mode Measurement Time
High 12.5 ms
Medium 4.5 ms
Low 2.5 ms
    /*!
     *@brief Set repeat mode
     *@param code: Select eHigh, eMedium and eLow modes in the enum variable eCode_t
     */
    sts.setRepeat(sts.eHigh);

Measurement Rate Select

  • Adjust data measurement rate by changing the parameters in parentheses.
  • Different modes correspond to different measurement rates.
Mode Measurement Rate
2S 0.5Hz
1Hz 1Hz
2Hz 2Hz
4Hz 4Hz
10Hz 10Hz
    /*!
     *@brief Set measurement frequency
     *@param freq: Select e2S, e1Hz, e2Hz, e4Hz and e10Hz modes in the enum variable eFreq_t
     */
    sts.setFreq(sts.e10Hz);

Was this article helpful?

TOP