본문 바로가기

아두이노

아두이노 온도 습도 미터기 DHT11 과 OLED display 사용

반응형


아두이노 온도 습도 미터기 DHT11 과 OLED display 사용

Arduino Nano Humidity Meter with DHT11 and I2C OLED display


아두이노 나노를 사용하였다. 연결은 아래 두 개를 합친다.


아두이노 나노와 OLED 연결도 - OLED는 I2C 인터페이스 이므로 아래 그림과 같이 A4, A5 번이 SDA, SCL 에 연결해야 한다. 


VCC – VCC

GND - GND

SCL – A5

SDA – A4


이미지 출처 : https://www.instructables.com/id/Monochrome-096-i2c-OLED-display-with-arduino-SSD13/


 이렇게 연결을 한다.


다음은 아두이노 나노와 온도 습도 센서 DHT11 을 연결한다. 

주의할 점은 아래 도면은 아두이노 우노의 7번 핀에 연결되었지만 실제 소스코드는 2번 핀에 연결되므로 2번에 연결해 준다. 아니면 7번에 연결하고 코드에서 7번으로 수정해도 마찬가지로 잘 동작한다. 원리가 중요하다. 




아래 동작하는  소스코드를 옮긴다. 소스코드 출처는 https://gist.github.com/embeddeduser/e7adc347397f1b3d1a9ae7e2598b4dc3 이고 보드에 맞게 수정하였다. 로그인 해야 코드를 볼 수 있다. 참고하기 바란다. 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Written by EmbeddedUser 
// Refer to blog http://www.jaimeferns.in/2016/10/how-to-build-humidity-and-temperature.html
// Youtube Video https://www.youtube.com/watch?v=g-rcEd_2I-w
// Humidity meter with DHT11, Arduino and I2c OLED display
// Now support displaying sensor error and configuring deg C or deg F
 
#include <DHT.h>   //온도 습도센서 라이브러리
#include "U8glib.h"  //OLED 라이브러리
 
// Display defaults to Degree F. TO use metric display,
// comment out next line to display temperature in Degree C,
#define METRIC  //이걸 살리면 섭씨 셀지우스, 주석처리하면 화씨 온도표시
 
#define DHTPIN 2          // what digital pin we're connected to
// Uncomment whatever type you're using!
#define DHTTYPE DHT11     // DHT 11
//#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)
 
// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
 
// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors.  This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
DHT dht(DHTPIN, DHTTYPE);
 
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE);  // 사용하는 OLE에 맞는 것 선택 반드시!
//U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NO_ACK);  // Display which does not send AC
//U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0);  // I2C / TWI
//U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_DEV_0|U8G_I2C_OPT_NO_ACK|U8G_I2C_OPT_FAST); // Fast I2C / TWI
//U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NO_ACK);  // Display which does not send AC
 
const uint8_t logo[] PROGMEM =
{
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x180x00x380x60x00x00x00x00x00x00x10xc3,
  0x800x00x00x00x180x00x380x60x00x400x00x00x00x00x1,
  0xc30x800x00x00x00x00x00x380x00x00xc00x00x00x00x0,
  0x10xc30x800x00x00x00x00x00x380x00x00xc00x00x00x0,
  0x00x10xc30x8e0xc0x6e0x600xf80x70xb80x3e0x30xfc0x380x18,
  0x00x00x10xc30x8e0xc0x7e0xf00xf80xf0xf80x3e0x30xfc0x38,
  0x380x00x00x10xff0x8e0xc0x770x300x180xe0x380x60x00xc0,
  0x180x300x00x00x10xff0x8e0xc0x670x300x180x1c0x380x60x0,
  0xc00x1c0x300x00x00x10xff0x8e0xc0x630x300x180x1c0x380x6,
  0x00xc00xc0x700x00x00x10xc30x8e0xc0x630x300x180x1c0x18,
  0x60x00xc00xc0x600x00x00x10xc30x8e0xc0x630x300x180x1c,
  0x180x60x00xc00xe0x600x00x00x10xc30x860x1c0x630x300x18,
  0x1c0x180x60x00xc00x60xc00x00x00x10xc30x870x1c0x630x30,
  0x180xe0x380x60x00xe00x70xc00x00x00x10xc30x870xec0x63,
  0x310xff0x8f0xd80x7f0xe00xfe0x30xc00x00x00x10xc30x830xcc,
  0x630x310xff0x870x980x7f0xe00x7e0x30x800x00x00x00x00x0,
  0x00x00x00x00x00x100x00x00x00x30x800x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x70x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x1f0x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x1e0x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x3c0x380x00x00x800x00x00x0,
  0x00x00x00x00x00x00x00x00x3c0x780x00x10x800x00x0,
  0x00x00x00x00x00x00x00x00x00x3c0x780x00x10x800x0,
  0x00x00x00x00x00x00x00x00x00x00x3e0x780x3e0x70xf8,
  0xf0x810xcf0x00x00x00x00x00x00x00x00x360xd80x7f0x7,
  0xf80x1f0xc00xdf0x00x00x00x00x00x00x00x00x360xd80xe1,
  0x810x800x380x600xf00x00x00x00x00x00x00x00x00x330xd8,
  0xc10xc10x800x300x700xe00x00x00x00x00x00x00x00x00x33,
  0x990xff0xc10x800x7f0xf00xe00x00x00x00x00x00x00x00x0,
  0x330x990xff0xc10x800x7f0xf00xc00x00x00x00x00x00x00x0,
  0x00x310x990xc00x10x800x700x00xc00x00x00x00x00x00x0,
  0x00x00x300x180xc00x10x800x300x00xc00x00x00x00x00x0,
  0x00x00x00x300x180xe10xc10xc00x380x700xc00x00x00x00x0,
  0x00x00x00x00x300x180x7f0x810xfc0x1f0xe00xc00x00x00x0,
  0x00x00x00x00x00x300x180x3e0x00xfc0xf0x800xc00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x0
};
 
