Loading [MathJax]/jax/output/HTML-CSS/config.js

2018년 9월 27일 목요일

아두이노 우리지역 미세먼지 알리미 : 2. MKR1000에 2.2' TFT LCD(ILI9341) + DHT11

아두이노 우리동네 미세먼지 알리미
- 1. MKR1000 + 2.2' TFT LCD(ili9341) : http://woolmakes.blogspot.com/2018/09/1-mkr1000-28-tft-lcdili9341.html

LCD 테스트는 끝났으니까 이제는 온습도계를 달아보자. 겨울이 오면 바깥은 미세먼지 가득해지고 방 안은 건조해진다. 방 안의 습도도 중요하다.

이전에 작은 OLED를 사용해 만들어봤는데 너무 작아서 큰 LCD를 찾게 되었다. 그리고 생각보다 LCD가 싸다. ㅎㅎ

OLED에 나노를 연결했는데 OLED가 너무 작았다. 가족들 집에 만들어주면 작아서 화낼듯....





너무 작다. 그래서 큰 LCD가 필요했다. 2.2인치 LCD도 작은 느낌이 들어 더 큰 걸로 주문해뒀다.




재료
- MKR1000
- 2.2 TFT LCD(ili9341)
- DHT11 온습도 센서 : https://www.eleparts.co.kr/goods/view?no=3353799
.


- 10k 옴 저항 : https://www.eleparts.co.kr/goods/view?no=11466

이전 재료 둘은 이전 글을 보면 된다.
오픈 마켓에 검색하면 DHT11 센서는 1000원 정도 한다. 엘레파츠가 조금 비싸다....


회로

LCD가 연결된 회로는 그대로 둔다.

DHT11 핀은 4개이다.

MKR1000
DHT11
5V
1번핀
GND
4번핀
D2
2번핀

추가로 DHT11에서 1번과 2번핀 사이에 10k 옴 저항을 연결한다.



코드

DHT11을 쓰기 위해 DHT 라이브러리가 필요하다. 라이브러리 관리에서 DHT를 검색해서 Adafruit DHT sensor 라이브러리를 설치해도 되고, https://github.com/adafruit/DHT-sensor-library 여기서 zip 파일로 다운로드해 추가해도 된다.

DHT가 잘 작동하는지는 예제에서 추가한 라이브러리 예제를 찾아 DHTTester를 실행해보면 된다.
우리가 사용할 센서는 11이므로
DHT11 앞의 주석 처리된 //을 지우고, DHT22 앞에는 //으로 주석처리를 한다.

#define DHTTYPE DHT11   // DHT 11
//#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)


시리얼 모니터로 습도와 온도 데이터가 잘 뜨는게 확인되면 LCD로 띄워보자. 코드는 다음과 같다. 코드 뒤에 LCD를 테스트 하는 함수들은 지워도 좋다.


#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include 
#include 

// For MKR1000
#define TFT_MISO 10 // SDO/MISO
#define TFT_CLK 9 // SCK
#define TFT_MOSI 8 // SDI/MOSI
#define TFT_DC 7 // DC/RS
#define TFT_CS 6 // CS 
#define TFT_RST 5 // RESET
//LED - vcc(3.3V)

Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);

#define DHTPIN 2    
#define DHTTYPE DHT11
DHT dht(DHTPIN,DHTTYPE);

void setup() {
  Serial.begin(9600);
  Serial.println("ILI9341 Test!"); 
 
  tft.begin();

  // read diagnostics (optional but can help debug problems)
  uint8_t x = tft.readcommand8(ILI9341_RDMODE);
  Serial.print("Display Power Mode: 0x"); Serial.println(x, HEX);
  x = tft.readcommand8(ILI9341_RDMADCTL);
  Serial.print("MADCTL Mode: 0x"); Serial.println(x, HEX);
  x = tft.readcommand8(ILI9341_RDPIXFMT);
  Serial.print("Pixel Format: 0x"); Serial.println(x, HEX);
  x = tft.readcommand8(ILI9341_RDIMGFMT);
  Serial.print("Image Format: 0x"); Serial.println(x, HEX);
  x = tft.readcommand8(ILI9341_RDSELFDIAG);
  Serial.print("Self Diagnostic: 0x"); Serial.println(x, HEX); 
  
  Serial.println(F("Benchmark                Time (microseconds)"));
  delay(10);
  Serial.print(F("Screen fill              "));
  //Serial.println(testFillScreen());
  delay(500);

  Serial.println(F("Done!"));

}


