반응형
아두이노 온도 습도 미터기 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 = { 0x0, 0x0, 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x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }; 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(255, 255, 255); } // picture loop u8g.firstPage(); do { u8g.drawBitmapP(0, 0, 16, 64, 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(4, 0, "Hum %"); u8g.setPrintPos(80, 0); u8g.print(*humPerc); #ifdef METRIC // if metric system, display Celsius u8g.drawStr(4, 20, "Temp C"); #else //display Farenheit u8g.drawStr(4, 20, "Temp F"); #endif u8g.setPrintPos(80, 20); u8g.print(*temp); u8g.drawStr(4, 40, "Heat Ind"); u8g.setPrintPos(80, 40); 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(10, 30, "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 에 표시되는 모양과 씨리얼 포트에 출력하는 결과를 나타낸다.
반응형
'아두이노' 카테고리의 다른 글
HM-10 BLE 모듈 사용 스마트폰으로 데이터 주기적으로 송신하기 (0) | 2019.02.26 |
---|---|
아두이노 나노 스마트폰과 HM-10 연동하기 1 - 초기 설정 (2) | 2019.02.25 |
아두이노 I2C 스캐너 소스코드, I2C scanner (1) | 2019.02.25 |
아두이노 나노 핀맵 - 아두이노 나노 I2C 핀 (0) | 2019.02.20 |
Fritzing 라이브러리 다운 받는 곳 (0) | 2019.01.28 |