아두이노 dht센서 값 oled에 나타내기
-
게시물 수정 , 삭제는 로그인 필요
아두이노 나노를 이용해 DHT22센서의 값을 읽어서 0.91인치 OLED 모듈에 나타내려고 합니다. 상단에 작게 Temperature: 을 쓰고 그 아래 좀 더 큰 글씨로 온도를 쓰고, 몇 초 뒤에 다시 상단에 Humidity: 를 쓰고 아래 습도와 % 기호를 쓰려고 합니다. 코드가 좀 길어 보이는데 중간에 if 문 부분은 led에 관한 내용이니 그냥 넘어가시면 됩니다. 코딩에 성공만 하면 내공 최대한 많이 드리겠습니다..
#include<SPI.h>
#include<Wire.h>
#include<Adafruit_GFX.h>
#include<Adafruit_SSD1306.h>
#include<Adafruit_Sensor.h>
#include<DHT.h>
#include<Adafruit_I2CDevice.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
#define OLED_RESET 4
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
#define RED 9
#define GREEN 10
#define BLUE 11
float h;
float t;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
dht.begin();
delay(2000);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
display.begin();
display.clearDisplay();
}
}
void loop() {
// put your main code here, to run repeatedly:
float h = dht.readHumidity();
float t = dht.readTemperature();
Serial.print("humidity");
Serial.print(h);
Serial.print("Temperature");
Serial.print(t);
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
}
else if (h<20) {
analogWrite(RED,(225));
analogWrite(GREEN,(0));
analogWrite(BLUE,(0));
}
else if (h<40) {
analogWrite(RED,(225));
analogWrite(GREEN,(225));
analogWrite(BLUE,(0));
}
else if (h<60) {
analogWrite(RED,(0));
analogWrite(GREEN,(225));
analogWrite(BLUE,(0));
}
else if (h<80) {
analogWrite(RED,(0));
analogWrite(GREEN,(225));
analogWrite(BLUE,(225));
}
else if (h<=100) {
analogWrite(RED,(0));
analogWrite(GREEN,(0));
analogWrite(BLUE,(225));
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.print(F("Temperature: "));
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(10, 0);
display.print(t);
delay(5000);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.print(F("Humidity: "));
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(10, 0);
display.print(h);
display.print(F(" %"));
delay(5000);
}
아두이노 나노를 이용해 DHT22센서의 값을 읽어서 0.91인치 OLED 모듈에 나타내려고 합니다. 상단에 작게 Temperature: 을 쓰고 그 아래 좀 더 큰 글씨로 온도를 쓰고, 몇 초 뒤에 다시 상단에 Humidity: 를 쓰고 아래 습도와 % 기호를 쓰려고 합니다. 코드가 좀 길어 보이는데 중간에 if 문 부분은 led에 관한 내용이니 그냥 넘어가시면 됩니다. 코딩에 성공만 하면 내공 최대한 많이 드리겠습니다..
#include<SPI.h>
#include<Wire.h>
#include<Adafruit_GFX.h>
#include<Adafruit_SSD1306.h>
#include<Adafruit_Sensor.h>
#include<DHT.h>
#include<Adafruit_I2CDevice.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
#define OLED_RESET 4
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
#define RED 9
#define GREEN 10
#define BLUE 11
float h;
float t;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
dht.begin();
delay(2000);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
display.begin();
display.clearDisplay();
}
}
void loop() {
// put your main code here, to run repeatedly:
float h = dht.readHumidity();
float t = dht.readTemperature();
Serial.print("humidity");
Serial.print(h);
Serial.print("Temperature");
Serial.print(t);
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
}
else if (h<20) {
analogWrite(RED,(225));
analogWrite(GREEN,(0));
analogWrite(BLUE,(0));
}
else if (h<40) {
analogWrite(RED,(225));
analogWrite(GREEN,(225));
analogWrite(BLUE,(0));
}
else if (h<60) {
analogWrite(RED,(0));
analogWrite(GREEN,(225));
analogWrite(BLUE,(0));
}
else if (h<80) {
analogWrite(RED,(0));
analogWrite(GREEN,(225));
analogWrite(BLUE,(225));
}
else if (h<=100) {
analogWrite(RED,(0));
analogWrite(GREEN,(0));
analogWrite(BLUE,(225));
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.print(F("Temperature: "));
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(10, 0);
display.print(t);
delay(5000);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.print(F("Humidity: "));
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(10, 0);
display.print(h);
display.print(F(" %"));
delay(5000);
}