아두이노 - 온습도 센서 - OLED 디스플레이 | Arduino - Temperature Humidity Sensor - OLED Display

이 튜토리얼에서는 다음을 배우려고 합니다:

Arduino DHT11/DHT22 temperature humidity sensor OLED display

준비물

1×Arduino Uno Amazon
1×USB 2.0 cable type A/B 쿠팡 | Amazon
1×SSD1306 I2C OLED Display 128x64 Amazon
1×DHT11 Temperature and Humidity Sensor Amazon
1×Jumper Wires Amazon
1×(Optional) 9V Power Adapter for Arduino Amazon
1×(Recommended) Screw Terminal Block Shield for Arduino Uno 쿠팡 | Amazon
1×(Optional) Transparent Acrylic Enclosure For Arduino Uno Amazon

You can use DHT22 sensor instead of DHT11 sensor.

공개: 이 섹션에서 제공된 링크 중 일부는 제휴 링크입니다. 이 링크를 통해 구매한 경우 추가 비용없이 수수료를 받을 수 있습니다. 지원해 주셔서 감사합니다.

OLED 디스플레이, DHT11 및 DHT22 온습도 센서에 대하여

OLED 디스플레이, DHT11 및 DHT22 온습도 센서(핀 배치, 작동 방식, 프로그래밍 방법 등)에 대해 모르신다면, 다음 튜토리얼에서 알아보세요:

선연결

아두이노 - DHT11 모듈 LCD 배선

Arduino DHT11 Sensor OLED Wiring Diagram

이 이미지는 Fritzing을 사용하여 만들어졌습니다. 이미지를 확대하려면 클릭하세요.

아두이노 - DHT22 모듈 LCD 배선

Arduino DHT22 Sensor OLED Wiring Diagram

이 이미지는 Fritzing을 사용하여 만들어졌습니다. 이미지를 확대하려면 클릭하세요.

아두이노 코드 - DHT11 센서 - OLED

/* * 이 Arduino 코드는 newbiely.kr 에서 개발되었습니다 * 이 Arduino 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/arduino/arduino-temperature-humidity-sensor-oled-display */ #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <DHT.h> #define SCREEN_WIDTH 128 // OLED 디스플레이 폭, 픽셀 단위 #define SCREEN_HEIGHT 64 // OLED 디스플레이 높이, 픽셀 단위 #define DHTPIN 2 // DHT11 센서에 연결된 핀 #define DHTTYPE DHT11 Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // I2C에 연결된 SSD1306 디스플레이 객체 생성 DHT dht(DHTPIN, DHTTYPE); String displayString; void setup() { Serial.begin(9600); // 128x64용 주소 0x3C로 OLED 디스플레이 초기화 if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); while (true); } delay(2000); // 초기화 대기 oled.clearDisplay(); // 디스플레이 클리어 oled.setTextSize(2); // 텍스트 크기 oled.setTextColor(WHITE); // 텍스트 색상 oled.setCursor(0, 10); // 디스플레이 위치 dht.begin(); // DHT11 온도 및 습도 센서 초기화 displayString.reserve(10); // String 사용 시 메모리 조각화 방지 } void loop() { float humi = dht.readHumidity(); // 습도 읽기 float tempC = dht.readTemperature(); // 온도 읽기 // 읽기 실패 여부 확인 if (isnan(humi) || isnan(tempC)) { displayString = "Failed"; } else { displayString = String(tempC, 1); // 소수점 한 자리 displayString += "°C"; displayString += String(humi, 1); // 소수점 한 자리 displayString += "%"; } Serial.println(displayString); // 시리얼 모니터에 섭씨 온도 출력 oledDisplayCenter(displayString); // OLED에 온도 및 습도 표시 } void oledDisplayCenter(String text) { int16_t x1; int16_t y1; uint16_t width; uint16_t height; oled.getTextBounds(text, 0, 0, &x1, &y1, &width, &height); // 가로 및 세로 중앙에 표시 oled.clearDisplay(); // 디스플레이 클리어 oled.setCursor((SCREEN_WIDTH - width) / 2, (SCREEN_HEIGHT - height) / 2); oled.println(text); // 표시할 텍스트 oled.display(); }

