본문 바로가기

카테고리 없음

(아두이노) AHT20 + BMP280 온도/습도/기압 센서

https://smartstore.naver.com/misoparts/products/9945203957

 

온도 습도 및 공기압 센서 AHT20 + BMP280 고정밀 디지털 온도 습도 공기압 모듈 : 알파마이크로

[알파마이크로] 빠른배송 전자부품전문몰. 아두이노, 라즈베리파이, 모듈, 키트 등 전자부품 최저가 판매

smartstore.naver.com

 

내가 구입한 칩의 I2C 주소는 i2c scanner로 확인했을 때,

AHT20의 주소는 0x38

BMP280의 주소는 0x77

ATH20용 라이브러리: Adafruit_ATHX0.h 

BMP280용 라이브러리: Adafruit BMP280 (예제 소스에서 기본 0x77이 아닌 다른 주소일 수도 있으므로 확인 및 수정)

아래는 adafruit_aht_test 예제임.

#include <Adafruit_AHTX0.h>

Adafruit_AHTX0 aht;

void setup() {
  Serial.begin(115200);
  Serial.println("Adafruit AHT10/AHT20 demo!");

  if (! aht.begin()) {
    Serial.println("Could not find AHT? Check wiring");
    while (1) delay(10);
  }
  Serial.println("AHT10 or AHT20 found");
}

void loop() {
  sensors_event_t humidity, temp;
  aht.getEvent(&humidity, &temp);// populate temp and humidity objects with fresh data
  Serial.print("Temperature: "); Serial.print(temp.temperature); Serial.println(" degrees C");
  Serial.print("Humidity: "); Serial.print(humidity.relative_humidity); Serial.println("% rH");

  delay(500);
}

다음으로, BMP280 기압 센서값을 읽어옴. Adafruit BMP280 예제 소소를 좀 수정해야 함. 이전에 올렸던 스케치와 동일. 다만 I2C 주소는 확인하고 수정해야 함.

#include <Wire.h>
#include <Adafruit_BMP280.h>

Adafruit_BMP280 bmp; // I2C

void setup() {
  Serial.begin(9600);
  while (!Serial) delay(100);
  Serial.println("BMP280 test");
  unsigned status = bmp.begin(0x77, BMP280_CHIPID);

  if (!status) {
    Serial.println("Could not find a valid BMP280 sensor, check wiring or try a different address!");
    while (1) delay(10);
  }

  // Default settings from datasheet
  bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,  // operating mode
                  Adafruit_BMP280::SAMPLING_X2,  // temp. oversampling
                  Adafruit_BMP280::SAMPLING_X16, // pressure oversampling
                  Adafruit_BMP280::FILTER_X16,   // filtering
                  Adafruit_BMP280::STANDBY_MS_500); // standby time
}

void loop() {
  Serial.print("Temperature : ");
  Serial.print(bmp.readTemperature());
  Serial.println(" *C");

  Serial.print("Pressure : ");
  Serial.print(bmp.readPressure());
  Serial.println(" Pa");

  Serial.print("Approx altitude : ");
  Serial.print(bmp.readAltitude(1006));  // for Bucheon city (2025.06.12) : 1013.9 (mb)
  Serial.println(" m");

  Serial.println();
  delay(2000);
}

기준 기압이 시간 별로 시시각각 변하니 확인 필요.

 

이 두 개를 합치는 것은 어렵지 않다. I2C 주소를 두 개 사용한다는 것, 즉 칩 두 개를 칩 하나로 퉁 친다는 것인데, 코드가 좀 길어질 뿐... 그리고 BMP280에서 얻는 온도와, AHT20에서 얻는 온도가 1도 이내에서 좀 차이가 있다. 많이 사용되는 DHT11 사이에도 오차가 있고, 특히 DHT11 몇 개를 테스트해보니 습도에서 상당히 서로 다른 값을 보여주고 있다. 습도를 정확하게 얻는 것은 좀 더 고민과 연구가 필요해 보인다. 다만 이 칩의 온도와 습도는 대략 쓸만한 수준인 것 같다. 특히 온도값 2개를 출력해서 참고하면 되는 거니까, 온도가 1개 출력되는 것보다 서로 다른 칩에서 2개 출력되는 게 더 좋아보인다.

// for AHT20 + BMP280 

#include <Adafruit_BMP280.h>
#include <Adafruit_AHTX0.h>

Adafruit_BMP280 bmp; // 0x77
Adafruit_AHTX0 aht;  // 0x38

