Example Code for Arduino-Read Data

Record program and read the current temperature and humidity data.

Hardware Preparation

  • DFRduino UNO R3 (or similar) x 1
  • SHTC3 Digital Temperature and Humudity Sensor x 1#
  • M-M/F-M/F-F Jumper wires

Software Preparation

Wiring Diagram

Connection diagram

Sample Code

    /**
   * @brief  初始化Wire,并且对传感器进行软件复位,然后使传感器进入睡眠状态,需要使用时,在对其进行唤醒。
   */
  void begin();

  /**
   * @brief  获取温度数据
   * @return 温度值,单位:摄氏度
   */
  float getTemperature();

  /**
   * @brief  获取湿度数据
   * @return 湿度值,单位:%RH
   */
  float getHumidity();

  /**
   * @brief  获取温湿度数据
   * @param   tem  存放温度数据的引用
   * @param   hum  存放湿度数据的引用
   */
  void  getTemHum(float &tem, float &hum);

  /**
   * @brief  设置传感器工作模式
   * @param  mode  传感器的工作模式
   * @n            SHTC3:
   * @n                    PRECISION_HIGH_CLKSTRETCH_ON                              Clock Stretching Enabled 
   * @n                    PRECISION_HIGH_CLKSTRETCH_OFF                             Clock Stretching Disabled 
   * @n                    PRECISION_LOW_CLKSTRETCH_ON                               Clock Stretching Enabled & Low Power
   * @n                    PRECISION_LOW_CLKSTRETCH_OFF                              Clock Stretching Disabled & Low Power
   * @n            SHT40:
   * @n                    PRECISION_HIGH_HEATER_OFF                                 measure T & RH with high precision (high repeatability) 
   * @n                    PRECISION_MID_HEATER_OFF                                  measure T & RH with medium precision (medium repeatability)
   * @n                    PRECISION_LOW_HEATER_OFF                                  measure T & RH with lowest precision (low repeatability) 
   * @n                    PRECISION_HIGH_HEATER_1S                                  activate highest heater power & high precis. meas. (typ. 200mW @ 3.3V) for 1s 
   * @n                    PRECISION_HIGH_HEATER_100MS                               activate highest heater power & high precis. meas. (typ. 200mW @ 3.3V) for 0.1s
   * @n                    PRECISION_MID_HEATER_1S                                   activate medium heater power  & high precis. meas. (typ. 110mW @ 3.3V) for 1s 
   * @n                    PRECISION_MID_HEATER_100MS                                activate medium heater power  & high precis. meas. (typ. 110mW @ 3.3V) for 0.1s 
   * @n                    PRECISION_LOW_HEATER_1S                                   activate lowest heater power  & high precis. meas. (typ. 20mW @ 3.3V) for 1s 
   * @n                    PRECISION_LOW_HEATER_100MS                                activate lowest heater power  & high precis. meas. (typ. 20mW @ 3.3V) for 0.1s 
   */
   void setMode(uint16_t mode) ;

  /**
   * @brief  获取传感器的唯一标识符
   * @return 获取成功返回传感器的唯一标识符,失败返回0
   */
   uint32_t getDeviceID();

  /**
   * @brief  software reset
   */
   void softwareReset() ;

  /**
   * @brief  Obtain raw data of temperature and humidity
   * @param  temp Pointer to the address of the original value of the temperature
   * @param  hun   Pointer to the address of the original value of the humidity
   * @return Is the data obtained correct? return true  The data is correct ; return false  The data  is incorrect
   */
   bool getTandRHRawData(uint16_t *temp, uint16_t *hum);

  /**
   * @brief  当传感器处于睡眠模式时,在进行任何进一步的通信之前,需要唤醒
   */
  void wakeup();

  /**
   * @brief  设置传感器为睡眠模式,设置后,在未唤醒前无法进行数据采集
   */
  void sleep();

Result

Open the serial port to display the current temperature and humidity data.

Result

Was this article helpful?

TOP