사용 방법

  • PC에서 Arduino IDE를 엽니다.
  • Arduino IDE의 왼쪽 바에 있는 Libraries 아이콘으로 이동합니다.
  • “SSD1306”을 검색한 다음, Adafruit의 SSD1306 라이브러리를 찾습니다.
  • 라이브러리를 설치하려면 Install 버튼을 클릭합니다.
Arduino OLED library
  • 다른 라이브러리 의존성을 설치하라는 요청을 받게 됩니다.
  • 모든 라이브러리 의존성을 설치하려면 Install All 버튼을 클릭하세요.
Arduino Adafruit GFX sensor library
  • “DHT”를 검색한 다음, Adafruit의 DHT 센서 라이브러리를 찾으세요.
  • 라이브러리를 설치하려면 Install 버튼을 클릭하세요.
Arduino DHT sensor library
  • 다른 라이브러리 의존성을 설치하라는 요청을 받게 됩니다
  • 모든 라이브러리 의존성을 설치하려면 Install All 버튼을 클릭하세요.
Arduino Adafruit Unified sensor library
  • 위의 코드를 복사하고 Arduino IDE로 열기
  • Arduino IDE에서 Upload 버튼을 클릭하여 Arduino에 코드를 업로드하기
  • 센서를 뜨거운 물과 차가운 물에 넣거나, 손으로 센서를 잡기
  • OLED와 시리얼 모니터에서 결과 보기

※ NOTE THAT:

해당 코드는 OLED 디스플레이에서 텍스트를 자동으로 수평 및 수직 중앙 정렬합니다.

아두이노 코드 - DHT22 센서 - OLED

/* * 이 Arduino 코드는 newbiely.kr 에서 개발되었습니다 * 이 Arduino 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/arduino/arduino-temperature-humidity-sensor-oled-display */ #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <DHT.h> #define SCREEN_WIDTH 128 // OLED 디스플레이 너비, 픽셀 단위 #define SCREEN_HEIGHT 64 // OLED 디스플레이 높이, 픽셀 단위 #define DHTPIN 2 // DHT22 센서에 연결된 핀 #define DHTTYPE DHT22 Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // I2C에 연결된 SSD1306 디스플레이 객체 생성 DHT dht(DHTPIN, DHTTYPE); String displayString; void setup() { Serial.begin(9600); // 주소 0x3C에 대한 128x64 OLED 디스플레이 초기화 if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); while (true); } delay(2000); // 초기화를 위한 대기 oled.clearDisplay(); // 디스플레이 지우기 oled.setTextSize(2); // 텍스트 크기 oled.setTextColor(WHITE); // 텍스트 색상 oled.setCursor(0, 10); // 디스플레이할 위치 설정 dht.begin(); // 온도 및 습도 센서인 DHT22 초기화 displayString.reserve(10); // String 사용 시 메모리 단편화 방지를 위해 } void loop() { float humi = dht.readHumidity(); // 습도 읽기 float tempC = dht.readTemperature(); // 온도 읽기 // 읽기 실패 여부 확인 if (isnan(humi) || isnan(tempC)) { displayString = "Failed"; } else { displayString = String(tempC, 1); // 소수점 아래 한 자리 displayString += "°C"; displayString += String(humi, 1); // 소수점 아래 한 자리 displayString += "%"; } Serial.println(displayString); // 시리얼 모니터에 섭씨 온도 출력 oledDisplayCenter(displayString); // OLED에 온도와 습도 표시 } void oledDisplayCenter(String text) { int16_t x1; int16_t y1; uint16_t width; uint16_t height; oled.getTextBounds(text, 0, 0, &x1, &y1, &width, &height); // 수평 및 수직 중앙에 표시 oled.clearDisplay(); // 디스플레이 지우기 oled.setCursor((SCREEN_WIDTH - width) / 2, (SCREEN_HEIGHT - height) / 2); oled.println(text); // 표시할 텍스트 oled.display(); }

※ NOTE THAT:

DHT11과 DHT22를 위한 코드는 코드 한 줄을 제외하고 동일합니다. DHT11과 DHT22를 위한 라이브러리는 같습니다.

동영상

비디오 제작은 시간이 많이 걸리는 작업입니다. 비디오 튜토리얼이 학습에 도움이 되었다면, YouTube 채널 을 구독하여 알려 주시기 바랍니다. 비디오에 대한 높은 수요가 있다면, 비디오를 만들기 위해 노력하겠습니다.

※ OUR MESSAGES

  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!