HOW TO INTERFACE AHT20 WITH ARDUINO AND OLED

CIRCUIT DIAGRAM

ARDUINO PROGRAM

#include <Arduino.h>
#include <U8g2lib.h>
#include "DFRobot_AHT20.h"
DFRobot_AHT20 aht20;
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
 
U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE, /* clock=*/ SCL, /* data=*/ SDA);   // pin remapping with ESP8266 HW I2C

void setup(void) {
  u8g2.begin();
  aht20.begin();
}

void loop(void) {
   if(aht20.startMeasurementReady(/* crcEn = */true)){
    Serial.print("temperature(-40~85 C): ");
    // Get temp in Celsius (℃), range -40-80℃
    Serial.print(aht20.getTemperature_C());
   
  
    Serial.print("humidity(0~100): ");
    // Get relative humidity (%RH), range 0-100℃
    Serial.print(aht20.getHumidity_RH());
    Serial.println(" %RH");
    delay(10);
   }
  u8g2.clearBuffer();
  u8g2.setFont(u8g2_font_ncenB08_tr);
  u8g2.setCursor(0, 10);
  u8g2.print("TEMP: ");
  u8g2.setCursor(60, 10);
  u8g2.print(" ");
  u8g2.println(aht20.getTemperature_C());
  u8g2.setCursor(90, 10);
  u8g2.print("C");
  u8g2.setCursor(0, 30);
  u8g2.print("HUMID: ");
  u8g2.setCursor(60, 30);
  u8g2.print(" ");
  u8g2.println(aht20.getHumidity_RH());
  u8g2.setCursor(90, 30);
  u8g2.print("%");
  u8g2.sendBuffer();				
  delay(10);  
}

Leave a comment