AHT20은 아두이노 환경에서 사용할 수 있는 디지털 온도 및 습도 센서입니다. 

 

AHT20 특징:

작동 전압: 2.2 - 5.5 V
작동 온도: -40 °C ~ 85 °C
습도 정확도: 일반적인 ± 2%
습도 해상도: 0.024%
온도 정확도: 일반적인 ± 0.3 ° C
온도 해상도: 일반적인 0.01 °C

AHT1x와 AHT2x 차이점:
- AHT1x 작동전압: 1.8 - 3.6V
- AHT1x 작동전압: 2.2 - 5.5V
- AHT2x 체크섬 지원(CRC)

 

Data Sheet AHT20.pdf
1.45MB

 

 

ESP32와 AHT20 연결방법

VIN -> 3.3V

SCL -> GPIO17

SDA -> GPIO5

VIN-3.3V/GND-GND/SCL-GPIO17/SDA-GPIO5
ESP32_AHT20.zip
0.00MB

#include <stdint.h>
#include <math.h>
#include <Arduino.h>
#include <Wire.h>

#define I2C_SDA 5
#define I2C_SCL 17
#define AHT20_ADD 0x38 // The device's I2C address is either 0x38 or 0x39.

const uint8_t eSensorCalibrateCmd[3]  PROGMEM = {0xE1, 0x08, 0x00};
const uint8_t eSensorMeasureCmd[3]    PROGMEM = {0xAC, 0x33, 0x00};

bool AHT20_begin() {
  Wire.setPins(I2C_SDA, I2C_SCL);
  //Serial.println(F("setPins"));
  Wire.begin(); 
  //Serial.println(F("Wire.begin"));
  Wire.beginTransmission(AHT20_ADD);
  //Serial.println(F("AHT20_ADD"));
  Wire.write(eSensorCalibrateCmd, 3);
  //Serial.println(F("command"));
  Wire.endTransmission();
  delay(100);
  Wire.requestFrom(AHT20_ADD, 1);
  unsigned char result = 0;
  result = Wire.read();
  if((result&0x68) == 0x08) { 
    Serial.println("Init AHT10/20 Sucess."); 
    return true; 
  } else { 
    Serial.println("Init AHT10/20 Failure."); 
    return false; 
  } 
}

uint8_t vTaskCount = 0;
DRAM_ATTR float temp_i2c = 0;
DRAM_ATTR float reh_i2c = 0;

// Specify the constants for water vapor and barometric pressure.
#define WATER_VAPOR 17.62f
#define BAROMETRIC_PRESSURE 243.5f

float GetDewPoint(float humidity, float temperature) {
  // Calculate the intermediate value 'gamma'
  float gamma = log(humidity / 100) + WATER_VAPOR * temperature / (BAROMETRIC_PRESSURE + temperature);
  // Calculate dew point in Celsius
  float dewPoint = BAROMETRIC_PRESSURE * gamma / (WATER_VAPOR - gamma);
  return dewPoint;
}

void readAHT20sensor() { // IRAM_ATTR 
  unsigned long result, temp[6] = { 255, };
  Wire.beginTransmission(AHT20_ADD);
  Wire.write(eSensorMeasureCmd, 3);
  Wire.endTransmission(); 
  vTaskDelay(40);  // 최소 35mil 필요 -> 연결 환경에 따라 조절
  vTaskCount += 2; // vTaskDelay 40밀리초 반영
  Wire.requestFrom(AHT20_ADD, 6); 
  uint8_t outCount = 0; bool ok = false;
  while(temp[0] == 255) {  
    for(unsigned char i = 0; i < 6; i++) { temp[i] = Wire.read(); }  //temp[0] = Wire.read();
    if (!bitRead(temp[0], 7)) { ok = true; break; } // If the status bit [Bit7] is 0, it means that the data can be read normally
    else { temp[0] = 255; } //return ok; }
    outCount++; if (outCount > 60) { break; }
    Wire.requestFrom(AHT20_ADD, 6); 
    vTaskDelay(1); 
  }
  if (ok) { // {status, RH, RH, RH+T, T, T, CRC*}, *CRC for AHT2x only
    result = ((temp[1] << 16) | (temp[2] << 8) | temp[3]) >> 4;
    reh_i2c = float(result) * 100 / 1048576;
    Serial.print("Humi: "); Serial.println(reh_i2c);
    result = ((temp[3] & 0x0F) << 16) | (temp[4] << 8) | temp[5];
    temp_i2c = ((200 * float(result)) / 1048576) - 50;
    Serial.print("Temp: "); Serial.println(temp_i2c);
    Serial.print("DewPoint: "); Serial.println(GetDewPoint(reh_i2c, temp_i2c));
  }
}

void SENSOR(void *pvParameters) { // esp32 example FreeRTOS
  (void) pvParameters;
  for (;;) {
    if (vTaskCount >= 25) { //0.5초
      vTaskCount = 0;
      readAHT20sensor(); 
    }
    vTaskDelay(20); 
    vTaskCount++;
  }
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  delay(1000);
  bool AHTOK = AHT20_begin();
  if (!AHTOK) { delay(10); AHTOK = AHT20_begin(); }
  if (AHTOK) {
    readAHT20sensor(); 
  }
  xTaskCreate(SENSOR, "SENSOR", 4096, NULL, 1, NULL); // Priority 0 being the lowest.
}

void loop() {
  // put your main code here, to run repeatedly:

}

 

시리얼 모니터 출력

 

참조 라이브러리: https://github.com/enjoyneering/AHTxx

 

주요 코드 내용

I2C 핀맵 설정: ESP32 GPIO 번호 지정

#define I2C_SDA 5
#define I2C_SCL 17

 

ESP32 FreeRTOS의 xTask 사용

I2C를 이용하여 AHT20센서와 통신을 하기위해서 반드시 응답 대기 시간이 필요하게 됩니다. 응답 대기 시간으로 아두이노의 delay()함수를 사용하지 않기 위해서는 xTask를 설정하고 vTaskDelay를 사용해야 합니다. 

xTask 할당 메모리는 4096 또는 2048등으로 변경하실 수 있습니다. 

 

https://s.click.aliexpress.com/e/_DBK9uAr

 

AHT10 AHT20 AHT21 High Precision Digital Temperature and Humidity Sensor Measurement Module I2C Communication Replace SHT20 - Al

Smarter Shopping, Better Living! Aliexpress.com

www.aliexpress.com

AHT20 센서 모듈 상세

 

 

반응형

+ Recent posts