연구노트/아두이노

아두이노와 AM2315 온습도 측정!(I2C 통신, AM2315 온습도 센서)

생존형개발자뀨 2020. 12. 11. 01:47

안녕하세요 뀨입니다.

오늘은 AM2315 온습도 센서를 아두이노와 연결해보겠습니다!

AM2315I2C 통신을 이용하는 온습도 센서입니다.

엄청 옛날에 사둔거라 그때 가격은 기억이 나지 않지만 지금은 검색해보니 약 4만원정도에 팔고있네요. 

 

AM2315 소개

AM2315 온습도 센서
AM2315 인터페이스

AM2315는 4개의 선이 나오고 각선들은 전원과 통신을 담당하게 됩니다.(요런 인터페이스 사진은 AM2315에 데이터시트를 보면 나옵니다.)

  • VDD, GND 전원 / SDA, SCL I2C 통신

AM2315.pdf
2.92MB

아두이노 회로 연결

아두이노 우노R3 I2C 포트

아두이노 우노는 2개의 I2C 포트가 존재합니다. 하지만 같은 버스로 연결되어 있기때문에 어느쪽에 연결하든 상관없습니다.

그리고 I2C 연결에는 SDA, SCL라인에 풀업을 연결해야합니다.

간단히 설명하면 I2C는 하나에 버스에 여러가지 센서를 연결할 수 있는데 각 센서들이 통신하는도중 신호의 교통사고를 막기위해 풀업을 달아준다고 합니다.

풀업으로 연결한 I2C

테스트는 10K 옴의 저항을 사용했습니다.

데이터시트 풀업 설명

소스코드

AM2315 라이브러리 설치

소스는 간단하게 개발하려면 Adafruit AM2315 라이브러리를 받으면 아주 간단하게 AM2315를 사용할 수 있습니다.

  • 설치 후 파일 - 예제 - Adafruit AM2315 - am2315test 실행
#include <Wire.h>
#include <Adafruit_AM2315.h>

/*************************************************** 
  This is an example for the AM2315 Humidity + Temp sensor

  Designed specifically to work with the Adafruit AM2315 Sensor 
  ----> https://www.adafruit.com/products/1293

  These displays use I2C to communicate, 2 pins are required to  
  interface
  Adafruit invests time and resources providing this open source code, 
  please support Adafruit and open-source hardware by purchasing 
  products from Adafruit!

  Written by Limor Fried/Ladyada for Adafruit Industries.  
  BSD license, all text above must be included in any redistribution
 ****************************************************/

// Connect RED of the AM2315 sensor to 5.0V
// Connect BLACK to Ground
// Connect WHITE to i2c clock
// Connect YELLOW to i2c data

Adafruit_AM2315 am2315;

void setup() {
  Serial.begin(9600);
  while (!Serial) {
    delay(10);
  }
  Serial.println("AM2315 Test!");

  if (! am2315.begin()) {	//AM2315 초기화
     Serial.println("Sensor not found, check wiring & pullups!");
     while (1);
  }
}

void loop() {
  float temperature, humidity;

  if (!am2315.readTemperatureAndHumidity(&temperature, &humidity)) {
    Serial.println("Failed to read data from AM2315");	//데이터 읽기 실패시
    return;
  }
  Serial.print("Temp *C: "); Serial.println(temperature);	//온도 출력
  Serial.print("Hum %: "); Serial.println(humidity);		//습도 출력

  delay(2000);
}

실행결과!!