Example Code for Arduino-Read Temperature and Humidity

Last revision 2026/01/07

This blog post offers a step-by-step guide on how to use Arduino with the AHT20 sensor to measure temperature and humidity, complete with sample code, wiring instructions, and expected results displayed on the serial monitor.

Hardware Preparation

  • DFRduino UNO R3 (or similar) x 1
  • SEN0527 Fermion: AHT20 Temperature and Humidity Sensor x 1
  • M-M/F-M/F-F Jumper wires

Software Preparation

Wiring Diagram

UNO Board: 3V3 - connect to - AHT20:VCC
UNO Board: GND - connect to - AHT20:GND
UNO Board: SCL - connect to - AHT20:SCL
UNO Board: SDA - connect to - AHT20:SDA

Other Preparation Work

Sample Code

#include "DFRobot_AHT20.h"

DFRobot_AHT20 aht20;

void setup(){
  Serial.begin(115200);
  uint8_t status;
  while((status = aht20.begin()) != 0){
    Serial.print("AHT20 sensor initialization failed. error status : ");
    Serial.println(status);
    delay(1000);
  }
}

void loop(){
  if(aht20.startMeasurementReady(/* crcEn = */true)){
    Serial.print(aht20.getTemperature_C());
    Serial.print(" C, ");
    // Get temp in Fahrenheit (F)
    Serial.print(aht20.getTemperature_F());
    Serial.print(" F\t");
    Serial.print(aht20.getHumidity_RH());
    Serial.println(" %RH");
    delay(5000);
  }
}

Result

As shown in the figure below, the temperature values in ℃ and ℉, and humidity values are displayed in the serial monitor.

Was this article helpful?

TOP