Day3 01-dht11

2024. 2. 4. 15:39arduino

  • dht11에서 정보를 받아와 OLED에 출력하는 코드
// Humid : 습도값
// Temp  : 온도

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128  // OLED display width, in pixels
#define SCREEN_HEIGHT 64  // OLED display height, in pixels

#define OLED_RESET -1        // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C  ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

int32_t count = 10;

#include "DHT.h"

#define DHTPIN 2     // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11   // DHT 11
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  // put your setup code here, to run once:
  oled.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
  delay(2000);

  oled.clearDisplay();
  oled.setTextSize(1);
  oled.setTextColor(WHITE);

  dht.begin();

}


void loop() {
  delay(2000);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();

  Serial.print(F("Humidity: "));
  Serial.print(h);
  Serial.print(F("%  Temperature: "));
  Serial.print(t);
  Serial.println(F("°C "));

  // put your main code here, to run repeatedly:
  oled.clearDisplay();
  oled.setTextSize(1);
  oled.setCursor(15, 5);
  oled.println("<< Smart House >> ");


  oled.setTextSize(1);
  oled.setCursor(15, 25);
  oled.println("Humid : " + String(h) + " %");

  oled.setCursor(15, 35);
  oled.println("Temp  : " + String(t) + String(" C "));
  oled.display();

}
// int32_t count = 10;

'arduino' 카테고리의 다른 글

Day4 02-encoder  (0) 2024.02.04
Day4 01-debounce  (0) 2024.02.04
Day2 06_Debounce  (0) 2024.01.18
Day2 05_IR  (0) 2024.01.18
Day2 04_sonic  (0) 2024.01.18