Example Code for Arduino-Read Sensor Data

Last revision 2025/12/17

Download the program to UNO, open the serial monitor to check temperature and humidity. Read ambient temperature and relative humidity and print them to serial port.

Hardware Preparation

  • DFRduino UNO R3 (or similar) x 1
  • SEN0497 Temperature and Humidity Sensor - DHT20 ×1
  • Gravity 4Pin Sensor Connector ×1

Software Preparation

Wiring Diagram

Other Preparation Work

Sample Code

/*!
 *@file getData.ino
 *@brief Read ambient temperature and relative humidity and print them to serial port.
 *@copyright   Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
 *@licence     The MIT License (MIT)
 *@author [fengli]([email protected])
 *@version  V1.0
 *@date  2021-6-24
 *@get from https://www.dfrobot.com
 *@https://github.com/DFRobot/DFRobot_DHT20
*/

#include <DFRobot_DHT20.h>
/*!
 * @brief Construct the function
 * @param pWire IC bus pointer object and construction device, can both pass or not pass parameters, Wire in default.
 * @param address Chip IIC address, 0x38 in default.
 */
DFRobot_DHT20 dht20;
void setup(){

  Serial.begin(115200);
  //Initialize sensor
  while(dht20.begin()){
    Serial.println("Initialize sensor failed");
    delay(1000);
  }
}

void loop(){
  //Get ambient temperature
  Serial.print("temperature:"); Serial.print(dht20.getTemperature());Serial.print("C");
  //Get relative humidity
  Serial.print("  humidity:"); Serial.print(dht20.getHumidity()*100);Serial.println(" %RH");
  
  delay(1000);

}

Result

Open the serial monitor and set the baud rate to 115000, then you can see the temperature and humidity data.

Additional Information

You can also use the module with Gravity I/O Expansion Shield, which can complete the project prototype more conveniently and quickly.

Was this article helpful?

TOP