bool first = true;
float hum = 0.0;
float temp = 0.0;
float hIndex = 0.0;
bool dht_test(float* humPerc, float* tempF, float* heatIndex);
 
void setup(void)
{
  Serial.begin(9600);
  dht.begin();
  first = true;
 
  // assign default color value
  if (u8g.getMode() == U8G_MODE_R3G3B2)
  {
    u8g.setColorIndex(255);     // white
  }
  else if (u8g.getMode() == U8G_MODE_GRAY2BIT)
  {
    u8g.setColorIndex(3);         // max intensity
  }
  else if (u8g.getMode() == U8G_MODE_BW)
  {
    u8g.setColorIndex(1);         // pixel on
  }
  else if (u8g.getMode() == U8G_MODE_HICOLOR)
  {
    u8g.setHiColorByRGB(255255255);
  }
 
  // picture loop
  u8g.firstPage();
 
  do
  {
    u8g.drawBitmapP(001664, logo);
  }
  while (u8g.nextPage());
 
  dht_test(&hum, &temp, &hIndex);
}
void HumMeter(float* humPerc, float* temp, float* heatIndex)
{
  u8g.setFont(u8g_font_fub11);
  u8g.setFontRefHeightExtendedText();
  u8g.setDefaultForegroundColor();
  u8g.setFontPosTop();
  u8g.drawStr(40"Hum %");
  u8g.setPrintPos(800);
  u8g.print(*humPerc);
#ifdef METRIC
  // if metric system, display Celsius
  u8g.drawStr(420"Temp C");
#else
  //display Farenheit
  u8g.drawStr(420"Temp F");
#endif
 
  u8g.setPrintPos(8020);
  u8g.print(*temp);
  u8g.drawStr(440"Heat Ind");
  u8g.setPrintPos(8040);
  u8g.print(*heatIndex);
}
 
void loop(void)
{
  bool result = dht_test(&hum, &temp, &hIndex);
 
  if (first)
  {
    // skip displaying readings first time, as its stale data.
    first = false;
  }
  else
  {
    if(result == false)
    {
      u8g.firstPage();
      do 
      {
        // sensor error
        u8g.setFont(u8g_font_fub11);
        u8g.setFontRefHeightExtendedText();
        u8g.setDefaultForegroundColor();
        u8g.setFontPosTop();
        u8g.drawStr(1030"Sensor Error");
      }
      while (u8g.nextPage());
    }
    else
    {
      u8g.firstPage();
      do
      {
        HumMeter(&hum, &temp, &hIndex);
      }
      while (u8g.nextPage());
    }
  }
}
bool dht_test(float* humPerc, float* temp, float* heatIndex)
{
  // Wait a few seconds between measurements.
  delay(2000);
  *humPerc = 0;
  *temp = 0;
  *heatIndex = 0;
  
  // 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();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);
 
  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f))
  {
    Serial.println("Failed to read from DHT sensor!");
    return false;
  }
 
  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false);
 
  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.print(" *C ");
  Serial.print(f);
  Serial.print(" *F\t");
  Serial.print("Heat index: ");
  Serial.print(hic);
  Serial.print(" *C ");
  Serial.print(hif);
  Serial.println(" *F");
  *humPerc = h;
#ifdef METRIC
  // metric system, load degree celsius
  *temp = t;
  *heatIndex = hic;
#else
  *temp = f;
  *heatIndex = hif;
#endif
  return true;
}
 
 


아래는 동작사진이다. 실제 OLED 에 표시되는 모양과 씨리얼 포트에 출력하는 결과를 나타낸다.










반응형