void setup() {
  Serial.begin(9600);

  // for bmp280
  unsigned status = bmp.begin(0x77, BMP280_CHIPID);
  if (!status) {
    Serial.println("Could not find a valid BMP280 sensor, check wiring or try a different address!");
    while (1) delay(10);
  }
  //default settings from datasheet
  bmp.setSampling(Adafruit_BMP280::MODE_NORMAL, // operating mode
                  Adafruit_BMP280::SAMPLING_X2,  // temp, oversampling
                  Adafruit_BMP280::SAMPLING_X16, // pressure oversampling
                  Adafruit_BMP280::FILTER_X16,  // filtering
                  Adafruit_BMP280::STANDBY_MS_500);  // standby time

  // for aht20
  if (! aht.begin()) {
    Serial.println("Could not find AHT? Check wiring or try a different address!");
    while (1) delay(10);
  }
}

void loop() {
  // for bmp280
  Serial.print("BMP280 Temp: ");
  Serial.print(bmp.readTemperature());
  Serial.print(" *C\t");

  Serial.print("Pressure: ");
  Serial.print(bmp.readPressure());
  Serial.print(" Pa\t");

  Serial.print("Approx altitude: ");
  Serial.print(bmp.readAltitude(1006));
  Serial.println(" m");

  // for aht20
  sensors_event_t humidity, temp;
  aht.getEvent(&humidity, &temp);
  Serial.print("AHT20 Temp: "); Serial.print(temp.temperature); Serial.print(" degrees C\t");
  Serial.print("Humidity: "); Serial.print(humidity.relative_humidity); Serial.println("% rH");
  delay(1000);
}

 

* AHT20+BMP280, Wemos d1 mini, OLED에 출력하기

// for AHT20 + BMP280 

#include <Adafruit_BMP280.h>
#include <Adafruit_AHTX0.h>

Adafruit_BMP280 bmp; // 0x77
Adafruit_AHTX0 aht;  // 0x38


#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 display(128, 32, &Wire, -1);

void setup() {
  Serial.begin(9600);
  while (!Serial) delay(100);

  Serial.println("BMP280 test");
  unsigned status = bmp.begin(0x77); //, BMP280_CHIPID);
  if (!status) {
    Serial.println("Could not find a valid BMP280 sensor, check wiring or try a different address!");
    while (1) delay(10);
  }
  // Default settings from datasheet
  bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,  // operating mode
                  Adafruit_BMP280::SAMPLING_X2,  // temp. oversampling
                  Adafruit_BMP280::SAMPLING_X16, // pressure oversampling
                  Adafruit_BMP280::FILTER_X16,   // filtering
                  Adafruit_BMP280::STANDBY_MS_500); // standby time

  // for aht20
  if (! aht.begin()) {
    Serial.println("Could not find AHT? Check wiring or try a different address!");
    while (1) delay(10);
  }

  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }
  display.setTextSize(2);
  display.setTextColor(SSD1306_WHITE);
}

void loop() {
  // for bmp280
  int temp_bmp = bmp.readTemperature();
  Serial.print("Temperature : ");
  Serial.print(temp_bmp);
  Serial.println(" *C");

  int pressure_bmp = bmp.readPressure();
  Serial.print("Pressure : ");
  Serial.print(pressure_bmp);
  Serial.println(" Pa");

  int altitude_bmp = bmp.readAltitude(1006.4); // for Bucheon city (2025.06.12) : 1013.9 (mb)
  Serial.print("Approx altitude : ");
  Serial.print(altitude_bmp); 
  Serial.println(" m");
  Serial.println();
  
  // for aht20
  sensors_event_t humidity_aht, temp_aht;
  aht.getEvent(&humidity_aht, &temp_aht);
  Serial.print("AHT20 Temp: "); Serial.print(temp_aht.temperature); Serial.print(" degrees C\t");
  Serial.print("Humidity: "); Serial.print(humidity_aht.relative_humidity); Serial.println("% rH");
  
  display.clearDisplay();
  display.setCursor(0,0);             // Start at top-left corner
  display.print(temp_bmp);
  display.print(char(247));
  display.print("C ");
  display.print(altitude_bmp);
  display.println(" m");
  display.print(int(temp_aht.temperature));
  display.print(char(247));
  display.print("C ");
  display.print(int(humidity_aht.relative_humidity));
  display.println("%");

  display.display();

  delay(1000);
}