void loop(void) {
  
  tft.fillScreen(ILI9341_BLACK);
  tft.setRotation(0); //0~3

  unsigned long start = micros();
  tft.setCursor(0, 0);
  tft.setTextColor(ILI9341_GREEN);
  tft.setTextSize(5);

  float h = dht.readHumidity();
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  tft.print("H : ");
  tft.print(int(h));
  tft.println("%");
  tft.print("T : ");
  tft.print(int(t));
  tft.println("*C ");
  delay(2000);
}

unsigned long testFillScreen() {
  unsigned long start = micros();
  tft.fillScreen(ILI9341_BLACK);
  yield();
  tft.fillScreen(ILI9341_RED);
  yield();
  tft.fillScreen(ILI9341_GREEN);
  yield();
  tft.fillScreen(ILI9341_BLUE);
  yield();
  tft.fillScreen(ILI9341_BLACK);
  yield();
  return micros() - start;
}

unsigned long testText() {
  tft.fillScreen(ILI9341_BLACK);
  unsigned long start = micros();
  tft.setCursor(0, 0);
  tft.setTextColor(ILI9341_WHITE);  tft.setTextSize(1);
  tft.println("Hello World!");
  tft.setTextColor(ILI9341_YELLOW); tft.setTextSize(2);
  tft.println(1234.56);
  tft.setTextColor(ILI9341_RED);    tft.setTextSize(3);
  tft.println(0xDEADBEEF, HEX);
  tft.println();
  tft.setTextColor(ILI9341_GREEN);
  tft.setTextSize(5);
  tft.println("Groop");
  tft.setTextSize(2);
  tft.println("I implore thee,");
  tft.setTextSize(1);
  tft.println("my foonting turlingdromes.");
  tft.println("And hooptiously drangle me");
  tft.println("with crinkly bindlewurdles,");
  tft.println("Or I will rend thee");
  tft.println("in the gobberwarts");
  tft.println("with my blurglecruncheon,");
  tft.println("see if I don't!");
  return micros() - start;
}

unsigned long testLines(uint16_t color) {
  unsigned long start, t;
  int           x1, y1, x2, y2,
                w = tft.width(),
                h = tft.height();

  tft.fillScreen(ILI9341_BLACK);
  yield();
  
  x1 = y1 = 0;
  y2    = h - 1;
  start = micros();
  for(x2=0; x20; i-=6) {
    i2    = i / 2;
    start = micros();
    tft.fillRect(cx-i2, cy-i2, i, i, color1);
    t    += micros() - start;
    // Outlines are not included in timing results
    tft.drawRect(cx-i2, cy-i2, i, i, color2);
    yield();
  }

  return t;
}

unsigned long testFilledCircles(uint8_t radius, uint16_t color) {
  unsigned long start;
  int x, y, w = tft.width(), h = tft.height(), r2 = radius * 2;

  tft.fillScreen(ILI9341_BLACK);
  start = micros();
  for(x=radius; x10; i-=5) {
    start = micros();
    tft.fillTriangle(cx, cy - i, cx - i, cy + i, cx + i, cy + i,
      tft.color565(0, i*10, i*10));
    t += micros() - start;
    tft.drawTriangle(cx, cy - i, cx - i, cy + i, cx + i, cy + i,
      tft.color565(i*10, i*10, 0));
    yield();
  }

  return t;
}

unsigned long testRoundRects() {
  unsigned long start;
  int           w, i, i2,
                cx = tft.width()  / 2 - 1,
                cy = tft.height() / 2 - 1;

  tft.fillScreen(ILI9341_BLACK);
  w     = min(tft.width(), tft.height());
  start = micros();
  for(i=0; i20; i-=6) {
    i2 = i / 2;
    tft.fillRoundRect(cx-i2, cy-i2, i, i, i/8, tft.color565(0, i, 0));
    yield();
  }

  return micros() - start;
}

댓글 없음:

댓글 쓰기